package com.engine.kq.cmd.shiftschedule;

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 com.engine.kq.biz.KQHolidaySetBiz;
import com.engine.kq.biz.KQShiftManagementComInfo;
import com.engine.kq.cmd.shiftmanagement.toolkit.ShiftManagementToolKit;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;

import java.util.*;

public class GetHrmScheduleReportInfoCmd extends AbstractCommonCommand<Map<String, Object>> {

	public GetHrmScheduleReportInfoCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> result = new HashMap<String, Object>();
		RecordSet rs = new RecordSet();
		Map<String, Object> data = null;

		try{
			ResourceComInfo ResourceComInfo = new ResourceComInfo();
			String fromDate = "", toDate = "", year = "", month = "";
			int type = Util.getIntValue(Util.null2String(params.get("type")), 1);
			String kqDate = Util.null2String(params.get("kqDate"));//月份
			String resourceId = Util.null2String(params.get("resourceId"));
			//添加级别控制的权限
			int subCompanyId = Util.getIntValue((String) params.get("subCompanyId"), 0);//指定查看的人员的所属分部
			int departmentId = Util.getIntValue((String) params.get("departmentId"), 0);//指定查看的人员的所属部门

			if(resourceId.length()==0){
				resourceId = "" + user.getUID();
			}
			if(type == 1){
				if(kqDate.length() == 0 || kqDate.length() != 4){//不合法数据转当前年
					kqDate = DateUtil.getYear();
				}
				year = kqDate;
				month = "";
				fromDate = kqDate + "-01-01";
				toDate = DateUtil.getLastDayOfYear(DateUtil.parseToDate(fromDate));
			}else{
				if(kqDate.length() == 0 || kqDate.length() != 7){//不合法数据转当前年月
					kqDate = DateUtil.getYear() + "-" + DateUtil.getMonth();
				}
				String [] arr = kqDate.split("-");
				year = arr[0];
				month = arr[1];
				fromDate = kqDate + "-01";
				toDate = DateUtil.getLastDayOfMonthToString(DateUtil.parseToDate(fromDate));
			}
			boolean isAdmin = user.isAdmin();
			boolean canSearch = HrmUserVarify.checkUserRight("HrmScheduling:set", user);
			//QC 607268 yz  修改我的排班默认展示左侧树
			canSearch = true;
			//QC 607268 yz  修改我的排班，点击下属 提示无权限查看，改为从数据库查询判断
			String allmanagerids = "";
			rs.executeQuery("select managerstr from hrmresource where id = ?",resourceId);
			if(rs.next()){
				allmanagerids = rs.getString("managerstr");
			}
			//添加级别控制的权限
			ResourceComInfo resourceComInfo = new ResourceComInfo();
			if (subCompanyId == 0 || departmentId == 0) {
				subCompanyId = Util.getIntValue(resourceComInfo.getSubCompanyID(resourceId), 0);
				departmentId = Util.getIntValue(resourceComInfo.getDepartmentID(resourceId), 0);
			}
            //boolean shiftRight = HrmUserVarify.checkUserRight("HrmScheduling:set", user);
			boolean shiftRight = HrmUserVarify.checkUserRight("HrmScheduling:set", user, departmentId);

			if(!shiftRight&&!isAdmin && !resourceId.equals("" + user.getUID()) && allmanagerids.indexOf("," + user.getUID() + ",") == -1) {
				result.put("status", "-1");
				result.put("hasRight", false);
				return result;
			}

			result.put("canSearch", canSearch);

			List<Map<String, Object>> holidays = KQHolidaySetBiz.getKQHolidaySetList(resourceId, year, month);
			Map<String, Object> holidayMap = new HashMap<>();
			for(Map<String, Object> h : holidays){
				data = new HashMap<>();
				String tip = "";
				boolean isWorkday = false;
				int dateType = Integer.parseInt(h.get("type").toString());
				if(dateType == 2) {
					tip = SystemEnv.getHtmlLabelName(125807, user.getLanguage());
					isWorkday = true;
				} else if(dateType != 0) {
					tip = SystemEnv.getHtmlLabelName(125806, user.getLanguage());
				}
				data.put("tip", tip);
				data.put("isWorkday", isWorkday);

				holidayMap.put(h.get("date").toString(), data);
			}
			//result.put("holiday", holidayMap);
			//QC  607268 yz   修改我的排班中，不显示节假日相关信息
			result.put("holiday", "");


			StringBuffer sql = new StringBuffer("SELECT kqdate, serialid, color FROM kq_shiftschedule a, kq_shiftmanagement b " +
							"WHERE a.serialid = b.id AND (a.isdelete is null or  a.isdelete <> '1') AND a.kqdate >= ? AND a.kqdate <= ? AND a.resourceid = ? ORDER BY kqdate");
			rs.executeQuery(sql.toString(), fromDate, toDate, resourceId);
			result.put("result", new HashMap<String, Object>());
			HashMap<String, Object> rsMap = (HashMap<String, Object>) result.get("result");
			while(rs.next()){
				String date = Util.null2String(rs.getString("kqdate"));
				String serialid = Util.null2String(rs.getString("serialid"));
				String color = Util.null2String(rs.getString("color"));

				String serial = new ShiftManagementToolKit().getShiftOnOffWorkSections(serialid,user.getLanguage());

				data = new HashMap<>();
				data.put("serial", serial);
				data.put("color", color);

				rsMap.put(date, data);
			}
			result.put("status", "1");
		}catch (Exception ex){
			result.put("status", "-1");
			result.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(ex);
		}
		return result;
	}

	@Override
	public BizLogContext getLogContext() {
		return null;
	}

}
