package com.jwipc.job.handler;

import com.jwipc.finance.model.po.ReceiveVoucherPO;
import com.jwipc.finance.model.query.ReceiveVoucherQuery;
import com.jwipc.finance.service.IReceiveVoucherService;
import com.jwipc.job.logic.impl.EmailSendLogicLogicImpl;
import com.jwipc.sys.model.po.FactoryPO;
import com.jwipc.sys.model.query.FactoryQuery;
import com.jwipc.sys.service.IFactoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * SRM系统物料凭证重复提醒邮件
 * </p>
 *
 * @author Chen Tianshi
 * @since 2025/8/13 17:49
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class VoucherRepeatWarnEmailJob extends QuartzJobBean implements InterruptableJob {
    @Autowired
    private EmailSendLogicLogicImpl emailSendLogicLogic;

    @Autowired
    private IReceiveVoucherService receiveVoucherService;

    @Autowired
    private IFactoryService factoryService;

    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        // 1. 获取所有分区表名
        FactoryQuery factoryQuery = new FactoryQuery();
        List<FactoryPO> factoryPOS = factoryService.findPageByQuery(null, factoryQuery);
        List<String> factoryCodes = factoryPOS.stream()
                .map(FactoryPO::getFactory) // 提取 factory 字段
                .collect(Collectors.toList());
        List<ReceiveVoucherPO> allPartitionsData = new ArrayList<>();

        // 获取物料凭证重复数据
        for (String code : factoryCodes) {
            ReceiveVoucherQuery query = new ReceiveVoucherQuery();
            query.setIsSearchRepeat("1");
            query.setFactoryCode(code); // 设置分区字段
            allPartitionsData.addAll(receiveVoucherService.findPageByQuery(null, query));
        }
        if (CollectionUtils.isNotEmpty(allPartitionsData)){
            Map<String,Object> params = new HashMap<>();
            StringBuilder table = new StringBuilder();
            // 表格开始
            table.append("<table border='1'><tbody>")
                    .append("<tr><th>物料凭证编码</th><th>物料凭证年</th><th>物料凭证行</th></tr>"); // 表头

            // 数据行
            for (ReceiveVoucherPO po : allPartitionsData) {
                table.append("<tr class=\"firstRow\">")
                        .append("<td>").append(po.getMvCode()).append("</td>")
                        .append("<td>").append(po.getMvYear()).append("</td>")
                        .append("<td>").append(po.getMvItemNo()).append("</td>")
                        .append("</tr>");
            }

            // 表格结束
            table.append("</tbody></table>");
            String tableStr = table.toString();
            params.put("repeatTable", tableStr);

            // 固定收件人：陈敏
            emailSendLogicLogic.syncSend("chenm1@jwele.com.cn","SRM系统物料凭证重复","JW_POST_161",params,1L,null);
        }
        log.info("{}", allPartitionsData);
    }
}
