package com.engine.voting.cmd.VotingList;

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

import org.apache.commons.lang3.StringUtils;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;
import weaver.general.PageIdConst;
import weaver.general.Util;

import com.alibaba.fastjson.JSON;
import com.api.voting.util.VotingTableType;
import com.cloudstore.dev.api.bean.SplitMobileDataBean;
import com.cloudstore.dev.api.util.Util_TableMap;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.voting.util.VotingPageUidFactory;
import com.engine.voting.biz.VotingGetSqlWhere;
import com.engine.voting.biz.VotingCreateMobileTemplate;
import com.engine.voting.biz.VotingGetVotingIdByShare;
/**
 * 前台页面调查结果列表：获取当前用户拥有权限查看调查结果的调查列表
 */

public class VotingGetVotingResultListCmd extends AbstractCommonCommand<Map<String, Object>>{
    public VotingGetVotingResultListCmd(User user, Map<String, Object> params) {
        this.user = user;
        this.params = params;
    }

    private static final String JSON_CONFIG = "[ \n" + "]";
	 
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        params = params == null ? new HashMap<String, Object>() : params;
        String loginType = user.getLogintype();
        String backFields = " id,descr, subject,begindate,begintime,enddate,endtime,createrid,approverid,status,createdate ,createtime,istemplate,id countid ";
        String fromSql = " voting ";
        VotingGetVotingIdByShare votinggetvotingidbyshare = new VotingGetVotingIdByShare();
        String sqlWhere = " where id in(" + votinggetvotingidbyshare.getVotingIdByShare(user) + ")";
        sqlWhere += " and status in (1,2) ";
        sqlWhere += " and (istemplate <> '1' or istemplate is null)";

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


        VotingGetSqlWhere votinggetsqlwhere = new VotingGetSqlWhere();
        sqlWhere += votinggetsqlwhere.getSqlWhere(params);
        String orderBy = "id";
        String operateString = "";
        VotingTableType tableType = VotingTableType.VOTING_RESULT;
        String pageId = "VOTING_RESULT";
        String pageUid = VotingPageUidFactory.getPageUid(pageId);
        VotingCreateMobileTemplate votingcreatemobiletemplate = new VotingCreateMobileTemplate();
        String mobileTemplate = votingcreatemobiletemplate.createMobileTemplate(JSON.parseArray(JSON_CONFIG,
                SplitMobileDataBean.class));
        String tableString = "";
        tableString = "<table mobileshowtemplate=\"" + mobileTemplate + "\" pageUid=\"" + pageUid + "\" pageId=\""
                + pageId + "\" pagesize=\"" + PageIdConst.getPageSize(pageId, user.getUID(), PageIdConst.VOTING)
                + "\" tabletype=\"none\">";
        // tableString
        // +="<checkboxpopedom id=\"checkbox\" popedompara=\""+checkboxpopedompara+"\" showmethod=\"weaver.voting.VotingManager.getVotingListCheckBoxRight\" />";
        tableString += "<sql backfields=\"" + backFields + "\" sqlform=\"" + Util.toHtmlForSplitPage(fromSql)
                + "\" sqlorderby=\"" + orderBy + "\"  sqlprimarykey=\"id\" sqlsortway=\"Desc\" sqlwhere=\""
                + Util.toHtmlForSplitPage(sqlWhere) + "\" />";
        tableString += operateString;
        tableString += "<head>"
                + "<col width=\"8%\"  text=\""+ SystemEnv.getHtmlLabelName(15486, user.getLanguage())+ "\" column=\"id\" orderkey=\"id\" />"
                + "<col width=\"30%\" text=\""+ SystemEnv.getHtmlLabelName(195, user.getLanguage())+ "\" column=\"subject\" orderkey=\"subject\" />"
                + "<col width=\"10%\"  text=\""+ SystemEnv.getHtmlLabelName(882, user.getLanguage())+ "\" column=\"createrid\" orderkey=\"createrid\" transmethod=\"weaver.splitepage.transform.SptmForDoc.getName\" otherpara=\""+ loginType+ "\" />"
                + "<col width=\"20%\"  text=\""+ SystemEnv.getHtmlLabelName(740, user.getLanguage())+ "\" column=\"begindate\" orderkey=\"begindate\" otherpara=\"column:begintime\" transmethod=\"com.api.voting.service.VotingTransMethod4E9.getDateAndTime\" />"
                + "<col width=\"20%\"  text=\""+ SystemEnv.getHtmlLabelName(741, user.getLanguage())+ "\" column=\"enddate\" orderkey=\"enddate\"  otherpara=\"column:endtime\" transmethod=\"com.api.voting.service.VotingTransMethod4E9.getDateAndTime\" />"
                + "<col width=\"12%\"  text=\"" + SystemEnv.getHtmlLabelName(602, user.getLanguage())+ "\" column=\"status\" orderkey=\"status\" otherpara=\"" + user.getLanguage()+ "\" transmethod=\"weaver.voting.VotingManager.getStatus\"/>";
        String isFromMobile = Util.null2String(params.get("_ec_ismobile"));
        if ("true".equals(isFromMobile)) {
            tableString += "<col text=\"\" column=\"begintime\" orderkey=\"begintime\" />"
                    + "<col text=\"\" column=\"endtime\" orderkey=\"endtime\" />"
                    + "<col text=\"\" column=\"countid\" orderkey=\"countid\" transmethod=\"com.api.voting.service.VotingTransMethod4E9.getVotingRemarkCount\" />";
        }
        tableString += "</head>" + "</table>";

        String sessionkey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
        Util_TableMap.setVal(sessionkey, tableString);
        apidatas.put("sessionkey", sessionkey);
        // String isFromMobile = Util.null2String(params.get("_ec_ismobile"));
        // if("true".equals(isFromMobile)){
        // //已参与调查中的已参与人数
        // Map<String, String> remarkMap = getVotingResultRemarkCount(votingShareSql, params);
        // apidatas.put("remarkMap", remarkMap);
        // }
        return apidatas;
    }
	 
    public BizLogContext getLogContext() {
        return null;
    }
}
