package com.zbf.quartz.task;

import com.zbf.common.core.redis.RedisCache;
import com.zbf.common.utils.StringUtils;
import com.zbf.system.domain.Plc;
import com.zbf.system.domain.TCkPickingwavegoods;
import com.zbf.system.domain.TOngoodsshelf;
import com.zbf.system.mapper.TCkPickingwavegoodsMapper;
import com.zbf.system.mapper.TOngoodsshelfMapper;
import com.zbf.system.service.IPlcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @program: zbf_wms
 * @description:
 * @author: Tony.wu
 * @create: 2024-07-03 13:42
 **/
@Slf4j
@Component("GeneratePlcIdTask")
public class GeneratePlcIdTask {
    @Autowired
    private IPlcService plcService;
    @Autowired
    private TOngoodsshelfMapper tOngoodsshelfMapper;
    @Autowired
    private TCkPickingwavegoodsMapper tCkPickingwavegoodsMapper;

    /**
     * 定时任务更新plcid
     */
    @Transactional(rollbackFor = Exception.class)
    public void scheduleTask() {
//        System.out.println("开始执行");
        //查询入库表
        List<Plc> plcs = plcService.selectPlcList();
        Plc plc = plcs.get(0);
        //查询入库任务表 到达9999999 则自动从0开始
        if(plc.getPlcid() == 9999999){
            plc.setPlcid(0L);
            plc.setId(1);
            plcService.updatePlc(plc);
        }

        List<TOngoodsshelf> tOngoodsshelves = tOngoodsshelfMapper.selecttOngoodsshelvesPlcList();
        Long count = plc.getPlcid();
        if(tOngoodsshelves.size() > 0){
            List<String> list = null;
            int i = 0;
            if (StringUtils.isNotBlank(tOngoodsshelves.get(0).getCtl())){
                i = 1;
                list =tOngoodsshelves.stream().map(TOngoodsshelf::getCtl).distinct().collect(Collectors.toList());
            }else{
                i = 2;
                list = tOngoodsshelves.stream().map(TOngoodsshelf::getCtl).distinct().collect(Collectors.toList());
            }
            for(String ctl: list){
                TOngoodsshelf tOngoodsshelf1 = new TOngoodsshelf();
                tOngoodsshelf1.setInStatus("0");
                if(i == 1){
                    tOngoodsshelf1.setCtl(ctl);
                }else{
                    tOngoodsshelf1.setLocationId(ctl);
                }
                tOngoodsshelf1.setPlcId(++count);
                tOngoodsshelfMapper.updatePlcId(tOngoodsshelf1);
            }
        }
        if(count.longValue() != plc.getPlcid().longValue()){
            log.info("定时任务更改plc：{}",count);
            plc.setPlcid(count);
            plc.setId(1);
            plcService.updatePlc(plc);
        }
        plcs = plcService.selectPlcList();
        plc = plcs.get(0);
        //查询入库任务表 到达9999999 则自动从0开始
        if(plc.getPlcid() == 9999999){
            plc.setPlcid(0L);
            plc.setId(1);
            plcService.updatePlc(plc);
        }
        count = plc.getPlcid();
        List<TCkPickingwavegoods> tCkPickingwavegoods1 = tCkPickingwavegoodsMapper.selectTckPickingwavegoodsPlcList();
        if(tCkPickingwavegoods1.size() > 0) {
            List<String> list = null;
            int i = 0;
            if (StringUtils.isNotBlank(tCkPickingwavegoods1.get(0).getCtl())){
                i = 1;
                list = tCkPickingwavegoods1.stream().map(TCkPickingwavegoods::getCtl).distinct().collect(Collectors.toList());
            }else{
                i = 2;
                list = tCkPickingwavegoods1.stream().map(TCkPickingwavegoods::getLocationId).distinct().collect(Collectors.toList());
            }
            for(String ctl: list){
                TCkPickingwavegoods tCkPickingwavegoods2 = new TCkPickingwavegoods();
                //任务完成 待拣选
                tCkPickingwavegoods2.setConfirmStatus("0");
                if(i == 1){
                    tCkPickingwavegoods2.setCtl(ctl);
                }else{
                    tCkPickingwavegoods2.setLocationId(ctl);
                }
                tCkPickingwavegoods2.setPlcId(++count);
                tCkPickingwavegoodsMapper.updatePlcId(tCkPickingwavegoods2);
            }
        }
        if(count.longValue() != plc.getPlcid().longValue()){
            log.info("定时任务更改plc：{}",count);
            plc.setPlcid(count);
            plc.setId(1);
            plcService.updatePlc(plc);
        }

    }

}
