package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.base;

import cn.estsh.i3plus.ext.mes.api.base.IContainerUnbundleCloudService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPackageRecord;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPackageRecordRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesContainer;
import cn.estsh.i3plus.pojo.mes.repository.MesContainerRepository;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文件描述
 *
 * @author xiangming.liao
 * @date 2021年04月07日 9:58
 */
@Slf4j
@Service
public class ContainerUnbundleCloudService implements IContainerUnbundleCloudService {

    @Autowired
    private MesContainerRepository containerRepository;

    @Autowired
    private MesPackageRecordRepository packageRecordRepository;

    @Override
    public void doContainerUnbundle(String sn, String organizeCode, String userName) {
        //1、解绑容器
        MesContainer containerByBarCode = getContainerByBarCode(sn, organizeCode);
        if (null == containerByBarCode) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("容器编号【%s】在容器信息中不存在数据，请检查参数", sn)
                    .build();
        }
        containerByBarCode.setUseStatus(MesExtEnumUtil.CONTAINER_USE_STATUS.LEISURE.getValue());
        ConvertBean.saveOrUpdate(containerByBarCode, userName);
        containerRepository.save(containerByBarCode);

        //2、包装记录状态解绑
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(sn, MesExtConstWords.CT_NO, packBean);
        DdlPreparedPack.getStringEqualPack(MesExtEnumUtil.CONTAINER_USE_STATUS.OCCUPY.getValue(), MesExtConstWords.CONTAINER_USE_STATUS, packBean);
        List<MesPackageRecord> packageRecordsByBarcode = packageRecordRepository.findByHqlWhere(packBean);
        for (MesPackageRecord mesPackageRecord : packageRecordsByBarcode) {
            mesPackageRecord.setContainerUseStatus(MesExtEnumUtil.CONTAINER_USE_STATUS.LEISURE.getValue());
            ConvertBean.saveOrUpdate(mesPackageRecord, userName);
        }
        packageRecordRepository.saveAll(packageRecordsByBarcode);
    }


    /**
     * @Description: 根据条码查询容器
     * @Param: [barcode, organizeCode]
     * @return: cn.estsh.i3plus.pojo.mes.bean.MesContainer
     * @Date: 2021/3/23
     */
    private MesContainer getContainerByBarCode(String barcode, String organizeCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(barcode, MesExtConstWords.CT_NO, packBean);
        MesContainer containerByDb = containerRepository.getByProperty(packBean);
        return containerByDb;
    }
}
