package com.engine.kq.cmd.overtimerules;

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.KQAttFlowSetComInfo;
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;

/**
 * 加班单位--保存
 */
public class SaveOvertimeUnitCmd extends AbstractCommonCommand<Map<String, Object>> {

    private SimpleBizLogger logger;

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

        BizLogContext bizLogContext = new BizLogContext();
        bizLogContext.setLogType(BizLogType.HRM_ENGINE);//模块类型
        bizLogContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_OVERTIMEUNIT);//所属大类型
        bizLogContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_OVERTIMEUNIT);//当前小类型
        bizLogContext.setParams(params);//当前request请求参数
        logger.setUser(user);//当前操作人
        String mainSql = "select * from kq_overtimeUnit";
        logger.setMainSql(mainSql, "id");//主表sql
        logger.setMainPrimarykey("id");//主日志表唯一key
        logger.setMainTargetNameMethod("com.engine.kq.util.KQTransMethod.getTargetName4OvertimeUnit",""+user.getLanguage());
        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>();

        /* 加班规则维护权限 */
        boolean canAdd = HrmUserVarify.checkUserRight("KQOvertimeRulesAdd:Add", user);//是否具有新建权限
        boolean canEdit = HrmUserVarify.checkUserRight("KQOvertimeRulesEdit:Edit", user);//是否具有编辑的权限;
        if (!canAdd && !canEdit) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
            return resultMap;
        }

        //是否设置了【流程和打卡取交集的加班方式】
        boolean hasComputingmode4 = false;
        RecordSet recordSet1 = new RecordSet();
        String checkSql = "select * from kq_overtimerulesdetail where computingmode = 4  and ruleid not in(select id from  kq_overtimerules where isdelete =1) ";
        recordSet1.executeQuery(checkSql);
        if(recordSet1.next()){
          hasComputingmode4 = true;
        }


        int id = 1;////暂为备用字段，加班单位ID，固定为1
        int minimumUnit = Util.getIntValue((String) params.get("minimumUnit"), 1);//最小加班单位：1-按天加班、2-按半天加班、3-按小时加班、4-按整天加班
        int overtimeConversion = Util.getIntValue((String) params.get("overtimeConversion"), 1);//加班时长折算方式：1-四舍五入、2-向上取整、3-向下取整
        double hoursToday = Util.getDoubleValue((String) params.get("hoursToday"), -1.00);//日折算时长
        int timeselection = Util.getIntValue((String) params.get("timeselection"), 1);//半天单位 时间选择方式：1-下拉框选择 、2-具体时间
      if(hasComputingmode4){
        if(minimumUnit == 2 || minimumUnit == 4){
          resultMap.put("sign", "-1");
          resultMap.put("message", SystemEnv.getHtmlLabelName(529168, user.getLanguage()));
          return resultMap;
        }
      }

        if(minimumUnit != 2){
          RecordSet rs = new RecordSet();
          //如果出差单位不再是半天了，需要删除原半天设置
          String del_2_sql = "delete from kq_timeselection where selectiontype=3 and minimumUnit=2";
          boolean is_2_del = rs.executeUpdate(del_2_sql);
        }

        boolean flag = true;
        String sql = "update kq_OvertimeUnit set minimumUnit=?,hoursToday=?,timeselection=?,overtimeConversion=? where id=?";
        RecordSet recordSet = new RecordSet();
        flag = recordSet.executeUpdate(sql, minimumUnit, hoursToday,timeselection,overtimeConversion, id);
        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()));//保存失败
        }

        KQAttFlowSetComInfo kqAttFlowSetComInfo = new KQAttFlowSetComInfo();
        kqAttFlowSetComInfo.removeKQAttFlowSetComInfoCache();

        return resultMap;
    }
}
