package com.sw.mes.sap.task;

import cc.cczn.matrix.commons.tasks.queue.Task;
import cc.cczn.matrix.commons.tasks.queue.TaskProcessor;
import com.alibaba.fastjson.JSON;
import com.sw.mes.base.factory.material.entity.MaterialModel;
import com.sw.mes.base.global.GlobalSwitch;
import com.sw.mes.base.global.GlobalSwitchNamespace;
import com.sw.mes.core.api.system.StoreFeignClients;
import com.sw.mes.core.dto.store.WarehouseMaterialBo;
import com.sw.mes.core.response.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 物料数据同步到线边仓的梳理
 *
 * @Author：wanjia
 * @Date：2024/2/4 15:15
 */
@Service
public class MaterialToStoreHandler implements TaskProcessor<MaterialModel> {
    @Autowired
    private StoreFeignClients storeFeignClients;

    public static AtomicInteger total = new AtomicInteger();

    private static final Logger logger = LoggerFactory.getLogger(MaterialToStoreHandler.class);

    @Override
    public void process(Task<MaterialModel> task) {
        MaterialModel materialModel = task.getData();
        if (null == materialModel) {
            logger.error("materialModel == null...");
            return;
        }
        ArrayList<WarehouseMaterialBo> warehouseMaterialBos = new ArrayList<>();
        WarehouseMaterialBo bo = new WarehouseMaterialBo();
        bo.setMaterialId(materialModel.getId().longValue());
        bo.setMaterialCode(materialModel.getCode());
        bo.setMaterialName(materialModel.getName());
        if (GlobalSwitch.enable(GlobalSwitchNamespace.NAMESPACE_COMMON_SWITCH, "syncWarehouseMaterial", false)) {
            bo.setUpdateMaterial(true);
        }
        warehouseMaterialBos.add(bo);

        Result result = storeFeignClients.syncMaterial(warehouseMaterialBos);
        int tt = total.incrementAndGet();
        if (tt % 1000 == 0) {
            logger.error(new Date() + ": 已经处理任务...." + tt + "条..");
        }
        if (null == result || result.getCode() != 200) {
            logger.error("storeFeignClients error...{}", JSON.toJSONString(result));
        }
    }
}
