package com.engine.kq.cmd.overtimerules;

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 GetOvertimeUnitFormCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetOvertimeUnitFormCmd(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>();

        int id = 1;////暂为备用字段，加班单位ID，固定为1
        int minimumUnit = 1;//最小加班单位：1-按天加班、2-按半天加班、3-按小时加班、4-按整天加班
        int overtimeConversion = 1;//加班时长折算方式：1-四舍五入、2-向上取整、3-向下取整
        double hoursToDay = 8.00;//日折算时长
        //半天单位 时间选择方式：1-下拉框选择 、2-具体时间
        int timeselection = 1;

        /* 加班规则维护权限 */
        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;
        }

        String sql = "select * from kq_OvertimeUnit where (isDelete is null or isDelete !=1) and id="+id;
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        if(recordSet.next()){
            minimumUnit = recordSet.getInt("minimumUnit");
            overtimeConversion = recordSet.getInt("overtimeConversion");
            hoursToDay = recordSet.getDouble("hoursToDay");
            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("minimumUnit");//最小加班单位
        hrmFieldBean.setFieldlabel("390388");
        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(390389, user.getLanguage()), minimumUnit == 1));
        optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(390390, user.getLanguage()), minimumUnit == 2));
        optionsList.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(390391, user.getLanguage()), minimumUnit == 3));
        optionsList.add(new SearchConditionOption("4", SystemEnv.getHtmlLabelName(390392, user.getLanguage()), minimumUnit == 4));
        optionsList.add(new SearchConditionOption("5", SystemEnv.getHtmlLabelName(528529, user.getLanguage()), minimumUnit == 5));
        optionsList.add(new SearchConditionOption("6", SystemEnv.getHtmlLabelName(528530, user.getLanguage()), minimumUnit == 6));
        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()));
        minimumUnitTips.add(SystemEnv.getHtmlLabelName(389734, user.getLanguage()));
        minimumUnitTips.add(SystemEnv.getHtmlLabelName(389734, user.getLanguage()));
        resultMap.put("minimumUnit", minimumUnitTips);

        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);
        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);

        //加班时长折算方式
        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("overtimeConversion");
        hrmFieldBean.setFieldlabel("528531");
        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()), overtimeConversion == 1));
        optionsList.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(508424, user.getLanguage()), overtimeConversion == 2));
        optionsList.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(508425, user.getLanguage()), overtimeConversion == 3));
        searchConditionItem.setOptions(optionsList);
        itemList.add(searchConditionItem);

//      按半小时加班联动说明：
        List<String> overtimeConversionTips = new ArrayList<String>();
        overtimeConversionTips.add(SystemEnv.getHtmlLabelName(528532, user.getLanguage()));
        overtimeConversionTips.add(SystemEnv.getHtmlLabelName(528533, user.getLanguage()));
        overtimeConversionTips.add(SystemEnv.getHtmlLabelName(528534, user.getLanguage()));
        resultMap.put("overtimeConversion5", overtimeConversionTips);

        //      按整小时加班联动说明：
        overtimeConversionTips = new ArrayList<String>();
        overtimeConversionTips.add(SystemEnv.getHtmlLabelName(528535, user.getLanguage()));
        overtimeConversionTips.add(SystemEnv.getHtmlLabelName(528536, user.getLanguage()));
        overtimeConversionTips.add(SystemEnv.getHtmlLabelName(529548, user.getLanguage()));
        resultMap.put("overtimeConversion6", overtimeConversionTips);

        groupItem.put("items", itemList);
        groupList.add(groupItem);
        resultMap.put("condition", groupList);

        return resultMap;
    }
}
