package com.engine.kq.cmd.travelrules;

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;

/**
 * 出差规则--获取表单
 */
public class GetTravelRulesFormCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetTravelRulesFormCmd(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 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;
            }

            /*出差规则 start*/
            String ruleId = "1";//暂为备用字段，规则ID，固定为1
            String name = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20084,weaver.general.ThreadVarLanguage.getLang())+"";//暂为备用字段，规则名称，目前固定为出差
            int minimumUnit = 1;//最小出差单位：1-按天出差、2-按半天出差、3-按小时出差、4-按整天出差
            double proportion = 8.00;//换算比例
            int computingMode = 1;//计算公出时长方式：1-按工作日计算出差时长、2-按自然日计算出差时长
            double hoursToDay = 8.00;//日折算时长

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

            String sql = "select * from kq_travelRules where (isDelete is null or isDelete != 1) and id=" + ruleId;
            RecordSet recordSet = new RecordSet();
            recordSet.executeQuery(sql);
            if (recordSet.next()) {
                minimumUnit = recordSet.getInt("minimumUnit");
                computingMode = recordSet.getInt("computingMode");
                hoursToDay = Util.getDoubleValue(recordSet.getString("hoursToday"),8.00);
                travel_timeselection = recordSet.getInt("timeselection");
                travel_timeselection = travel_timeselection < 1 ? 1 : travel_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("minimumUnit");//最小出差单位
            hrmFieldBean.setFieldlabel("389789");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            List<SearchConditionOption> optionsList = new ArrayList<SearchConditionOption>();
            optionsList.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(389899, user.getLanguage()), minimumUnit == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(389900, user.getLanguage()), minimumUnit == 2));
            optionsList.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(389901, user.getLanguage()), minimumUnit == 3));
            optionsList.add(new SearchConditionOption("4", SystemEnv.getHtmlLabelName(389902, user.getLanguage()), minimumUnit == 4));
            searchConditionItem.setOptions(optionsList);
            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()));
            resultMap.put("minimumUnit", minimumUnitTips);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("proportion");//换算比例
            hrmFieldBean.setFieldlabel("390754");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("2");
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            hrmFieldBean.setFieldvalue(proportion);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|numeric");
            searchConditionItem.setPrecision(2);
            searchConditionItem.setMin("1");
            itemList.add(searchConditionItem);
            resultMap.put("proportion_Tips",SystemEnv.getHtmlLabelName(500947, user.getLanguage()));

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("travel_timeselection");//时间选择方式
            hrmFieldBean.setFieldlabel("513304");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(2);
            hrmFieldBean.setFieldvalue(travel_timeselection+"");
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            optionsList = new ArrayList<SearchConditionOption>();
            optionsList.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(513301, user.getLanguage()), travel_timeselection == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(513302, user.getLanguage()), travel_timeselection == 2));
            searchConditionItem.setOptions(optionsList);
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("computingMode");//计算出差时长方式
            hrmFieldBean.setFieldlabel("389790");
            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(389903, user.getLanguage()), computingMode == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(389904, user.getLanguage()), computingMode == 2));
            searchConditionItem.setOptions(optionsList);
            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);//保留两位小数
            itemList.add(searchConditionItem);
            resultMap.put("hoursToDay_Tips",SystemEnv.getHtmlLabelName(506369, user.getLanguage()));

            groupItem.put("title", SystemEnv.getHtmlLabelName(389801, user.getLanguage()));//出差规则
            groupItem.put("defaultshow",true);
            groupItem.put("items", itemList);
            groupList.add(groupItem);
            /*出差规则 end*/

            /*公出规则 start*/
            String exit_ruleId = "1";//暂为备用字段，规则ID，固定为1
            String exit_name = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(24058,weaver.general.ThreadVarLanguage.getLang())+"";//暂为备用字段，规则名称，目前固定为公出
            int exit_minimumUnit = 1;//最小公出单位：1-按天公出、2-按半天公出、3-按小时公出、4-按整天公出
            double exit_proportion = 8.00;//换算比例
            int exit_computingMode = 1;//计算公出时长方式：1-按工作日计算公出时长、2-按自然日计算公出时长
            double exit_hoursToDay = 8.00;//日折算时长

            sql = "select * from kq_exitRules where (isDelete is null or isDelete != 1) and id=" + exit_ruleId;
            recordSet.executeQuery(sql);
            if (recordSet.next()) {
                exit_minimumUnit = recordSet.getInt("minimumUnit");
                exit_proportion = Util.getDoubleValue(recordSet.getString("proportion"),8.00);
                exit_computingMode = recordSet.getInt("computingMode");
                exit_hoursToDay = Util.getDoubleValue(recordSet.getString("hoursToday"),8.00);
                exit_timeselection = recordSet.getInt("timeselection");
                exit_timeselection = exit_timeselection < 1 ? 1 : exit_timeselection;
            }

            groupItem = new HashMap<String, Object>();
            itemList = new ArrayList<Object>();
            hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
            searchConditionItem = null;
            hrmFieldBean = null;

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("exit_minimumUnit");//最小出差单位
            hrmFieldBean.setFieldlabel("389787");
            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(389905, user.getLanguage()), exit_minimumUnit == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(389906, user.getLanguage()), exit_minimumUnit == 2));
            optionsList.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(389907, user.getLanguage()), exit_minimumUnit == 3));
            optionsList.add(new SearchConditionOption("4", SystemEnv.getHtmlLabelName(389908, user.getLanguage()), exit_minimumUnit == 4));
            searchConditionItem.setOptions(optionsList);
            itemList.add(searchConditionItem);

            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()));
            resultMap.put("exit_minimumUnit", minimumUnitTips);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("exit_timeselection");//时间选择方式
            hrmFieldBean.setFieldlabel("513304");
            hrmFieldBean.setFieldhtmltype("5");
            hrmFieldBean.setType("1");
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(2);
            hrmFieldBean.setFieldvalue(exit_timeselection+"");
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            optionsList = new ArrayList<SearchConditionOption>();
            optionsList.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(513301, user.getLanguage()), exit_timeselection == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(513302, user.getLanguage()), exit_timeselection == 2));
            searchConditionItem.setOptions(optionsList);
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("exit_proportion");//换算比例
            hrmFieldBean.setFieldlabel("390754");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("2");
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            hrmFieldBean.setFieldvalue(proportion);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|numeric");
            searchConditionItem.setPrecision(2);
            searchConditionItem.setMin("1");
            itemList.add(searchConditionItem);
            resultMap.put("exit_proportion_Tips",SystemEnv.getHtmlLabelName(500949, user.getLanguage()));

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("exit_computingMode");//计算公出时长方式
            hrmFieldBean.setFieldlabel("389788");
            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(389909, user.getLanguage()), exit_computingMode == 1));
            optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(389910, user.getLanguage()), exit_computingMode == 2));
            searchConditionItem.setOptions(optionsList);
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("exit_hoursToDay");//日折算时长
            hrmFieldBean.setFieldlabel("388907");
            hrmFieldBean.setFieldhtmltype("1");
            hrmFieldBean.setType("2");
            hrmFieldBean.setFieldvalue(exit_hoursToDay);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|numeric");
            searchConditionItem.setPrecision(2);//保留两位有效数字
            itemList.add(searchConditionItem);
            resultMap.put("exit_hoursToDay_Tips",SystemEnv.getHtmlLabelName(506368, user.getLanguage()));

            groupItem.put("title", SystemEnv.getHtmlLabelName(389808, user.getLanguage()));//公出规则
            groupItem.put("defaultshow",true);
            groupItem.put("items", itemList);
            groupList.add(groupItem);
            /*公出规则 end*/

            resultMap.put("condition", groupList);
        }catch (Exception e){
            writeLog(e);
        }
        return resultMap;
    }
}
