package com.engine.attendance.component.persongroup.service.impl;

import com.engine.attendance.component.persongroup.cmd.GetDatatableCmd;
import com.engine.attendance.component.persongroup.cmd.GetDatatableCountCmd;
import com.engine.attendance.component.persongroup.cmd.GetDatatableRecurrenceCmd;
import com.engine.attendance.component.persongroup.commonutil.PersongroupCommonUtil;
import com.engine.attendance.component.persongroup.service.SchedulingResultsService;
import com.engine.attendance.enums.SchedulingApproachEnum;
import com.engine.common.util.DateUtil;
import com.engine.common.util.Utils;
import com.engine.core.impl.Service;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
public class SchedulingResultsServiceImpl extends Service implements SchedulingResultsService {
    private  Map<String,Object> personGroupMap = PersongroupCommonUtil.getPersonGroup();
    private  Map<String,Object> departMentMap = PersongroupCommonUtil.getDepartMent();
    private  Map<String,Object> subCompanyMap = PersongroupCommonUtil.getSubCompany();
    private  Map<String,Object> classesInformationMap = PersongroupCommonUtil.getClassesInformation();
    private  Map<String,Object> candidateTeamMap = PersongroupCommonUtil.getCandidateTeam();

    @Override
    public Map<String, Object> queryDataTable(Map<String, Object> params) {
        String recurrence = Util.null2String(params.get("recurrence"));
        Map<String,Object> resultMap = Maps.newHashMap();
        int current = Integer.valueOf(Util.null2String(params.get("current")));
        int pageSize = Integer.valueOf(Util.null2String(params.get("pageSize")));
        int startindex = (current-1)*pageSize;
        int endindex = current*pageSize;
//        Map<String,Object> personGroupMap = CommonUtil.getPersonGroup();
//        Map<String,Object> departMentMap = CommonUtil.getDepartMent();
//        Map<String,Object> subCompanyMap = CommonUtil.getSubCompany();
//        Map<String,Object> classesInformationMap = CommonUtil.getClassesInformation();
//        Map<String,Object> candidateTeamMap = CommonUtil.getCandidateTeam();

        if ("1".equals(recurrence)){
            //递归查询方式
            Map<String,Object> result = commandExecutor.execute(new GetDatatableRecurrenceCmd(params));
            List<Map<String, Object>> dataTableList = (List<Map<String, Object>>)result.get("dataTableList");
            int total = dataTableList.size();
            resultMap.put("total",total);
            if (endindex > total){
                endindex = total;
            }
            startindex = endindex-pageSize;
            if (startindex < 0){
                startindex = 0;
            }
            dataTableList = dataTableList.subList(startindex,endindex);
            for (Map<String, Object> map:dataTableList){
                SetBrowSerName(map);
            }
            log.info("dataTableList : {}",dataTableList);
            resultMap.put("data",dataTableList);

        }else {
            //非递归查询方式
            Map<String,Object> result = commandExecutor.execute(new GetDatatableCountCmd(params));
            int total = Integer.valueOf(result.get("total").toString());
            params.put("total",total);
            resultMap = commandExecutor.execute(new GetDatatableCmd(params));
            List<Map<String,Object>> dataTable = (List<Map<String,Object>>)resultMap.get("data");
            for (Map<String,Object> map :dataTable){
                SetBrowSerName(map);

            }
            resultMap.put("total",total);
        }

        return resultMap;
    }

    /**
     * 塞入 人员、人员分组、部门、分部、班次信息、候选班组浏览按钮显示值
     * @param map
     */
    public void SetBrowSerName(Map<String,Object> map){
        log.info("personGroupMap :{},departMentMap:{},subCompanyMap:{},classesInformationMap:{},candidateTeamMap:{}"
                ,personGroupMap.hashCode(),departMentMap.hashCode(),subCompanyMap.hashCode(),classesInformationMap.hashCode(),candidateTeamMap.hashCode());
        String dxlx = Util.null2String(map.get("dxlx"));
        if (dxlx.equals("0")){
            //人员
            String lastname = Util.null2String(map.get("lastname"));
            String pbdxry = Util.null2String(map.get("pbdxry"));
            pbdxry = pbdxry +"-"+lastname;
            map.put("pbdxry",pbdxry);

        }else if (dxlx.equals("1")){
            //人员分组
            String pbdxryfz = Util.null2String(map.get("pbdxryfz"));
            pbdxryfz= pbdxryfz+"-"+personGroupMap.get(pbdxryfz);
            map.put("pbdxryfz",pbdxryfz);

        }else if (dxlx.equals("2")){
            //部门
            String pbdxbm = Util.null2String(map.get("pbdxbm"));
            pbdxbm= pbdxbm+"-"+departMentMap.get(pbdxbm);
            map.put("pbdxbm",pbdxbm);

        }else if (dxlx.equals("3")){
            //分部
            String pbdxfb = Util.null2String(map.get("pbdxfb"));
            pbdxfb= pbdxfb+"-"+subCompanyMap.get(pbdxfb);
            map.put("pbdxfb",pbdxfb);
        }
        String bcxx = Util.null2String(map.get("bcxx"));
        String hxbz = Util.null2String(map.get("hxbz"));
        if (!"".equals(bcxx)){
            bcxx = bcxx +"-"+classesInformationMap.get(bcxx);
            map.put("bcxx",bcxx);
        }
        if (!"".equals(hxbz)){
            hxbz = hxbz +"-"+candidateTeamMap.get(hxbz);
            map.put("hxbz",hxbz);
        }
    }

    @Override
    public Map<String, Object> queryDataTableActualUse(Map<String, Object> params) {
        Map<String, Object> paramMap = queryDataTable(params);
        Map<String, Object> resultMap = Maps.newHashMap();
        List<Map<String, Object>> dataTableList = (List<Map<String, Object>>)paramMap.get("data");
        List<Map<String, Object>> resultList = Lists.newArrayList();
        resultMap.put("data",Utils.getSchedulingList(dataTableList));
        log.info("queryDataTableActualUse schedule : [{}]",resultList);
        return resultMap;
    }
}
