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

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

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		List<Object> lsColumns = new ArrayList<>();
		Map<String, Object> column = null;
		List<Object> lsDatas = null;
		Map<String, Object> data = null;
		String sql = "";
		RecordSet rs = new RecordSet();

		try{
			//必要的权限判断
			if(!HrmUserVarify.checkUserRight("HrmKQGroup:Add",user)) {
		  	retmap.put("status", "-1");
		  	retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
		  	return retmap;
		  }

			Calendar cal = DateUtil.getCalendar();
			KQGroupComInfo kQGroupComInfo = new KQGroupComInfo();
			KQShiftManagementComInfo kQShiftManagementComInfo = new KQShiftManagementComInfo();
		  String groupId = Util.null2String(params.get("groupId"));//考勤组id
			String kqMonth = Util.null2String(params.get("kqMonth"));//月份
		  String monthBeginDate = DateUtil.getFirstDayOfMonth(kqMonth+"-01");
			String monthEndDate = DateUtil.getLastDayOfMonth(kqMonth+"-01");

			boolean hasRestShift = false;
			Map<String,Object> mapShiftSchedule = new HashMap<>();
			sql = " select kqdate,serialid,count(resourceid) as num from kq_shiftschedule " +
						" where (isdelete is null or  isdelete <> '1') and kqdate>=? and kqdate<=? and groupid=? " +
						" group by kqdate,serialid ";
			rs.executeQuery(sql,monthBeginDate,monthEndDate,groupId);
			while(rs.next()){
				if(Util.null2String(rs.getString("serialid")).equals("-1")){
					hasRestShift=true;
				}
				mapShiftSchedule.put(rs.getString("kqdate")+"|"+rs.getInt("serialid"),rs.getInt("num"));
			}

			column = new HashMap<String, Object>();
			column.put("title", SystemEnv.getHtmlLabelName(389646,user.getLanguage()));
			column.put("dataIndex", "serialName");
			lsColumns.add(column);
			boolean isEnd = false;
			for(String date=monthBeginDate; !isEnd;) {
				if (date.equals(monthEndDate)) isEnd = true;
				column = new HashMap<String, Object>();
				column.put("title", date);
				column.put("dataIndex", date);
				lsColumns.add(column);
				cal.setTime(DateUtil.parseToDate(date));
				date = DateUtil.getDate(cal.getTime(), 1);
			}

			List<String> lsSerialId = Util.splitString2List(Util.null2String(kQGroupComInfo.getSerialids(groupId)),",");
			if(hasRestShift && !lsSerialId.contains("-1"))lsSerialId.add("-1");//统计休息班
			lsDatas = new ArrayList<>();
			for(int i=0;lsSerialId!=null&&i<lsSerialId.size();i++){
				data = new HashMap<>();
				data.put("serialId",lsSerialId.get(i));
				if(lsSerialId.get(i).equals("-1")){
					data.put("serialName",SystemEnv.getHtmlLabelName(26593,user.getLanguage()));
				}else{
					data.put("serialName",kQShiftManagementComInfo.getSerial(lsSerialId.get(i)));
				}
				data.put("bgColor",kQShiftManagementComInfo.getColor(lsSerialId.get(i)));
				isEnd = false;
				for(String date=monthBeginDate; !isEnd;) {
					if (date.equals(monthEndDate)) isEnd = true;
					data.put(date,mapShiftSchedule.get(date+"|"+lsSerialId.get(i)));
					cal.setTime(DateUtil.parseToDate(date));
					date = DateUtil.getDate(cal.getTime(), 1);
				}
				lsDatas.add(data);
			}
			retmap.put("columns", lsColumns);
			retmap.put("datas", lsDatas);
			retmap.put("groupName", kQGroupComInfo.getGroupname(groupId));
			retmap.put("status", "1");
		}catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}

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

}
