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

    public GetHolidayCountCmd(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 groupId = Util.null2String(params.get("groupId"));//考勤组ID
        String currentYear = Util.null2String(params.get("currentYear"));//年份

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

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

        Map<String, Object> countMap = new HashMap<String, Object>();
        List<Map<String, Object>> countList = new ArrayList<Map<String, Object>>();
        int sumNum1 = 0;//公众假日总计
        int sumNum2 = 0;//调配工作日总计
        int sumNum3 = 0;//调配休息日总计

        String sql = " select changeType,COUNT(*) AS sum_num from kq_HolidaySet where 1=1 ";
        if (!groupId.equals("")) {
            sql += " and groupId in (" + groupId + ")";
        }
        if (!currentYear.equals("")) {
            String dateFrom = currentYear + "-01-01";
            String dateTo = currentYear + "-12-31";
            sql += " and holidayDate>='" + dateFrom + "' and holidayDate<='" + dateTo + "'";
        }
        sql += " group by changeType ";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        while (recordSet.next()) {
            String changeType = recordSet.getString("changeType");
            if (changeType.equals("1")) {
                sumNum1 = recordSet.getInt("sum_num");
            } else if (changeType.equals("2")) {
                sumNum2 = recordSet.getInt("sum_num");
            } else if (changeType.equals("3")) {
                sumNum3 = recordSet.getInt("sum_num");
            }
        }
        countMap = new HashMap<String, Object>();
        countMap.put("changeType", "1");
        countMap.put("typeName", SystemEnv.getHtmlLabelName(16478, user.getLanguage()));
        countMap.put("bgColor", "#00cc66");
        countMap.put("value", sumNum1);
        countList.add(countMap);

        countMap = new HashMap<String, Object>();
        countMap.put("changeType", "2");
        countMap.put("typeName", SystemEnv.getHtmlLabelName(16751, user.getLanguage()));
        countMap.put("bgColor", "#ff0000");
        countMap.put("value", sumNum2);
        countList.add(countMap);

        countMap = new HashMap<String, Object>();
        countMap.put("changeType", "3");
        countMap.put("typeName", SystemEnv.getHtmlLabelName(16752, user.getLanguage()));
        countMap.put("bgColor", "#0066ff");
        countMap.put("value", sumNum3);
        countList.add(countMap);
        resultMap.put("type", countList);
        return resultMap;
    }
}
