package com.engine.kq.cmd.holidaySet;

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.*;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

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

/**
 * 节假日设置--同步
 */
public class SyncHolidaySetCmd extends AbstractCommonCommand<Map<String, Object>> {

    private SimpleBizLogger logger;

    public SyncHolidaySetCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
        this.logger = new SimpleBizLogger();

        String syncGroupIds = Util.null2String(params.get("syncGroupIds"));//需要同步的考勤组ID
        BizLogContext bizLogContext = new BizLogContext();
        bizLogContext.setLogType(BizLogType.HRM_ENGINE);//模块类型
        bizLogContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_HOLIDAYSET);//所属大类型
        bizLogContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_HOLIDAYSET);//当前小类型
        bizLogContext.setParams(params);//当前request请求参数
        logger.setUser(user);//当前操作人
        String mainSql = "select * from kq_HolidaySet where groupId in (" + syncGroupIds + ")";
        logger.setMainSql(mainSql, "id");//主表sql
        logger.setMainPrimarykey("id");//主日志表唯一key
        logger.setMainTargetNameMethod("com.engine.kq.util.KQTransMethod.getTargetName4HolidaySet", "column:groupId+column:holidayDate+" + user.getLanguage());//当前targetName对应的列（对应日志中的对象名）
        logger.before(bizLogContext);
    }

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

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String ids = Util.null2String(params.get("ids"));//kq_HolidaySet的主键ID
        String syncGroupIds = Util.null2String(params.get("syncGroupIds"));//考勤组的ID
        try {
            boolean flag = true;

            String deleteSql = "delete from kq_HolidaySet where groupId=? and holidayDate=?";
            String insertSql = " insert into kq_HolidaySet(groupId,holidayDate,changeType,holidayDesc,relatedDay) values(?,?,?,?,?)";

            String[] idArr = ids.split(",");
            String[] groupIdArr = syncGroupIds.split(",");
            RecordSet recordSet = new RecordSet();
            KQHolidaySetComInfo kqHolidaySetComInfo = new KQHolidaySetComInfo();
            for (int i = 0; i < idArr.length; i++) {
                String id = idArr[i];
                String sql = "select * from kq_holidaySet where id=?";
                recordSet.executeQuery(sql, id);
                if(recordSet.next()) {
                    String holidayDate = recordSet.getString("holidayDate");
                    String holidayDesc = recordSet.getString("holidayDesc");
                    String changeType = recordSet.getString("changeType");
                    String relatedDay = recordSet.getString("relatedDay");

                    KQGroupComInfo kqGroupComInfo = new KQGroupComInfo();
                    for (int j = 0; j < groupIdArr.length; j++) {
                        String groupId = groupIdArr[j];

                        if (Util.null2String(kqGroupComInfo.getKqtype(groupId)).equals("2") && changeType.equals("2")) {
                            continue;
                        }
                        recordSet.executeUpdate(deleteSql, groupId, holidayDate);
                        flag = recordSet.executeUpdate(insertSql, groupId, holidayDate, changeType, holidayDesc, relatedDay);
                        if (!flag) {
                            resultMap.put("sign", "-1");
                            resultMap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));//保存失败
                            return resultMap;
                        }
                    }
                }
            }

            if (flag) {
                resultMap.put("sign", "1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(83551, user.getLanguage()));//保存成功
            } else {
                resultMap.put("sign", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));//保存失败
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("sign", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));//保存失败
        } finally {
            /*刷新缓存*/
            KQHolidaySetComInfo kqHolidaySetComInfo = new KQHolidaySetComInfo();
            kqHolidaySetComInfo.removeCache();

            /*刷新考勤报表*/
            List<String> holidatDateList = new ArrayList<String>();
            RecordSet recordSet = new RecordSet();
            String sql = " select * from kq_holidaySet where id in (" + ids + ") ";
            recordSet.executeQuery(sql);
            while (recordSet.next()) {
                String holidayDate = recordSet.getString("holidayDate");
                if (!holidatDateList.contains(holidayDate)) {
                    holidatDateList.add(holidayDate);
                }
            }
            List<String> groupIdList = Util.TokenizerString(syncGroupIds,",");
            new KQHolidaySetThread(groupIdList,holidatDateList).start();
        }
        return resultMap;
    }
}
