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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2025/07/28  上午 9:32【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Component
@Slf4j
public class PqlineCache {
    @Autowired
    private PqLineMapper pqLineMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private  PqsDeptslineService pqsDeptslineService;
    @Autowired

    private  PqsDeptsService pqsDeptsService;
    private final static String NAME_KEY = "LineCache:";
    @Value("${SYS_TYPE_ZT}")
    private String sysTypeZt;
    @PostConstruct
    public void init() {
        log.info("初始化刷新redis缓存----------------------");
        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);
    }

    @PreDestroy
    public void destroy() {
        log.info("系统运行结束");
        redisUtil.deleteKeysByString(NAME_KEY);
    }

}
