package com.api.customization.qc3189074.util;

import com.alibaba.fastjson.JSON;
import com.wbi.util.Util;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;

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

public class ResourceUtil {

    /**
     * 获取所有人未提交的排班信息
     * @return
     */
    public Map<String,List<List<Object>>> getAllResourceIds(String kqMonth,String groupId){
        CustomScheduleUtil customScheduleUtil = new CustomScheduleUtil();
        Map<String,List<List<Object>>> resourceData = new LinkedHashMap<>();//人员排班

        try {
            List<List<Object>> insertList =null;
            String monthBeginDate = DateUtil.getFirstDayOfMonth(kqMonth+"-01");
            String monthEndDate = DateUtil.getLastDayOfMonth(kqMonth+"-01");
            RecordSet rs = new RecordSet();
            String sql = "select  t.resourceid, t.groupid, t.status, t.dsporder,t.lastname,t.subcompanyid1, t.departmentid, t.loginid  from (SELECT distinct t.resourceid, t.groupid, t.status, t.dsporder,t.lastname,t.subcompanyid1, t.departmentid, t.loginid FROM (  " +
                    " SELECT a.id AS resourceid, b.groupid, a.status,a.dsporder,a.lastname,a.subcompanyid1, a.departmentid, a.loginid FROM HrmResource a, kq_groupmember b  " +
                    " WHERE a.id=b.typevalue and b.type =1 and (b.isdelete is null or  b.isdelete <> '1')  " +
                    " UNION ALL  " +
                    " SELECT a.id AS resourceid, b.groupid, a.status,a.dsporder,a.lastname,a.subcompanyid1, a.departmentid, a.loginid FROM HrmResource a, kq_groupmember b  " +
                    " WHERE a.subcompanyid1 = b.typevalue AND a.seclevel>=b.seclevel AND a.seclevel<=b.seclevelto AND b.type=2 and (b.isdelete is null or  b.isdelete <> '1')  " +
                    " UNION ALL  " +
                    " SELECT a.id AS resourceid, b.groupid, a.status,a.dsporder,a.lastname,a.subcompanyid1, a.departmentid, a.loginid FROM HrmResource a, kq_groupmember b  " +
                    " WHERE a.departmentid = b.typevalue AND a.seclevel>=b.seclevel AND a.seclevel<=b.seclevelto AND b.type=3 and (b.isdelete is null or  b.isdelete <> '1')  " +
                    " UNION ALL  " +
                    " SELECT a.id AS resourceid, b.groupid, a.status,a.dsporder,a.lastname,a.subcompanyid1, a.departmentid, a.loginid FROM HrmResource a, kq_groupmember b  " +
                    " WHERE  (a.jobtitle = b.typevalue AND b.type=5 and (b.isdelete is null or  b.isdelete <> '1') AND (b.jobtitlelevel=1 OR (b.jobtitlelevel=2 AND a.subcompanyid1 IN(b.jobtitlelevelvalue)) OR (b.jobtitlelevel=3 AND a.departmentid IN(b.jobtitlelevelvalue)))) " +
                    " UNION ALL  SELECT a.id AS resourceid, b.groupid, a.status,a.dsporder,a.lastname,a.subcompanyid1, a.departmentid, a.loginid FROM HrmResource a, kq_groupmember b " +
                    " WHERE b.type=6 AND a.seclevel>=b.seclevel AND a.seclevel<=b.seclevelto and (b.isdelete is null or b.isdelete <> '1') )t ) t where t.groupid = "+groupId+" and t.status in(0,1,2,3) and t.loginid is not null  and t.loginid<>''  and (t.status = 0 or t.status = 1 or t.status = 2 or t.status = 3)   order by dsporder asc, lastname asc ";
            rs.executeQuery(sql);
            while (rs.next()){
                String resourceId = Util.null2String(rs.getString("resourceid"));
                List<List<Object>> shiftScheduleDatas = getShiftSchedule(groupId,resourceId,monthBeginDate,monthEndDate);
                boolean flag = customScheduleUtil.checkStatus(resourceId, groupId, kqMonth);
                if (flag){
                    resourceData.put(resourceId,shiftScheduleDatas);
                }
            }
        } catch (Exception e) {
            new BaseBean().writeLog("所有人员排班报错:" + JSON.toJSONString(e));
        }
        return resourceData;
    }

    private List<List<Object>> getShiftSchedule(String groupid, String resourceid, String monthBeginDate,String monthEndDate){
        List<List<Object>> insertList = new ArrayList<>();
        try {

            ArrayList<Object> data = null;
            String sql = "";
            RecordSet rs = new RecordSet();
            sql = "	select id,resourceid,kqdate,serialid from kq_flow_shiftschedule where groupid= '"+groupid+"' and resourceid="+resourceid+" " +
                    "	and kqdate>='"+monthBeginDate+"' and kqdate<='"+monthEndDate+"' and (isdelete is null or  isdelete <> '1')";
            rs.executeQuery(sql);
            while(rs.next()){
                String kqdate = weaver.general.Util.null2String(rs.getString("kqdate"));
                String serialid = weaver.general.Util.null2String(rs.getString("serialid"));

                data = new ArrayList<>();
                data.add(Integer.parseInt(resourceid));//人员
                data.add(0);//分部
                data.add(0);//部门
                data.add(0);//岗位
                data.add(kqdate);//排班日期
                data.add(serialid);//排班id
                insertList.add(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return insertList;
    }
}
