package com.engine.kq.cmd.travelrules;

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 com.engine.kq.biz.KQExitRulesComInfo;
import com.engine.kq.biz.KQTravelRulesComInfo;
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 SaveTravelRulesCmd extends AbstractCommonCommand<Map<String, Object>> {

    private SimpleBizLogger logger;

    public SaveTravelRulesCmd(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_TRAVELRULES);//所属大类型
        bizLogContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQ_TRAVELRULES);//当前小类型
        bizLogContext.setParams(params);//当前request请求参数
        logger.setUser(user);//当前操作人
        String mainSql = "select 1 as id,minimumUnit,proportion,computingMode,hoursToDay from kq_travelRules union all " +
                " select 2 as id,minimumUnit,proportion,computingMode,hoursToDay from kq_exitRules";
        logger.setMainSql(mainSql, "id");//主表sql
        logger.setMainPrimarykey("id");//主日志表唯一key
        logger.setMainTargetNameMethod("com.engine.kq.util.KQTransMethod.getTargetName4TravelRules",""+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>();
        try{
            boolean canAdd = HrmUserVarify.checkUserRight("KQTravelRulesAdd:Add", user);//是否具有新建的权限;
            boolean canEdit = HrmUserVarify.checkUserRight("KQTravelRulesEdit:Edit", user);//是否具有编辑的权限;
            if (!canAdd && !canEdit) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return resultMap;
            }

            /*出差规则*/
            String ruleId = "1";//暂为备用字段，规则ID，固定为1
            String name = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20084,weaver.general.ThreadVarLanguage.getLang())+"";//暂为备用字段，规则名称，目前固定为出差
            int minimumUnit = Util.getIntValue((String) params.get("minimumUnit"), 1);//最小出差单位：1-按天出差、2-按半天出差、3-按小时出差、4-按整天出差
            double proportion = Util.getDoubleValue((String) params.get("proportion"), 8.00);//换算比例(当单位由天、半天、整天切换成小时或者由小时切换成天、半天、整天时，历史数据的转换比例)
            int computingMode = Util.getIntValue((String) params.get("computingMode"), 1);//计算公出时长方式：1-按工作日计算出差时长、2-按自然日计算出差时长
            double hoursToDay = Util.getDoubleValue((String) params.get("hoursToDay"), 8.00);//日折算时长
            int travel_timeselection = Util.getIntValue((String) params.get("travel_timeselection"), 1);//半天单位 时间选择方式：1-下拉框选择 、2-具体时间

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

            boolean flag = true;
            String sql = "update kq_travelRules set minimumUnit=?,proportion=?,computingMode=?,hoursToDay=?,timeselection=? where id=?";
            RecordSet recordSet = new RecordSet();
            flag = recordSet.executeUpdate(sql, minimumUnit, proportion, computingMode, hoursToDay,travel_timeselection, ruleId);
            if (!flag) {
                resultMap.put("sign", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));//保存失败
                return resultMap;
            }

            /*公出规则*/
            String exit_ruleId = "1";//暂为备用字段，规则ID，固定为1
            String exit_name = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20084,weaver.general.ThreadVarLanguage.getLang())+"";//暂为备用字段，规则名称，目前固定为出差
            int exit_minimumUnit = Util.getIntValue((String) params.get("exit_minimumUnit"), 1);//最小公出单位：1-按天公出、2-按半天公出、3-按小时公出、4-按整天公出
            double exit_proportion = Util.getDoubleValue((String) params.get("exit_proportion"), 8.00);//换算比例(当单位由天、半天、整天切换成小时或者由小时切换成天、半天、整天时，历史数据的转换比例)
            int exit_computingMode = Util.getIntValue((String) params.get("exit_computingMode"), 1);//计算公出时长方式：1-按工作日计算公出时长、2-按自然日计算公出时长
            double exit_hoursToDay = Util.getDoubleValue((String) params.get("exit_hoursToDay"), 8.00);//日折算时长
            int exit_timeselection = Util.getIntValue((String) params.get("exit_timeselection"), 1);//半天单位 时间选择方式：1-下拉框选择 、2-具体时间

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

            sql = "update kq_exitRules set minimumUnit=?,proportion=?,computingMode=?,hoursToDay=?,timeselection=? where id=?";
            flag = recordSet.executeUpdate(sql, exit_minimumUnit, exit_proportion, exit_computingMode, exit_hoursToDay,exit_timeselection, exit_ruleId);
            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();
        }finally {
            KQExitRulesComInfo kqExitRulesComInfo = new KQExitRulesComInfo();
            KQTravelRulesComInfo kqTravelRulesComInfo = new KQTravelRulesComInfo();
            KQAttFlowSetComInfo kqAttFlowSetComInfo = new KQAttFlowSetComInfo();

            kqExitRulesComInfo.removeCache();
            kqTravelRulesComInfo.removeCache();
            kqAttFlowSetComInfo.removeKQAttFlowSetComInfoCache();
        }
        return resultMap;
    }
}
