package com.engine.kq.cmd.group;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.biz.SimpleBizLogger;
import com.engine.common.constant.BizLogSmallType4Hrm;
import com.engine.common.constant.BizLogType;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQFixedSchedulceComInfo;
import com.engine.kq.biz.KQFormatBiz;
import com.engine.kq.biz.KQGroupComInfo;
import com.engine.kq.biz.KQGroupMemberComInfo;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.common.DateUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.Reminder.KQAutoCardTask;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;

/**
 * 获取考勤组管理tab页签
 *
 * @author pzy
 */
public class SaveAddMemberCmd extends AbstractCommonCommand<Map<String, Object>> {

    private SimpleBizLogger logger;
    public SaveAddMemberCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> retmap = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        try {
            if (!HrmUserVarify.checkUserRight("HrmKQGroup:Add", user)) {
                retmap.put("status", "-1");
                retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return retmap;
            }

            String ids = Util.null2String(params.get("ids"));
            String groupId = Util.null2String(params.get("groupId"));

            String sql = "INSERT INTO kq_groupmember (groupid, typevalue, type) ";
            sql += "VALUES (?,?,?)";
            String type = "1";

            List<String> list = Util.splitString2List(ids, ",");
            List paramList = new ArrayList();
            List param = null;
            for(String id : list){
                param = new ArrayList();
                param.add(groupId);
                param.add(id);
                param.add(type);
                paramList.add(param);
            }
            rs.executeBatchSql(sql, paramList);

            //refresh data
            KQGroupComInfo kQGroupComInfo = new KQGroupComInfo();
            String kqType = kQGroupComInfo.getKqtype(groupId);
            kQGroupComInfo.removeCache();
            new KQGroupMemberComInfo().removeCache();
            if (kqType.equals("1")) new KQFixedSchedulceComInfo().removeCache();
            new KQFormatBiz().formatDateByGroupId(groupId, DateUtil.getCurrentDate());

            this.boforeLog(groupId);
            retmap.put("status", "1");
        } catch (Exception e) {
            writeLog(e);
            retmap.put("status", "-1");
            retmap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
        }
        return retmap;
    }

    private void boforeLog(String id){
        this.logger = new SimpleBizLogger();
        BizLogContext logContext = new BizLogContext();
        logContext.setDateObject(new Date());
        logContext.setLogType(BizLogType.HRM_ENGINE);
        logContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_KQGROUP);
        logContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQGROUP);
        logContext.setParams(params);
        logger.setUser(user);//当前操作人

        String mainSql = " select * from kq_group where id= " + id;
        logger.setMainSql(mainSql);//主表sql
        logger.setMainPrimarykey("id");//主日志表唯一key
        logger.setMainTargetNameColumn("groupname");

        SimpleBizLogger.SubLogInfo subLogInfo1 = logger.getNewSubLogInfo();
        String subSql1 = "select * from kq_groupmember where groupid="+id;
        subLogInfo1.setSubTargetNameColumn("typevalue");
        subLogInfo1.setGroupId("0");  //所属分组， 按照groupid排序显示在详情中， 不设置默认按照add的顺序。
        subLogInfo1.setSubGroupNameLabel(505654); //在详情中显示的分组名称，不设置默认显示明细x
        subLogInfo1.setSubSql(subSql1);
        logger.addSubLogInfo(subLogInfo1);

        logger.before(logContext);
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public List<BizLogContext> getLogContexts() {
        return logger.getBizLogContexts();
    }

}
