package com.sw.mes.itemclean.clean.jobhandler;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sw.mes.itemclean.clean.domain.MaterialGroup;
import com.sw.mes.itemclean.clean.service.MaterialGroupService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: JS111816
 * @Description:
 * @Date: 2022/11/14
 * @Modified By:
 **/
@Component
@Slf4j
public class CleanJobHandler {

    @Resource
    private MaterialGroupService materialGroupService;
    @Value("${spring.profiles.active}")
    private String active;

    private static ExecutorService executorService;

    static {
        executorService = new ThreadPoolExecutor(20, 40, 2000, TimeUnit.SECONDS, new LinkedBlockingQueue());
    }

    @XxlJob("cleanJob")
    public void synCleanJob() {
        log.info("清洗组托数据开始补偿同步！");
        if (!active.equals("prod")) {
            log.info("非生产环境，不做清洗重试机制");
        } else {
            LambdaQueryWrapper<MaterialGroup> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MaterialGroup::getStatus, 0);
            List<MaterialGroup> materialGroupList = materialGroupService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(materialGroupList)) {
                materialGroupList.stream().forEach(t -> {
                    executorService.execute(() -> {
                        materialGroupService.fbGroupToWms(t.getGroupNo());
                    });
                });
            }
        }
    }
}
