package com.engine.kq.cmd.holidaySet;

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.hrm.location.LocationComInfo;
import weaver.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.systeminfo.systemright.CheckSubCompanyRight;

import java.util.*;

/**
 * 节假日设置--日历
 */
public class GetHolidaySetCalendarCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetHolidaySetCalendarCmd(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>();
        RecordSet recordSet = new RecordSet();
        String groupId = Util.null2String(params.get("groupId"));//考勤组的ID
        String currentYear = Util.null2String(params.get("currentYear"));//年份
        String changeType = Util.null2String(params.get("changeType"));//类型：1-公众假日、2-调配工作日、3-调配休息日

        boolean canAdd = HrmUserVarify.checkUserRight("KQHolidaySetAdd:Add", user);//是否具有新建权限
        if (!canAdd) {
            resultMap.put("status", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
            return resultMap;
        }

        Map<String, Object> itemMap = new HashMap<String, Object>();
        List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
        List<Object> dataList = new ArrayList<Object>();

        /*公众假日*/
        Map<String, Object> holidayMap1 = new HashMap<String, Object>();
        List<Object> holidayList1 = new ArrayList<Object>();

        /*调配工作日*/
        Map<String, Object> holidayMap2 = new HashMap<String, Object>();
        List<Object> holidayList2 = new ArrayList<Object>();

        /*调配休息日*/
        Map<String, Object> holidayMap3 = new HashMap<String, Object>();
        List<Object> holidayList3 = new ArrayList<Object>();

        /*如果没有传入年份，默认为今年的年份*/
        Calendar today = Calendar.getInstance();
        if (currentYear.equals("")) {
            currentYear = "" + today.get(Calendar.YEAR);
        }

        String sql = "select * from kq_HolidaySet where 1=1";
        if (!groupId.equals("")) {
            sql += " and groupId in (" + groupId + ")";
        }
        if (!changeType.equals("")) {
            sql += " and changeType in (" + changeType + ")";
        } else {
            sql += " and 1=2 ";//表示不显示任何节假日数据
        }
        if (!currentYear.equals("")) {
            String dateFrom = currentYear + "-01-01";
            String dateTo = currentYear + "-12-31";
            sql += " and holidayDate>='" + dateFrom + "' and holidayDate<='" + dateTo + "'";
        }
        recordSet.executeQuery(sql);
        while (recordSet.next()) {
            Map<String, Object> holidayMap = new HashMap<String, Object>();
            changeType = recordSet.getString("changeType");//类型：1-公众假日、2-调配工作日、3-调配休息日
            String holidayDate = recordSet.getString("holidayDate");//日期
            String holidayDesc = recordSet.getString("holidayDesc");//说明
            if (changeType.equals("1")) {
                holidayMap.put("fontColor", "#FFF");
                holidayMap.put("bgColor", "#00CC66");//绿色
                holidayMap.put("changeType", "1");//1-公众假日
                holidayMap.put("typeName", getTypeName("1"));
                holidayMap.put("desc", holidayDesc);
                holidayMap.put("id", recordSet.getInt("id"));
                holidayMap.put("date", holidayDate);
                holidayMap1.put(holidayDate, holidayMap);
                holidayList1.add(holidayDate);
            } else if (changeType.equals("2")) {
                holidayMap.put("fontColor", "#FFF");
                holidayMap.put("bgColor", "#FF0000");//红色
                holidayMap.put("changeType", "2");//调配工作日
                holidayMap.put("typeName", getTypeName("2"));
                holidayMap.put("desc", holidayDesc);
                holidayMap.put("id", recordSet.getInt("id"));
                holidayMap.put("date", holidayDate);
                holidayMap2.put(holidayDate, holidayMap);
                holidayList2.add(holidayDate);
            } else if (changeType.equals("3")) {
                holidayMap.put("fontColor", "#FFF");
                holidayMap.put("bgColor", "#0066FF");//蓝色
                holidayMap.put("changeType", "3");//调配休息日
                holidayMap.put("typeName", getTypeName("3"));
                holidayMap.put("desc", holidayDesc);
                holidayMap.put("id", recordSet.getInt("id"));
                holidayMap.put("date", holidayDate);
                holidayMap3.put(holidayDate, holidayMap);
                holidayList3.add(holidayDate);
            }
        }
        Calendar dateTemp = Calendar.getInstance();
        String dateStr = "";
        for (int i = 1; i <= 12; i++) {
            itemList = new ArrayList<Map<String, Object>>();
            for (int j = 1; j <= 31; j++) {
                itemMap = new HashMap<String, Object>();
                dateTemp.clear();
                dateTemp.set(Util.getIntValue(currentYear), i - 1, j);
                if (dateTemp.get(Calendar.MONTH) != i - 1) {//防止出现2月30号等错误日期参数
                    continue;
                }
                dateStr = Util.add0(dateTemp.get(Calendar.YEAR), 4) + "-" +
                        Util.add0(dateTemp.get(Calendar.MONTH) + 1, 2) + "-" +
                        Util.add0(dateTemp.get(Calendar.DAY_OF_MONTH), 2);
                if (holidayList1.contains(dateStr)) {
                    itemMap = (Map<String, Object>) holidayMap1.get(dateStr);
                } else if (holidayList2.contains(dateStr)) {
                    itemMap = (Map<String, Object>) holidayMap2.get(dateStr);
                } else if (holidayList3.contains(dateStr)) {
                    itemMap = (Map<String, Object>) holidayMap3.get(dateStr);
                } else if (dateTemp.getTime().getDay() == 6 || dateTemp.getTime().getDay() == 0) {//周末
                    itemMap.put("date", dateStr);
                    itemMap.put("fontColor", "#C0C0C0");//灰色
                    itemMap.put("bgColor", "");
                    itemMap.put("changeType", "");
                    itemMap.put("typeName", "");
                    itemMap.put("desc", "");
                    itemMap.put("id", "");
                } else {
                    itemMap.put("date", dateStr);
                    itemMap.put("fontColor", "#000000");
                    itemMap.put("bgColor", "");
                    itemMap.put("changeType", "");
                    itemMap.put("typeName", "");
                    itemMap.put("desc", "");
                    itemMap.put("id", "");
                }
                itemList.add(itemMap);
            }
            dataList.add(itemList);
        }
        resultMap.put("data", dataList);
        return resultMap;
    }

    /**
     * 根据类型值获取显示名称
     *
     * @param type
     * @return
     */
    private String getTypeName(String type) {
        String typeName = "";
        if (type.equals("1")) {
            typeName = SystemEnv.getHtmlLabelName(16478, user.getLanguage());//公众假日
        } else if (type.equals("2")) {
            typeName = SystemEnv.getHtmlLabelName(16751, user.getLanguage());//调配工作日
        } else if (type.equals("3")) {
            typeName = SystemEnv.getHtmlLabelName(16752, user.getLanguage());//调配休息日
        } else {
            typeName = "";
        }
        return typeName;
    }
}
