package com.engine.kq.cmd.holidaySet;

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 com.engine.kq.biz.KQGroupComInfo;
import weaver.conn.RecordSet;
import weaver.filter.XssUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.country.CountryComInfo;
import weaver.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.systeminfo.systemright.CheckSubCompanyRight;

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

/**
 * 节假日设置--新建或编辑的表单
 */
public class GetHolidaySetFormCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetHolidaySetFormCmd(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>();
        String id = Util.null2String(params.get("id"));//ID
        String groupId = Util.null2String(params.get("groupId"));//考勤组的ID
        String holidayDate = Util.null2String(params.get("holidayDate"));//节假日日期
        String holidayDesc = "";//说明
        String changeType = "1";//类型：1-公众假日、2-调配工作日、3-调配休息日
        String relatedDay = "";//对应工作日：0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六、6-星期日
        String shiftId = "";//班次ID

        /*参数有误*/
        if (id.equals("") && groupId.equals("")) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(388858, user.getLanguage()));
            return resultMap;
        }

        /*是否具有编辑的权限*/
        boolean canEdit = HrmUserVarify.checkUserRight("KQHolidaySetEdit:Edit", user);
        if (!canEdit) {
            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;
        List<Map<String, Object>> replaceDatas = new ArrayList<Map<String, Object>>();
        Map<String, Object> datas = new HashMap<String, Object>();

        boolean isEdit = false;
        if (!id.equals("")) {
            String sql = "select * from kq_HolidaySet where id=" + id;
            RecordSet recordSet = new RecordSet();
            recordSet.executeQuery(sql);
            if (recordSet.next()) {
                isEdit = true;
                holidayDate = recordSet.getString("holidayDate");
                groupId = recordSet.getString("groupId");
                holidayDesc = recordSet.getString("holidayDesc");
                changeType = recordSet.getString("changeType");
                relatedDay = recordSet.getString("relatedDay");
                shiftId = recordSet.getString("shiftId");
            }
        }

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("groupId");//考勤组
        hrmFieldBean.setFieldlabel("390221");
        hrmFieldBean.setFieldhtmltype("3");
        hrmFieldBean.setType("kqgroup");
        hrmFieldBean.setIsFormField(true);
        hrmFieldBean.setViewAttr(1);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        KQGroupComInfo kqGroupComInfo = new KQGroupComInfo();
        replaceDatas = new ArrayList<Map<String, Object>>();
        datas = new HashMap<String, Object>();
        datas.put("id", groupId);
        datas.put("name", kqGroupComInfo.getGroupname(groupId));
        replaceDatas.add(datas);
        searchConditionItem.getBrowserConditionParam().setReplaceDatas(replaceDatas);
        Map<String, Object> OtherParamsMap = new HashMap<String, Object>();
        OtherParamsMap.put("hasBorder", true);
        searchConditionItem.setOtherParams(OtherParamsMap);
        searchConditionItem.setRules("required|string");
        itemList.add(searchConditionItem);

        if (isEdit) {
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("holidayDate");//日期
            hrmFieldBean.setFieldlabel("97");
            hrmFieldBean.setFieldhtmltype("3");
            hrmFieldBean.setType("2");
            hrmFieldBean.setFieldvalue(holidayDate);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(1);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            OtherParamsMap = new HashMap<String, Object>();
            OtherParamsMap.put("hasBorder", true);
            searchConditionItem.setOtherParams(OtherParamsMap);
            searchConditionItem.setRules("required|string");
            itemList.add(searchConditionItem);
        } else {
            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("startDate");//开始日期
            hrmFieldBean.setFieldlabel("740");
            hrmFieldBean.setFieldhtmltype("3");
            hrmFieldBean.setType("2");
            hrmFieldBean.setFieldvalue(holidayDate);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|string");
            itemList.add(searchConditionItem);

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname("endDate");//结束日期
            hrmFieldBean.setFieldlabel("741");
            hrmFieldBean.setFieldhtmltype("3");
            hrmFieldBean.setType("2");
            hrmFieldBean.setFieldvalue(holidayDate);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(3);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            searchConditionItem.setRules("required|string");
            itemList.add(searchConditionItem);
        }

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("holidayDesc");//说明
        hrmFieldBean.setFieldlabel("85");
        hrmFieldBean.setFieldhtmltype("1");
        hrmFieldBean.setType("1");
        hrmFieldBean.setFieldvalue(holidayDesc);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        if (hrmFieldBean.getViewAttr() == 1) {
            OtherParamsMap = new HashMap<String, Object>();
            OtherParamsMap.put("hasBorder", true);
            searchConditionItem.setOtherParams(OtherParamsMap);
        }
        itemList.add(searchConditionItem);

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("changeType");//类型
        hrmFieldBean.setFieldlabel("132033");
        hrmFieldBean.setFieldhtmltype("5");
        hrmFieldBean.setType("1");
        hrmFieldBean.setFieldvalue(changeType);
        hrmFieldBean.setIsFormField(true);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        List statusOptions = new ArrayList<SearchConditionOption>();
        statusOptions.add(new SearchConditionOption("1", SystemEnv.getHtmlLabelName(16478, user.getLanguage()), changeType.equals("1")));
        statusOptions.add(new SearchConditionOption("3", SystemEnv.getHtmlLabelName(16752, user.getLanguage()), changeType.equals("3")));
        if (!Util.null2String(kqGroupComInfo.getKqtype(groupId)).equals("2")) {
            statusOptions.add(new SearchConditionOption("2", SystemEnv.getHtmlLabelName(16751, user.getLanguage()), changeType.equals("2")));
        }
        searchConditionItem.setOptions(statusOptions);
        searchConditionItem.setHelpfulTip(SystemEnv.getHtmlLabelName(529901, user.getLanguage()));
        searchConditionItem.setDetailtype(3);
        itemList.add(searchConditionItem);

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

        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("isSync");//同步到其他考勤组
        hrmFieldBean.setFieldlabel("506392");
        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);

//        XssUtil xssUtil = new XssUtil();
        hrmFieldBean = new HrmFieldBean();
        hrmFieldBean.setFieldname("syncGroupIds");//同步到考勤组--多选
        hrmFieldBean.setFieldlabel("390664");
        hrmFieldBean.setFieldhtmltype("3");
        hrmFieldBean.setType("mkqgroup");
        hrmFieldBean.setIsFormField(true);
        hrmFieldBean.setViewAttr(3);
        searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
        searchConditionItem.setRules("required|string");
        Map<String,Object> dataMap = new HashMap<String,Object>();
//        dataMap.put("sqlWhere",xssUtil.put(" kqtype!=2 "));
        searchConditionItem.getBrowserConditionParam().setDataParams(dataMap);
        itemList.add(searchConditionItem);

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