package com.njcn.product.event.devcie.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.njcn.product.event.devcie.mapper.PqDeviceMapper;
import com.njcn.product.event.devcie.mapper.PqLineMapper;
import com.njcn.product.event.devcie.pojo.dto.LedgerBaseInfoDTO;
import com.njcn.product.event.devcie.pojo.po.PqLine;
import com.njcn.product.event.devcie.pojo.po.PqsDeptsline;
import com.njcn.product.event.devcie.service.PqsDeptslineService;
import com.njcn.product.event.transientes.pojo.po.PqsDepts;
import com.njcn.product.event.transientes.service.PqsDeptsService;
import com.njcn.product.event.config.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * Description:
 * Date: 2025/08/05  上午 10:17【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Component
@EnableScheduling
public class LineCacheJob {
    @Autowired
    private PqLineMapper pqLineMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private PqsDeptslineService pqsDeptslineService;
    @Autowired
    private PqsDeptsService pqsDeptsService;

    @Autowired
    private PqDeviceMapper pqDeviceMapper;

    private final static String NAME_KEY = "LineCache:";
    @Value("${SYS_TYPE_ZT}")
    private String sysTypeZt;
    @Scheduled(cron="0 0 1 * * ?" ) // 每3钟执行一次
    public void lineCache(){
        redisUtil.deleteKeysByString(NAME_KEY);

        List<PqLine> pqLines = pqLineMapper.selectList(null);
        redisUtil.saveByKey(NAME_KEY + StrUtil.DASHED+"pqLineList",pqLines);
        List<PqsDepts> list = pqsDeptsService.lambdaQuery().eq(PqsDepts::getState, 1).list();
        for (PqsDepts pqsDepts : list) {
            List<String> deptAndChildren = pqsDeptsService.findDeptAndChildren(pqsDepts.getDeptsIndex());
            List<PqsDeptsline> deptslines = pqsDeptslineService.lambdaQuery().in(PqsDeptsline::getDeptsIndex, deptAndChildren).eq(PqsDeptsline::getSystype, sysTypeZt).list();
            List<Integer> deptslineIds = deptslines.stream().map(PqsDeptsline::getLineIndex).collect(Collectors.toList());

            List<Integer> result = new ArrayList<>();
            if(CollUtil.isNotEmpty(deptslineIds)){
                if(deptslineIds.size()> 1000 ){
                 List<List<Integer>> listList = CollUtil.split(deptslineIds,1000);
                 for(List<Integer> li : listList){
                     List<Integer> temList = pqLineMapper.getRunMonitorIds(li);
                     result.addAll(temList);
                 }
                }else {
                    result= pqLineMapper.getRunMonitorIds(deptslineIds);
                }
            }
            redisUtil.saveByKey(NAME_KEY + StrUtil.DASHED+pqsDepts.getDeptsIndex(),result);
        }

        List<PqsDepts> deptsList = pqsDeptsService.lambdaQuery().eq(PqsDepts::getState,1).list();
        redisUtil.saveByKey(NAME_KEY + StrUtil.DASHED+"AllDept",deptsList);
        List<Integer> collect = pqLines.stream().map(PqLine::getLineIndex).collect(Collectors.toList());
        List<LedgerBaseInfoDTO> ledgerBaseInfoDTOS = new ArrayList<>();

        if(collect.size()>1000){
            List<List<Integer>> listIds = CollUtil.split(collect,1000);
            for(List<Integer> itemIds : listIds){
                List<LedgerBaseInfoDTO> temp =pqLineMapper.getBaseLineInfo(itemIds);
                ledgerBaseInfoDTOS.addAll(temp);
            }
        }else {
            List<LedgerBaseInfoDTO> temp =pqLineMapper.getBaseLineInfo(collect);
            ledgerBaseInfoDTOS.addAll(temp);
        }
        redisUtil.saveByKey(NAME_KEY + StrUtil.DASHED+"LedgerBaseInfoDTO",ledgerBaseInfoDTOS);

    }
}
