package com.engine.attendance.vacation.service.impl;

import com.engine.attendance.attendanceplan.service.AttendancePlanService;
import com.engine.attendance.attendanceplan.service.impl.AttendancePlanServiceImpl;
import com.engine.attendance.vacation.cmd.HandleOverdueVocationCmd;
import com.engine.attendance.vacation.cmd.VocationCmd;
import com.engine.attendance.vacation.service.VocationService;
import com.engine.common.cmd.GetPersonDateBySuitOrganzation;
import com.engine.common.util.DbTools;
import com.engine.common.util.ServiceUtil;
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.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class VocationServiceImpl extends Service implements VocationService {

    private AttendancePlanService basicsetService = ServiceUtil.getService(AttendancePlanServiceImpl.class);


    @Override
    public void generateVocation(Map<String, Object> params) {
        String modeId = Util.null2String(params.get("modeId"));
        Map<String,Object> param = Maps.newHashMap();
        param.put("modeId",modeId);
        Map<String,Object> result = commandExecutor.execute(new GetPersonDateBySuitOrganzation(param));

        String sql = "select a.id userid,a.*,b.* from hrmresource a left join cus_fielddata b on a.id=b.id where a.id in (";

        List<String> userIds = Lists.newArrayList();
        for (Map.Entry<String, Object> entry : result.entrySet()){
            userIds.add(entry.getKey());
        }
        List<List<String>> userPartions = Lists.partition(userIds,50);
        List<Map<String,Object>> dataList = Lists.newArrayList();
        for (List<String> list : userPartions){
            String querySql = sql + String.join(",",list) +")";
            log.info("querySql : [{}]",querySql);
            List<Map<String,Object>> resultList = DbTools.getSqlToListKeySmallLetter(querySql);
            resultList = resultList.stream().filter(e->"HrmCustomFieldByInfoType".equals(e.get("scope")) || "".equals(Util.null2String(e.get("scope")))).collect(Collectors.toList());
            dataList.addAll(resultList);
        }
        Map<String,List<Map<String,Object>>> userIdMap = dataList.stream().collect(Collectors.groupingBy(e -> Util.null2String(e.get("userid"))));


        for (Map.Entry<String, Object> entry : result.entrySet()){
            String userId = entry.getKey();
            Map<String,String> vocationMap = (Map<String, String>)entry.getValue();
            Map<String,Object> paramMap = Maps.newHashMap();
            paramMap.put("startDate","");
            paramMap.put("endDate","");
            paramMap.put("resourceId",userId);
            Map<String,Object> dataMap = basicsetService.getAttendanceItemsByPerson(paramMap);
            List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)dataMap.get("data");
            Map<String, Object> needGenerateVocationMap = Maps.newHashMap();
            for (Map<String,Object> map : attendanceItems){
                if (vocationMap.get(map.get("key")) != null){
                    needGenerateVocationMap.put(Util.null2String(map.get("key")),vocationMap.get(map.get("key")));
                }
            }
            log.info("VocationServiceImpl userId:{}, userList:{}",userId,userIdMap.get(userId));
            for (Map.Entry<String, Object> e:needGenerateVocationMap.entrySet()){
                if (e.getValue() != null && ((List<Map<String,Object>>)e.getValue()).size() > 0){
                    Map<String,Object> vocationParam = Maps.newHashMap();
                    vocationParam.put("userId",userId);
                    vocationParam.put("vocationList",e.getValue());
                    vocationParam.put("userList",userIdMap.get(userId));
                    vocationParam.put("releaseDate",params.get("releaseDate"));
                    vocationParam.put("cover",params.get("cover"));
                    commandExecutor.execute(new VocationCmd(vocationParam));
                }
            }
        }
    }


    @Override
    public void handleOverdueVocation(Map<String, Object> params) {
        commandExecutor.execute(new HandleOverdueVocationCmd(params));
    }
}
