package com.engine.salary.timer;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.salaryBill.po.SalarySendInfoPO;
import com.engine.salary.entity.salaryBill.po.SalarySendPO;
import com.engine.salary.entity.salaryBill.po.SalaryTemplatePO;
import com.engine.salary.service.SalarySendService;
import com.engine.salary.service.SalaryTemplateService;
import com.engine.salary.service.impl.SalarySendServiceImpl;
import com.engine.salary.service.impl.SalaryTemplateServiceImpl;
import com.engine.salary.util.SalaryDateUtil;
import com.engine.salary.util.SalaryEntityUtil;

import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Harryxzy
 * @ClassName AutoConfirmSalaryBill
 * @date 2023/07/11 15:50
 * @description 自动确认工资单
 */
public class AutoConfirmSalaryBillCronJob extends BaseCronJob {
    private SalarySendService getSalarySendService(User user) {
        return ServiceUtil.getService(SalarySendServiceImpl.class, user);
    }

    private SalaryTemplateService getSalaryTemplateService(User user) {
        return ServiceUtil.getService(SalaryTemplateServiceImpl.class, user);
    }

    @Override
    public void execute() {
        User user = new User();
        user.setUid(1);
        user.setLoginid("sysadmin");
        // 获取所有需要确认反馈的默认工资单模板
        List<SalaryTemplatePO> templates = getSalaryTemplateService(user).listNeedAckDefaultTemplate();
        // key:薪资账套id，value：对应的自动确认天数
        Map<Long, Integer> salarySobAckDaysMap = SalaryEntityUtil.convert2Map(templates, SalaryTemplatePO::getSalarySobId, SalaryTemplatePO::getAutoAckDays);
        // 根据薪资账套id获取已发送，确认状态为未确认的工资单
        List<SalarySendInfoPO> unConfirmedList = getSalarySendService(user).listUnConfirmedSendInfo(new ArrayList<>(salarySobAckDaysMap.keySet()));
        List<SalarySendPO> sendList = getSalarySendService(user).getByIds(unConfirmedList.stream().map(SalarySendInfoPO::getSalarySendId).distinct().collect(Collectors.toList()));
        // key:工资单发放id，value：自动确认超时天数
        HashMap<Long, Integer> autoAckDaysMap = new HashMap<>();
        sendList.stream().forEach(send -> {
            Integer autoAckDays = salarySobAckDaysMap.getOrDefault(send.getSalarySobId(), 7);
            autoAckDaysMap.put(send.getId(), autoAckDays);
        });
        LocalDateTime now = LocalDateTime.now();
        List<Long> needAutoIds = unConfirmedList.stream().filter(po -> {
            LocalDateTime sendDateTime = SalaryDateUtil.dateToLocalDateTime(po.getSendTime());
            Integer autoDays = autoAckDaysMap.get(po.getSalarySendId());
            if (autoDays == null) {
                return false;
            }
            LocalDateTime autoDateTime = sendDateTime.plus(autoDays, ChronoUnit.DAYS);
            return !autoDateTime.isAfter(now);
        }).map(SalarySendInfoPO::getId).collect(Collectors.toList());
        // 自动确认
        getSalarySendService(user).autoConfirmSalaryBill(needAutoIds);
    }

}
