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

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpWorkCenterContainerLocationService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterContainerLocation;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWorkCenterContainerLocationRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class EpWorkCenterContainerLocationService implements IEpWorkCenterContainerLocationService {

    @Autowired
    private MesWorkCenterContainerLocationRepository workCenterContainerLocationRepository;

    @Override
    public MesWorkCenterContainerLocation insertWorkCenterContainerLocation(MesWorkCenterContainerLocation workCenterContainerLocation) {
        MesWorkCenterContainerLocation workCenterContainerLocationDb1 = workCenterContainerLocationRepository.getByProperty(
                new String[]{MesExtConstWords.IS_DELETED, MesExtConstWords.CT_LOCATION_NO},
                new Object[]{CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workCenterContainerLocation.getCtLocationNo()});
        if (null != workCenterContainerLocationDb1) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("容器位置编号[%s]记录已存在", workCenterContainerLocation.getCtLocationNo())
                    .build();
        }
        MesWorkCenterContainerLocation workCenterContainerLocationDb2 = workCenterContainerLocationRepository.getByProperty(
                new String[]{MesExtConstWords.IS_DELETED, MesExtConstWords.WORK_CENTER_CODE},
                new Object[]{CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workCenterContainerLocation.getWorkCenterCode()});
        if (null != workCenterContainerLocationDb2) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("生产线[%s]记录已存在", workCenterContainerLocation.getWorkCenterCode())
                    .build();
        }
        ConvertBean.serviceModelInitialize(workCenterContainerLocation, workCenterContainerLocation.getCreateUser());
        return workCenterContainerLocationRepository.insert(workCenterContainerLocation);
    }

}
