package cn.iocoder.yudao.module.smartfactory.framework.job.factoryData;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydata.vo.FactoryDataInfoRespVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factorydata.FactoryDataDO;
import cn.iocoder.yudao.module.smartfactory.service.factorydata.FactoryDataService;
import cn.iocoder.yudao.module.smartfactory.service.factorydata.FactoryDataSizeService;
import cn.iocoder.yudao.module.smartfactory.service.packagedata.PackageDataService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 工厂 出入库数据统计 定时任务
 */
@Component
@Slf4j
public class FactoryDataJob {

    @Resource
    private FactoryDataSizeService dataSizeService;

    @Resource
    private FactoryDataService factoryDataService;

    @XxlJob("factoryDataJob")
    @TenantJob // --- ⚠️ 这个注解 会将租户列表拉出来 完了后逐个租户执行 定时任务需要注意
    public ReturnT<String> execute() throws Exception {

        LocalDateTime[] dateTimes = {LocalDateTime.now().minusDays(1).with(LocalTime.MIN), LocalDateTime.now().minusDays(1).with(LocalTime.MAX)};

//        //查询 当前统计时间前一天的的打包线数据
//        List<FactoryDataDO> factoryDataDOS = packageDataService.getDataListByDateTime(dateTimes);

        //查询 当前统计时间前一天的的出入库规格数据
        List<FactoryDataInfoRespVO> infoRespVOS = dataSizeService.getSUMByDateTimes(dateTimes);

        List<FactoryDataDO> outFactoryDataDOs = new ArrayList<>();

//        //处理入库数据
//        factoryDataDOS.forEach(data -> {
//
//            //设置 出入库类型为 入库
//            data.setDataType(2);
//
//            //取出 同一个工厂和日期的数据
//            List<FactoryDataInfoRespVO> infos = infoRespVOS.stream()
//                    .filter(info -> info.getFactoryId().equals(data.getFactoryId()) && info.getDate().equals(data.getDate()))
//                    .collect(Collectors.toList());
//
//            if (!infos.isEmpty()) {
//
//                if (infos.get(0).getInDamageNum() != null) {
//
//                    // 设置 入库破损数
//                    data.setDamageNum(data.getDamageNum() + infos.get(0).getInDamageNum());
//                }
//
//                if (infos.get(0).getInNum() != null) {
//
//                    // 设置 入库总数数
//                    data.setTotalNum(data.getTotalNum() + infos.get(0).getInNum());
//                }
//
//                // 如果存在出库数时
//                if (infos.get(0).getOutNum() != null) {
//
//                    FactoryDataDO dataDO = new FactoryDataDO();
//
//                    dataDO.setFactoryId(data.getFactoryId());
//                    dataDO.setDate(data.getDate());
//                    dataDO.setDataType(1);
//                    dataDO.setTotalNum(infos.get(0).getOutNum());
//                    dataDO.setDamageNum(infos.get(0).getOutDamageNum());
//
//                    outFactoryDataDOs.add(dataDO);
//                }
//            }
//
//            // 移除已处理过的 入库数据
//            infoRespVOS.removeAll(infos);
//        });

        // 插入 打包线以外数据
        for (FactoryDataInfoRespVO infoRespVO : infoRespVOS) {

            if (infoRespVO.getOutNum() != null) {

                // 插入出库数据
                FactoryDataDO outDO = new FactoryDataDO();

                outDO.setFactoryId(infoRespVO.getFactoryId());
                outDO.setDate(infoRespVO.getDate());
                outDO.setDataType(1);
                outDO.setTotalNum(infoRespVO.getOutNum());
                outDO.setDamageNum(infoRespVO.getOutDamageNum());

                outFactoryDataDOs.add(outDO);
            }

            // 插入入库数据
            if (infoRespVO.getInNum() != null) {

                FactoryDataDO inDO = new FactoryDataDO();

                inDO.setFactoryId(infoRespVO.getFactoryId());
                inDO.setDate(infoRespVO.getDate());
                inDO.setDataType(2);
                inDO.setTotalNum(infoRespVO.getInNum());
                inDO.setDamageNum(infoRespVO.getInDamageNum());

                outFactoryDataDOs.add(inDO);
            }
        }

        // 插入
        if (!CollectionUtil.isEmpty(outFactoryDataDOs)) {
            factoryDataService.createData(outFactoryDataDOs);
        }

        // 返回执行成功
        return ReturnT.SUCCESS;
    }
}
