package com.engine.kq.cmd.leavetypes;

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

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

/**
 * @Auther: xiaj
 * @Date: 2019-03-07 16:42
 * @Description:
 */
public class DeleteLeaveTypesCmd extends AbstractCommonCommand<Map<String, Object>> {

    private SimpleBizLogger logger;

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

        String ruleIds = Util.null2String(params.get("typeIds"));//需要删除的ID
        BizLogContext bizLogContext = new BizLogContext();
        bizLogContext.setLogType(BizLogType.HRM_ENGINE);//模块类型
        bizLogContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_LEAVETYPES);//所属大类型
        bizLogContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_LEAVETYPES);//当前小类型
        bizLogContext.setParams(params);//当前request请求参数
        logger.setUser(user);//当前操作人
        String mainSql = "select * from kq_leaveRules where (isDelete is null or isDelete<>1) and id in ("+ruleIds+")";
        logger.setMainSql(mainSql, "id");//主表sql
        logger.setMainPrimarykey("id");//主日志表唯一key
        logger.setMainTargetNameColumn("leavename");
        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>();
        try {
            boolean canDelete = HrmUserVarify.checkUserRight("KQLeaveRulesDelete:Delete", user);//是否具有删除的权限
            if (!canDelete) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));//没有权限
                return resultMap;
            }

            String ruleIds = Util.null2String(params.get("typeIds"));//需要删除的ID

            if (ruleIds.equals("")) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(388858, user.getLanguage()));//参数有误
                return resultMap;
            }
            KQLeaveRulesComInfo kqLeaveRulesComInfo = new KQLeaveRulesComInfo();
            List<String> ruleIds_list = Util.splitString2List(ruleIds, ",");
            if(!ruleIds_list.isEmpty()){
              RecordSet rs = new RecordSet();
              for(String ruleId : ruleIds_list){
                String minimumUnit = kqLeaveRulesComInfo.getMinimumUnit(ruleId);
                if(Util.getIntValue(minimumUnit) != 2){
                  //如果请假类型不再是半天了，需要删除原半天设置
                  String del_2_sql = "delete from kq_timeselection where selectiontype=0 and leaveruleid=? and minimumUnit=2";
                  boolean is_2_del = rs.executeUpdate(del_2_sql,ruleId);
                  System.out.println("is_2_del:"+is_2_del);
                }
              }
            }

            String sql = "delete from kq_LeaveRules where id in (" + ruleIds + ")";
            RecordSet recordSet = new RecordSet();
            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) {
            e.printStackTrace();
            resultMap.put("sign", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(20462, user.getLanguage()));//删除失败
        }finally {
            KQLeaveRulesComInfo rulesComInfo = new KQLeaveRulesComInfo();
            rulesComInfo.removeCache();
        }
        return resultMap;
    }
}
