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.HrmUserVarify;
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 DeleteHolidaySetCmd extends AbstractCommonCommand<Map<String, Object>> {

    private SimpleBizLogger logger;

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

        String ids = Util.null2String(params.get("ids"));//需要删除的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 id in (" + ids + ")";
        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"));//需要删除的Id

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            boolean canDelete = HrmUserVarify.checkUserRight("KQHolidaySetDelete:Delete", user);
            if (!canDelete) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return resultMap;
            }

            /*预先加载删除了哪些考勤组的哪些日期的节假日设置*/
            String formateSql = "select * from kq_HolidaySet where id in (" + ids + ")";
            RecordSet recordSet = new RecordSet();
            recordSet.executeQuery(formateSql);
            while (recordSet.next()) {
                String groupId = recordSet.getString("groupId");
                String holidayDate = recordSet.getString("holidayDate");

                map = new HashMap<String, Object>();
                map.put("groupId", groupId);
                map.put("holidayDate", holidayDate);
                list.add(map);
            }

            /*删除节假日设置*/
            String sql = "delete from kq_HolidaySet where id in (" + ids + ")";
            boolean flag = recordSet.executeUpdate(sql);

            if (flag) {
                resultMap.put("sign", "1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(20461, user.getLanguage()));//删除成功
            } else {
                resultMap.put("sign", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(20462, user.getLanguage()));//删除失败
            }
        } catch (Exception e) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));//发生未知错误，请与管理员联系
            writeLog(e);
        } finally {
            /*刷新缓存*/
            KQHolidaySetComInfo holidaySetComInfo = new KQHolidaySetComInfo();
            holidaySetComInfo.removeCache();

            /*刷新报表*/
            List<String> groupIdList = new ArrayList<String>();
            List<String> holidayDateList = new ArrayList<String>();
            for (int i = 0; i < list.size(); i++) {
                map = list.get(i);
                String groupId = (String) map.get("groupId");
                String holidayDate = (String) map.get("holidayDate");
                if(!groupIdList.contains(groupId)){
                    groupIdList.add(groupId);
                }
                if(!holidayDateList.contains(holidayDate)){
                    holidayDateList.add(holidayDate);
                }
            }
            new KQHolidaySetThread(groupIdList,holidayDateList).start();
        }
        return resultMap;
    }
}
