package com.engine.voting.cmd.VotingList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.systeminfo.setting.HrmUserSettingComInfo;
import weaver.conn.RecordSet;
import weaver.general.TimeUtil;
import weaver.general.Util;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.voting.biz.VotingGetVotingIdByShare;
/**
 * 与我相关，未参与，我发起，已参与 调查数量
 * @author ZH
 *
 */
public class VotingGetMyVotingNumCmd extends AbstractCommonCommand<Map<String, Object>> {
    public VotingGetMyVotingNumCmd(User user, Map<String, Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        VotingGetVotingIdByShare votinggetvotingidbyshare = new VotingGetVotingIdByShare();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        Map<String, Object> data = new HashMap<String, Object>();

        RecordSet rs = new RecordSet();

        String votingCurrentDate = TimeUtil.getCurrentDateString();
        String votingCurrentTime = TimeUtil.getOnlyCurrentTimeString();

        HrmUserSettingComInfo userSetting = new HrmUserSettingComInfo();
        String belongtoshow = userSetting.getBelongtoshowByUserId(user.getUID() + "");
        String account_type = user.getAccount_type();
        String belongtoids = user.getBelongtoids();
        if (belongtoshow.equals("1") && account_type.equals("0") && !belongtoids.equals("")) {
            belongtoids += "," + user.getUID();
        } else {
            belongtoids = user.getUID() + "";
        }

        String sql = "select count(id) voteNum from Voting where id in("
                + votinggetvotingidbyshare.getJoinVotingIdByShare(user)
                + ")  and (istemplate <> '1' or istemplate is null)";

        sql += " and status=1 ";
        sql += " and (beginDate<'" + votingCurrentDate + "' or (beginDate='" + votingCurrentDate
                + "' and (beginTime is null or beginTime='' or beginTime<='" + votingCurrentTime + "')))";
        sql += " and (";

        List<String> belongtoidsList = Util.TokenizerString(belongtoids, ",");
        for (int k = 0; k < belongtoidsList.size(); k++) {// 次账号存在于参与人范围，又不存在提交remark表
            if (k == 0) {
                sql += " (exists(select 1 from VotingShareDetail where votingid=voting.id and resourceid="
                        + belongtoidsList.get(k) + ")";
                sql += " and not exists(select 1 from VotingRemark where votingid=voting.id and resourceid="
                        + belongtoidsList.get(k) + "))";
            } else {
                sql += " or (exists(select 1 from VotingShareDetail where votingid=voting.id and resourceid="
                        + belongtoidsList.get(k) + ")";
                sql += " and not exists(select 1 from VotingRemark where votingid=voting.id and resourceid="
                        + belongtoidsList.get(k) + "))";
            }
        }

        sql += ") ";

        BaseBean bb = new BaseBean();
        String voting_type = Util.null2String(params.get("votingtype"));
        bb.writeLog("voting_type:"+voting_type);
        if(StringUtils.isNotBlank(voting_type)){
            sql += " and votingtype in("+voting_type+")";
        }
        bb.writeLog("sql:"+sql);

        rs.executeQuery(sql);
        if (rs.next()) {// 未参与
            String voteNum = rs.getString("voteNum");
            data.put("unjoin", voteNum);
        }

        sql = "select count(id) voteNum from Voting where id in("
                + votinggetvotingidbyshare.getJoinVotingIdByShare(user)
                + ")  and (istemplate <> '1' or istemplate is null)";
        sql += " and exists";
        sql += " (select 1 from VotingRemark where votingid=voting.id and resourceid"
                + (belongtoids.contains(",") ? (" in (" + belongtoids + ")") : ("=" + belongtoids)) + ")";

        if(StringUtils.isNotBlank(voting_type)){
            sql += " and votingtype in("+voting_type+")";
        }
        bb.writeLog("sql:"+sql);
        rs.executeQuery(sql);
        if (rs.next()) {// 我参与
            String voteNum = rs.getString("voteNum");
            data.put("join", voteNum);
        }

        sql = "select count(id) voteNum from Voting where (istemplate <> '1' or istemplate is null) and status in(1,2) and createrid ="
                + user.getUID();

        if(StringUtils.isNotBlank(voting_type)){
            sql += " and votingtype in("+voting_type+")";
        }
        bb.writeLog("sql:"+sql);
        rs.executeQuery(sql);
        if (rs.next()) {// 我发起
            String voteNum = rs.getString("voteNum");
            data.put("create", voteNum);
        }

        sql = "select count(id) voteNum from Voting where (istemplate <> '1' or istemplate is null) and status in(1,2)";
        sql += " and (beginDate<'" + votingCurrentDate + "' or (beginDate='" + votingCurrentDate
                + "' and (beginTime is null or beginTime='' or beginTime<='" + votingCurrentTime + "')))";
        sql += " and (id in(" + votinggetvotingidbyshare.getJoinVotingIdByShare(user) + ") or createrid ="
                + user.getUID() + ")";
        // System.out.println("sql--------:"+sql);

        if(StringUtils.isNotBlank(voting_type)){
            sql += " and votingtype in("+voting_type+")";
        }
        bb.writeLog("sql:"+sql);

        rs.executeQuery(sql);
        if (rs.next()) {// 与我有关
            String voteNum = rs.getString("voteNum");
            data.put("related", voteNum);
        }

        apidatas.put("data", data);

        return apidatas;
    }

    public BizLogContext getLogContext() {
        return null;
    }
}
