package com.engine.kq.cmd.setupwizard;

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

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

        boolean canInit = HrmUserVarify.checkUserRight("KQ:SetupWizard", user);//是否具有初始化的权限
        if (!canInit) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));//没有权限
            return resultMap;
        }

        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("workTime");//上班时间
        hrmFieldBean.setFieldlabel("125809");
        hrmFieldBean.setFieldhtmltype("3");
        hrmFieldBean.setType("19");
        hrmFieldBean.setIsFormField(true);
        hrmFieldBean.setViewAttr(3);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        searchConditionItem.setRules("required|string");
        itemList.add(searchConditionItem);

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("restTime");//下班时间
        hrmFieldBean.setFieldlabel("125810");
        hrmFieldBean.setFieldhtmltype("3");
        hrmFieldBean.setType("19");
        hrmFieldBean.setIsFormField(true);
        hrmFieldBean.setViewAttr(3);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        searchConditionItem.setRules("required|string");
        itemList.add(searchConditionItem);

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("excludeRestTime");//排除休息时间
        hrmFieldBean.setFieldlabel("388565");
        hrmFieldBean.setFieldhtmltype("4");
        hrmFieldBean.setType("1");
        hrmFieldBean.setIsFormField(true);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        Map<String, Object> otherParamsMap = new HashMap<String, Object>();
        otherParamsMap.put("display", "switch");
        searchConditionItem.setOtherParams(otherParamsMap);
        itemList.add(searchConditionItem);

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("restTime");//休息时间
        hrmFieldBean.setFieldlabel("388417");
        hrmFieldBean.setFieldhtmltype("3");
        hrmFieldBean.setType("TIMERANGEPICKER");
        hrmFieldBean.setIsFormField(true);
        hrmFieldBean.setViewAttr(3);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        searchConditionItem.setRules("required|string");
        itemList.add(searchConditionItem);

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("workDate");//工作日期
        hrmFieldBean.setFieldlabel("16221");
        hrmFieldBean.setFieldhtmltype("5");
        hrmFieldBean.setType("1");
        hrmFieldBean.setIsFormField(true);
        hrmFieldBean.setFieldvalue("0,1,2,3,4");
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        List statusOptions = new ArrayList<SearchConditionOption>();
        statusOptions.add(new SearchConditionOption("0", SystemEnv.getHtmlLabelName(392, user.getLanguage())));
        statusOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(393, user.getLanguage())));
        statusOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(394, user.getLanguage())));
        statusOptions.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(395, user.getLanguage())));
        statusOptions.add(new SearchConditionOption("4", SystemEnv.getHtmlLabelName(396, user.getLanguage())));
        statusOptions.add(new SearchConditionOption("5", SystemEnv.getHtmlLabelName(397, user.getLanguage())));
        statusOptions.add(new SearchConditionOption("6", SystemEnv.getHtmlLabelName(398, user.getLanguage())));
        searchConditionItem.setOptions(statusOptions);
        searchConditionItem.setDetailtype(2);
        itemList.add(searchConditionItem);

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

        Map<String,Object> dataMap = new HashMap<String,Object>();
        List<Map<String,Object>> datasList = new ArrayList<Map<String,Object>>();
        List<String> descList = new ArrayList<String>();

        dataMap = new HashMap<String,Object>();
        dataMap.put("key","1");
        dataMap.put("title",SystemEnv.getHtmlLabelName(24803, user.getLanguage()));//班次
        descList = new ArrayList<String>();
        //本次初始化将新建一个【班次】，所新建的班次的上班时间、下班时间、休息时段均取自本页面的上班时间、下班时间、休息时段；若此前已经初始化过，则根据本次设置的上班时间、下班时间、休息时段更新此前新建过的【班次】
        descList.add(SystemEnv.getHtmlLabelName(505627, user.getLanguage()));
        dataMap.put("desc",descList);
        datasList.add(dataMap);

        dataMap = new HashMap<String,Object>();
        dataMap.put("key","2");
        dataMap.put("title",SystemEnv.getHtmlLabelName(390221, user.getLanguage()));//考勤组
        descList = new ArrayList<String>();
        // 本次初始化将新建一个【考勤组】，所新建的考勤组的考勤类型为固定班制(即每周的工作日固定，且每一个工作日的工作时间也固定)
        // 工作日的设置取自本页面所勾选的工作日期，每个工作日的工作时间取自上一步的【班次】
        // 考勤组成员默认取全公司所有人(即默认设置所有人按照此考勤组的工作日以及工作时间进行考勤)
        // 若此前已经初始化过，将根据本次的设置更新此前新建的【考勤组】
        descList.add(SystemEnv.getHtmlLabelName(505629, user.getLanguage()));
        dataMap.put("desc",descList);
        datasList.add(dataMap);

        dataMap = new HashMap<String,Object>();
        dataMap.put("key","3");
        dataMap.put("title",SystemEnv.getHtmlLabelName(28386, user.getLanguage()));//节假日
        descList = new ArrayList<String>();
        //本次初始化将根据国家规定，自动为上一步新建的【考勤组】设置本年度的公众假日、调配工作日和调配休息日
        descList.add(SystemEnv.getHtmlLabelName(505630, user.getLanguage()));
        //注：此功能需服务器能够链接外网，否则获取不到网络数据，无法自动生成节假日数据
        descList.add(SystemEnv.getHtmlLabelName(505631, user.getLanguage()));
        dataMap.put("desc",descList);
        datasList.add(dataMap);

        dataMap = new HashMap<String,Object>();
        dataMap.put("key","4");
        dataMap.put("title",SystemEnv.getHtmlLabelName(500272, user.getLanguage()));//考勤流程
        descList = new ArrayList<String>();
        //本次初始化将新建如下几条考勤流程：请假流程、出差流程、公出流程、加班流程、排班流程、补卡流程、销假流程、考勤变更流程，并且会自动完成与考勤的字段对应(如若没有将流程与考勤字段正确对应，则无法将流程数据统计为考勤数据)
        descList.add(SystemEnv.getHtmlLabelName(514749, user.getLanguage()));
        //注：因每个公司的流程流转节点以及流程的节假操作者各有不同，所以初始化无法为考勤流程设置好节点以及节点操作者，如若想正常启用考勤流程，还需要设置好考勤流程的考勤节点以及节点操作者
        descList.add(SystemEnv.getHtmlLabelName(505633, user.getLanguage()));
        dataMap.put("desc",descList);
        datasList.add(dataMap);

        dataMap = new HashMap<String,Object>();
        dataMap.put("key","5");
        dataMap.put("title",SystemEnv.getHtmlLabelName(388881, user.getLanguage()));//加班规则
        descList = new ArrayList<String>();
        //本次初始化将新建一个【加班规则】，并关联本次初始化所新建的【考勤组】(即考勤内的成员会按照此加班规则的规则内容生成加班调休数据)。具体规则如下所示：
        descList.add(SystemEnv.getHtmlLabelName(505634, user.getLanguage()));
        //允许工作日加班，但是需审批，以加班流程为准，下班后30分钟开始计算加班，最小加班时长为30分钟，允许加班转调休，加班时长与调休时长的比例为1:1
        descList.add(SystemEnv.getHtmlLabelName(505635, user.getLanguage()));
        //允许休息日加班，但是需审批，以加班流程为准，最小加班时长为30分钟，允许加班转调休，加班时长与调休时长的比例为1:1
        descList.add(SystemEnv.getHtmlLabelName(505636, user.getLanguage()));
        //允许节假日加班，但是需审批，以加班流程为准，最小加班时长为30分钟，允许加班转调休，加班时长与调休时长的比例为1:1
        descList.add(SystemEnv.getHtmlLabelName(505637, user.getLanguage()));
        dataMap.put("desc",descList);
        datasList.add(dataMap);

        dataMap = new HashMap<String,Object>();
        dataMap.put("key","6");
        dataMap.put("title",SystemEnv.getHtmlLabelName(390004, user.getLanguage()));//公出出差规则
        descList = new ArrayList<String>();
        //本次初始化将更新出差规则为：按天出差，按工作日计算出差时长
        descList.add(SystemEnv.getHtmlLabelName(505638, user.getLanguage()));
        //本次初始化将更新公出规则为：按天公出，按工作日计算公出规则
        descList.add(SystemEnv.getHtmlLabelName(505639, user.getLanguage()));
        dataMap.put("desc",descList);
        datasList.add(dataMap);

        dataMap = new HashMap<String,Object>();
        dataMap.put("key","7");
        dataMap.put("title",SystemEnv.getHtmlLabelName(388996, user.getLanguage()));//假期规则
        descList = new ArrayList<String>();
        //年假-初始化：按照国家规定职工工龄已满1年不满10年的，年休假5天；已满10年不满20年的，年休假10天；已满20年的，年休假15天
        descList.add(SystemEnv.getHtmlLabelName(505640, user.getLanguage()));
        //带薪事假-初始化：不限制余额，按天请假，按照工作日计算请假时长
        descList.add(SystemEnv.getHtmlLabelName(505641, user.getLanguage()));
        //带薪病假-初始化：不限制余额，按天请假，按照工作日计算请假时长
        descList.add(SystemEnv.getHtmlLabelName(505642, user.getLanguage()));
        //调休-初始化：根据加班时长自动累加
        descList.add(SystemEnv.getHtmlLabelName(505643, user.getLanguage()));
        //事假-初始化：不限制余额，按天请假，按照工作日计算请假时长
        descList.add(SystemEnv.getHtmlLabelName(505644, user.getLanguage()));
        //病假-初始化：不限制余额，按天请假，按照工作日计算请假时长
        descList.add(SystemEnv.getHtmlLabelName(505645, user.getLanguage()));
        //产假-初始化：不限制余额，按天请假，按照自然日计算请假时长
        descList.add(SystemEnv.getHtmlLabelName(505646, user.getLanguage()));
        //陪产假-初始化：不限制余额，按天请假，按照自然日计算请假时长
        descList.add(SystemEnv.getHtmlLabelName(505647, user.getLanguage()));
        //婚假-初始化：不限制余额，按天请假，按照工作日计算请假时长
        descList.add(SystemEnv.getHtmlLabelName(505648, user.getLanguage()));
        //丧假-初始化：不限制余额，按天请假，按照工作日计算请假时长
        descList.add(SystemEnv.getHtmlLabelName(505649, user.getLanguage()));
        //哺乳假-初始化：不限制余额，按天请假，按照工作日计算请假时长
        descList.add(SystemEnv.getHtmlLabelName(505650, user.getLanguage()));
        dataMap.put("desc",descList);
        datasList.add(dataMap);

        dataMap = new HashMap<String,Object>();
        dataMap.put("key","8");
        dataMap.put("title",SystemEnv.getHtmlLabelName(390297, user.getLanguage()));//假期余额
        descList = new ArrayList<String>();
        //根据年假-初始化的假期规则为全公司的人批量生成的本年度的假期余额数据
        descList.add(SystemEnv.getHtmlLabelName(505651, user.getLanguage()));
        dataMap.put("desc",descList);
        datasList.add(dataMap);

        resultMap.put("datas",datasList);
        return resultMap;
    }
}
