package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.bu3;

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToSoftAdapterCfgService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProdDataToSoftAdapterCfg;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToSoftAdapterCfgModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToSoftAdapterDataModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProdDataToSoftAdapterCfgRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.icloud.softswitch.sdk.IBsSuitServiceCloud;
import cn.estsh.i3plus.pojo.base.bean.BaseResultBean;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.model.softswitch.SuitModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class SxProdDataToSoftAdapterCfgService implements ISxProdDataToSoftAdapterCfgService {

    @Autowired
    private MesProdDataToSoftAdapterCfgRepository prodDataToSoftAdapterCfgRepository;

    @Autowired
    private IBsSuitServiceCloud bsSuitServiceCloud;

    @Autowired
    private SnowflakeIdMaker snowflakeIdMaker;

    @Override
    public List<MesProdDataToSoftAdapterCfg> queryProdDataToSoftAdapterCfgList(String organizeCode) {
        if (StringUtils.isEmpty(organizeCode)) return null;
        return prodDataToSoftAdapterCfgRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue()});
    }

    @Override
    public BaseResultBean executeBsSuitCaseByCloud(SxProdDataToSoftAdapterDataModel data, SxProdDataToSoftAdapterCfgModel cfgModel, SuitModel suit) {
        try {
            suit.setRequestId(String.valueOf(snowflakeIdMaker.nextId()));
            suit.setBusiIdList(Arrays.asList(String.valueOf(snowflakeIdMaker.nextId())));
            BaseResultBean resultBean = bsSuitServiceCloud.executeBsSuitCaseByCloud(suit);
            if (null == resultBean || !resultBean.isSuccess())
                log.info("MES工厂{}DATE{}执行生产数据回传软适配JOB --- EXEC STRATEGY_CLASS[{}] --- EXEC_CLASS[{}] --- EXEC FAILURE:executeBsSuitCaseByCloud[{}] --- THREAD:[{}]",
                        data.getOrganizeCode(), data.getDate(), data.getClassName(), cfgModel.getClassName(), null == resultBean ? "NULL" : resultBean.getErrorMsg(), data.getCurrentThreadName());
            return resultBean;
        } catch (Exception e) {
            log.info("MES工厂{}DATE{}执行生产数据回传软适配JOB --- EXEC STRATEGY_CLASS[{}] --- EXEC_CLASS[{}] --- EXEC ERROR:executeBsSuitCaseByCloud[{}] --- THREAD:[{}]",
                    data.getOrganizeCode(), data.getDate(), data.getClassName(), cfgModel.getClassName(), e.getMessage(), data.getCurrentThreadName());
            return null;
        }
    }

}
