package com.engine.kq.cmd.leavetypes;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
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;

/**
 * @Auther: xiaj
 * @Date: 2019-03-07 16:29
 * @Description:假期类型--获取新建或者编辑的菜单
 */
public class GetLeaveTypesFormCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetLeaveTypesFormCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            boolean canEdit = HrmUserVarify.checkUserRight("KQLeaveRulesEdit:Edit", user);//是否具有编辑权限
            if (!canEdit) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return resultMap;
            }

            /*假期类型的ID*/
            String ruleId = Util.null2String(params.get("typeId"));

            /*假期类型的名称*/
            String leaveName = "";

            /*流程可见范围：0-总部、1-分部(只有在此范围内的人请假的时候才能看见此假期类型)*/
            int scopeType = 0;

            /*流程可见范围为分部时，选择的分部ID*/
            String scopeValue = "";

            /*最小请假单位：1-按天请假、2-按半天请假、3-按小时请假、4-按整天请假*/
            int minimumUnit = 1;
          //折算方式：1-四舍五入、2-向上取整、3-向下取整
            int conversion = 1;

            /*换算比例*/
            double proportion = 8.00;

            /*计算请假时长方式：1-按工作日计算请假时长、2-按自然日计算请假时长*/
            int computingMode = 1;

            /*日折算时长(按自然日计算请假时长时有效)*/
            double hoursToDay = 8.00;

            /*按自然日计算请假时长，0-不排除，1-排除节假日，2-排除休息日，3-排除节假日和休息日*/
            String filterHolidays ="0";

            /*启用假期余额：0-不启用、1-启用*/
            int balanceEnable = 0;

            /*是否启用：0-未启用、1-启用*/
            int isEnable = 1;

            //半天单位 时间选择方式：1-下拉框选择 、2-具体时间
            int timeselection = 1;

            //显示顺序
            double showOrder = getMaxShowOrder();

            if (!ruleId.equals("")) {
                String sql = "select * from kq_LeaveRules where (isDelete is null or isDelete<>1) and id=?";
                RecordSet recordSet = new RecordSet();
                recordSet.executeQuery(sql, ruleId);
                if (recordSet.next()) {
                    leaveName = recordSet.getString("leaveName");
                    scopeType = Util.getIntValue(recordSet.getString("scopeType"), 0);
                    scopeValue = recordSet.getString("scopeValue");
                    minimumUnit = recordSet.getInt("minimumUnit");
                    conversion = recordSet.getInt("conversion");
                    conversion = conversion < 1 ? 1 : conversion;
                    proportion = Util.getDoubleValue(recordSet.getString("proportion"), 8.00);
                    computingMode = recordSet.getInt("computingMode");
                    hoursToDay = Util.getDoubleValue(recordSet.getString("hoursToDay"), 8.00);
                    filterHolidays = Util.null2String(recordSet.getString("filterHolidays"),"0");
                    balanceEnable = recordSet.getInt("balanceEnable");
                    isEnable = Util.getIntValue(recordSet.getString("isEnable"), 0);
                    showOrder = Util.getDoubleValue(recordSet.getString("showOrder"), 0.00);
                    timeselection = recordSet.getInt("timeselection");
                    timeselection = timeselection < 1 ? 1 : timeselection;
                }
            }

            List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>();
            Map<String, Object> groupItem = new HashMap<String, Object>();
            List<Object> itemList = new ArrayList<Object>();
            HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
            SearchConditionItem searchConditionItem = null;
            HrmFieldBean hrmFieldBean = null;

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("leaveName");//假期类型的名称
            hrmFieldBean.setFieldlabel("388883");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("1");
            hrmFieldBean.setFieldvalue(leaveName);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|string");
            if (hrmFieldBean.getViewAttr() == 1) {
                Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
                OtherParamsMap.put("hasBorder", true);
                searchConditionItem.setOtherParams(OtherParamsMap);
            }
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("scopeType");//流程可见范围
            hrmFieldBean.setFieldlabel("390881");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            List<SearchConditionOption> optionsList = new ArrayList<SearchConditionOption>();
            optionsList.add(new SearchConditionOption("0", SystemEnv.getHtmlLabelName(140, user.getLanguage()), scopeType == 0));
            optionsList.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(33553, user.getLanguage()), scopeType == 1));
            searchConditionItem.setOptions(optionsList);
            searchConditionItem.setRules("required|string");
            searchConditionItem.setHelpfulTip(SystemEnv.getHtmlLabelName(500951, user.getLanguage()));
            if (hrmFieldBean.getViewAttr() == 1) {
                Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
                OtherParamsMap.put("hasBorder", true);
                searchConditionItem.setOtherParams(OtherParamsMap);
            }
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("scopeValue");//分部
            hrmFieldBean.setFieldlabel("33553");
            hrmFieldBean.setFieldhtmltype("3");
            hrmFieldBean.setType("170");
            hrmFieldBean.setFieldvalue(scopeValue);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.getBrowserConditionParam().getDataParams().put("rightStr", "KQLeaveRulesAdd:Add");
            searchConditionItem.setRules("required|string");
            searchConditionItem.setViewAttr(3);
            if (hrmFieldBean.getViewAttr() == 1) {
                Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
                OtherParamsMap.put("hasBorder", true);
                searchConditionItem.setOtherParams(OtherParamsMap);
            }
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("minimumUnit");//最小请假单位
            hrmFieldBean.setFieldlabel("388884");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            optionsList = new ArrayList<SearchConditionOption>();
            optionsList.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(388885, user.getLanguage()), minimumUnit == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(388886, user.getLanguage()), minimumUnit == 2));
            optionsList.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(388887, user.getLanguage()), minimumUnit == 3));
            optionsList.add(new SearchConditionOption("4", SystemEnv.getHtmlLabelName(389680, user.getLanguage()), minimumUnit == 4));
            optionsList.add(new SearchConditionOption("5", SystemEnv.getHtmlLabelName(514712, user.getLanguage()), minimumUnit == 5));
            optionsList.add(new SearchConditionOption("6", SystemEnv.getHtmlLabelName(514786, user.getLanguage()), minimumUnit == 6));
          searchConditionItem.setOptions(optionsList);
            if (hrmFieldBean.getViewAttr() == 1) {
                Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
                OtherParamsMap.put("hasBorder", true);
                searchConditionItem.setOtherParams(OtherParamsMap);
            }
            itemList.add(searchConditionItem);

            //时长折算方式
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("conversion");
            hrmFieldBean.setFieldlabel("530092");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            optionsList = new ArrayList<SearchConditionOption>();
            optionsList.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(389654, user.getLanguage()), conversion == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(508424, user.getLanguage()), conversion == 2));
            optionsList.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(508425, user.getLanguage()), conversion == 3));
            searchConditionItem.setOptions(optionsList);
            itemList.add(searchConditionItem);

            if (!ruleId.equals("")) {
                hrmFieldBean = new HrmFieldBean();
                hrmFieldBean.setFieldname("proportion");//换算比例
                hrmFieldBean.setFieldlabel("390754");
                hrmFieldBean.setFieldhtmltype("1");
                hrmFieldBean.setType("2");
                hrmFieldBean.setFieldvalue(proportion);
                hrmFieldBean.setIsFormField(true);
                hrmFieldBean.setViewAttr(3);
                searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
                searchConditionItem.setRules("required|numeric");
                searchConditionItem.setPrecision(2);
                searchConditionItem.setMin("1");
                if (hrmFieldBean.getViewAttr() == 1) {
                    Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
                    OtherParamsMap.put("hasBorder", true);
                    searchConditionItem.setOtherParams(OtherParamsMap);
                }
                itemList.add(searchConditionItem);
            }

            List<String> minimumUnitTips = new ArrayList<String>();
            minimumUnitTips.add(SystemEnv.getHtmlLabelName(390372, user.getLanguage()));
            minimumUnitTips.add(SystemEnv.getHtmlLabelName(389733, user.getLanguage()));
            minimumUnitTips.add(SystemEnv.getHtmlLabelName(389734, user.getLanguage()));
            minimumUnitTips.add(SystemEnv.getHtmlLabelName(389732, user.getLanguage()));
            minimumUnitTips.add(SystemEnv.getHtmlLabelName(389734, user.getLanguage()));
            minimumUnitTips.add(SystemEnv.getHtmlLabelName(389734, user.getLanguage()));
            resultMap.put("minimumUnit", minimumUnitTips);

            //      按半小时请假联动说明：
            List<String> conversionTips = new ArrayList<String>();
            conversionTips.add(SystemEnv.getHtmlLabelName(530011, user.getLanguage()));
            conversionTips.add(SystemEnv.getHtmlLabelName(530068, user.getLanguage()));
            conversionTips.add(SystemEnv.getHtmlLabelName(530069, user.getLanguage()));
            resultMap.put("conversion5", conversionTips);

            //      按整小时请假联动说明：
            conversionTips = new ArrayList<String>();
            conversionTips.add(SystemEnv.getHtmlLabelName(530065, user.getLanguage()));
            conversionTips.add(SystemEnv.getHtmlLabelName(530066, user.getLanguage()));
            conversionTips.add(SystemEnv.getHtmlLabelName(530067, user.getLanguage()));
            resultMap.put("conversion6", conversionTips);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("timeselection");//时间选择方式
            hrmFieldBean.setFieldlabel("513304");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(2);
            hrmFieldBean.setFieldvalue(timeselection+"");
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            optionsList = new ArrayList<SearchConditionOption>();
            optionsList.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(513301, user.getLanguage()), timeselection == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(513302, user.getLanguage()), timeselection == 2));
            searchConditionItem.setOptions(optionsList);
            if (hrmFieldBean.getViewAttr() == 1) {
              Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
              OtherParamsMap.put("hasBorder", true);
              searchConditionItem.setOtherParams(OtherParamsMap);
            }
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("computingMode");//计算请假时长方式
            hrmFieldBean.setFieldlabel("388888");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            optionsList = new ArrayList<SearchConditionOption>();
            optionsList.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(388889, user.getLanguage()), computingMode == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(388890, user.getLanguage()), computingMode == 2));
            searchConditionItem.setOptions(optionsList);
            if (hrmFieldBean.getViewAttr() == 1) {
                Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
                OtherParamsMap.put("hasBorder", true);
                searchConditionItem.setOtherParams(OtherParamsMap);
            }
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("hoursToDay");//日折算时长
            hrmFieldBean.setFieldlabel("388907");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("2");
            hrmFieldBean.setFieldvalue(hoursToDay);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|numeric");
            searchConditionItem.setPrecision(2);
            searchConditionItem.setMin("0");
            if (hrmFieldBean.getViewAttr() == 1) {
                Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
                OtherParamsMap.put("hasBorder", true);
                searchConditionItem.setOtherParams(OtherParamsMap);
            }
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("filterHolidays");/*按自然日计算请假时长，0-不排除，1-排除节假日，2-排除休息日，3-排除节假日和休息日*/
            hrmFieldBean.setFieldlabel("126833");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            if(filterHolidays.equals("0")){
                filterHolidays = "";
            }
            hrmFieldBean.setFieldvalue(filterHolidays);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            List statusOptions = new ArrayList<SearchConditionOption>();
            statusOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(28386, user.getLanguage())));
            statusOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(458, user.getLanguage())));
            searchConditionItem.setOptions(statusOptions);
            searchConditionItem.setDetailtype(2);
            itemList.add(searchConditionItem);

            /*如果之前开了假期余额并且新建了假期规则，在删除该假期类型下的所有假期规则之前，不允许关闭 启用假期余额*/
            boolean canCloseBalanceEnable = true;
            if (!ruleId.equals("")) {
                String sql = "select * from kq_LeaveRulesDetail where (isDelete is null or isDelete<>1) and ruleId=?";
                RecordSet recordSet = new RecordSet();
                recordSet.executeQuery(sql, ruleId);
                if (recordSet.next()) {
                    canCloseBalanceEnable = false;
                }
            }
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("balanceEnable");//启用假期余额
            hrmFieldBean.setFieldlabel("388945");
            hrmFieldBean.setFieldhtmltype("4");
            hrmFieldBean.setType("1");
            hrmFieldBean.setFieldvalue("" + balanceEnable);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(isEnable == 0 || (balanceEnable == 1 && !canCloseBalanceEnable) ? 1 : 3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            Map<String, Object> otherParamsMap = new HashMap<String, Object>();
            otherParamsMap.put("display", "switch");
            searchConditionItem.setOtherParams(otherParamsMap);
            searchConditionItem.setHelpfulTip(SystemEnv.getHtmlLabelName(505663, user.getLanguage()));
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("showOrder");//显示顺序
            hrmFieldBean.setFieldlabel("15513");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("2");
            hrmFieldBean.setFieldvalue(showOrder);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|numeric");
            searchConditionItem.setPrecision(2);
            if (hrmFieldBean.getViewAttr() == 1) {
                Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
                OtherParamsMap.put("hasBorder", true);
                searchConditionItem.setOtherParams(OtherParamsMap);
            }
            itemList.add(searchConditionItem);

            groupItem.put("items", itemList);
            groupList.add(groupItem);
            resultMap.put("condition", groupList);
            resultMap.put("isEnable", isEnable == 1);
        } catch (Exception e) {
            writeLog(e);
        }
        return resultMap;
    }

    private double getMaxShowOrder() {
        double maxShowOrder = 0;
        String sql = "select max(showOrder) maxShowOrder from kq_leaveRules where 1=1 and (isDelete is null or isDelete<>1) ";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        if (recordSet.next()) {
            maxShowOrder = Util.getDoubleValue(recordSet.getString("maxShowOrder"), 0) + 1;
        }
        return maxShowOrder;
    }
}
