package com.engine.kq.cmd.balanceofleave;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.util.ConditionFactory;
import com.api.browser.util.ConditionType;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.util.ImportProcessUtil;
import weaver.general.GCONST;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 假期余额--获取导入的表单
 */
public class GetImportFormCmd extends AbstractCommonCommand<Map<String, Object>> {

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

        /*假期规则的ID*/
        String ruleId = Util.null2String(params.get("ruleId"));
        /*所属机构类型：0-总部、1-分部、2-部门*/
        int organizationType = Util.getIntValue((String) params.get("organizationType"), 0);
        /*所属机构的ID（机构类型为分部时为分部ID）*/
        String organizationId = Util.null2String(params.get("organizationId"));
        /*搜索条件中的年份*/
        String searchYearSelect = Util.null2String(params.get("searchYearSelect"));//日期控件的选择值
        String searchYear = Util.null2String(params.get("searchYear"));//年份
        if (searchYearSelect.equals("5") || searchYearSelect.equals("8")) {
            searchYear = TimeUtil.getDateByOption(searchYearSelect, "0").substring(0, 4);
        }
        /*搜索条件中的人员姓名*/
        String hrmResourceId = Util.null2String(params.get("hrmResourceId"));
        //机构ID以及所有下级机构的ID
        String organizationIds = "";
        try {
            if (organizationType == 1) {
                organizationIds = SubCompanyComInfo.getAllChildSubcompanyId(organizationId, organizationIds);
            } else if (organizationType == 2) {
                organizationIds = DepartmentComInfo.getAllChildDepartId(organizationId, organizationIds);
            }
            if (organizationIds.equals("")) {
                organizationIds = organizationId;
            } else {
                organizationIds = organizationId + organizationIds.trim();
            }
            if (!hrmResourceId.equals("")) {
                organizationType = 3;
                organizationIds = hrmResourceId;
            }

            ManageDetachComInfo manageDetachComInfo = new ManageDetachComInfo();
            //是否开启了人力资源模块的管理分权
            boolean isUseHrmManageDetach = manageDetachComInfo.isUseHrmManageDetach();
            //如果开启了分权
            if (isUseHrmManageDetach) {
                SubCompanyComInfo subCompanyComInfo = new SubCompanyComInfo();
                String allRightSubComIds = subCompanyComInfo.getRightSubCompany(user.getUID(), "KQLeaveRulesEdit:Edit", 1);
                if (organizationType == 0) {
					organizationType = 1;
                    organizationIds = allRightSubComIds;
                } else if (organizationType == 1) {
                    if (("," + allRightSubComIds + ",").contains("," + organizationId + ",")) {
                        organizationIds = organizationId;
                    } else {
                        organizationIds = allRightSubComIds;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*生成模板文件*/
        ImportProcessUtil importProcessUtil = new ImportProcessUtil();
        importProcessUtil.createTempletOfLeave(ruleId, searchYear, "" + organizationType, organizationIds);

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

        //下载模板文件
        ConditionFactory conditionFactory = new ConditionFactory(user);
        searchConditionItem = conditionFactory.createCondition(ConditionType.INPUT, 389495, "templet");
        searchConditionItem.setValue("/hrm/import/templet/KQBalanceOfLeave.xls");
        itemList.add(searchConditionItem);

        //Excel文件
        searchConditionItem = conditionFactory.createCondition(ConditionType.RESOURCEIMG, 16630, "excelfile");
        itemList.add(searchConditionItem);

        groupItem.put("title", SystemEnv.getHtmlLabelName(384128, user.getLanguage()));//基本信息
        groupItem.put("defaultshow", true);
        groupItem.put("items", itemList);
        groupList.add(groupItem);

        Map<String, Object> itemMap = null;
        itemList = new ArrayList<Object>();
        groupItem = new HashMap<String, Object>();

        /**请先下载模板*/
        itemMap = new HashMap<String, Object>();
        itemMap.put("index", 0);
        itemMap.put("value", SystemEnv.getHtmlLabelName(383083, user.getLanguage()));
        itemMap.put("link", "/hrm/import/templet/KQBalanceOfLeave.xls");
        itemList.add(itemMap);

        /**导入模板中，列的位置不可改变，也不可增加列或者减少列*/
        itemMap = new HashMap<String, Object>();
        itemMap.put("index", 1);
        itemMap.put("value", SystemEnv.getHtmlLabelName(389749, user.getLanguage()));
        itemList.add(itemMap);

        /**导入模板中，ID列和姓名列的数据属自动生成，且一一对应，不允许更改*/
        itemMap = new HashMap<String, Object>();
        itemMap.put("index", 2);
        itemMap.put("value", SystemEnv.getHtmlLabelName(389750, user.getLanguage()));
        itemList.add(itemMap);

        /**导入模板中，余额数据不填写则视为0*/
        itemMap = new HashMap<String, Object>();
        itemMap.put("index", 3);
        itemMap.put("value", SystemEnv.getHtmlLabelName(389751, user.getLanguage()));
        itemList.add(itemMap);

        /**当假期类型为调休时，导入模板中的数据均为新增*/
        itemMap = new HashMap<String, Object>();
        itemMap.put("index", 4);
        itemMap.put("value", SystemEnv.getHtmlLabelName(509971, user.getLanguage()));
        itemList.add(itemMap);

        /**当假期类型为调休时，模板中的年份格式为yyyy，例如2019*/
        itemMap = new HashMap<String, Object>();
        itemMap.put("index", 5);
        itemMap.put("value", SystemEnv.getHtmlLabelName(509972, user.getLanguage()));
        itemList.add(itemMap);

        /**当假期类型为调休时，模板中的月份格式为MM，例如08*/
        itemMap = new HashMap<String, Object>();
        itemMap.put("index", 6);
        itemMap.put("value", SystemEnv.getHtmlLabelName(509973, user.getLanguage()));
        itemList.add(itemMap);

        /**当假期类型为调休时，模板中的加班日期和失效日期的格式均为yyyy-MM-dd，例如2019-08-01*/
        itemMap = new HashMap<String, Object>();
        itemMap.put("index", 7);
        itemMap.put("value", SystemEnv.getHtmlLabelName(509974, user.getLanguage()));
        itemList.add(itemMap);

        groupItem.put("title", SystemEnv.getHtmlLabelName(33803, user.getLanguage()));
        groupItem.put("defaultshow", true);
        groupItem.put("items", itemList);
        groupList.add(groupItem);

        resultMap.put("condition", groupList);
        resultMap.put("status", "1");
        return resultMap;
    }
}
