package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.recyclablepackage;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.recyclablepackage.IMesRecyclablePackageService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.recyclablepackage.MesRecyclablePackage;
import cn.estsh.i3plus.pojo.mes.bean.recyclablepackage.MesRecyclablePackagePart;
import cn.estsh.i3plus.pojo.mes.bean.recyclablepackage.MesRecyclablePackageType;
import cn.estsh.i3plus.pojo.mes.repository.recyclablepackage.MesRecyclablePackagePartRepository;
import cn.estsh.i3plus.pojo.mes.repository.recyclablepackage.MesRecyclablePackageRepository;
import cn.estsh.i3plus.pojo.mes.repository.recyclablepackage.MesRecyclablePackageTypeRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description : 回收包装
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/7 10:20
 * @Modify:
 **/
@Service
@Slf4j
public class MesRecyclablePackageServiceImpl implements IMesRecyclablePackageService {

    @Autowired
    private MesRecyclablePackageRepository mesRecyclablePackageRepository;

    @Autowired
    private MesRecyclablePackagePartRepository mesRecyclablePackagePartRepository;

    @Autowired
    private MesRecyclablePackageTypeRepository mesRecyclablePackageTypeRepository;

    @Override
    public MesRecyclablePackage getMesRecyclablePackageByPackageSn(String organizeCode, String packageSn) {
        if (StringUtil.isEmptyAndNull(organizeCode) || StringUtil.isEmptyAndNull(packageSn)) {
            return null;
        }
        return mesRecyclablePackageRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PACKAGE_SN},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageSn});
    }

    @Override
    public MesRecyclablePackage checkMesRecyclablePackage(String organizeCode, String packageSn) {
        MesRecyclablePackage recyclablePackage = this.getMesRecyclablePackageByPackageSn(organizeCode, packageSn);
        if (Objects.isNull(recyclablePackage)) {
            MesPcnException.throwMesBusiException("包条码【%s】信息不存在", packageSn);
        }
        return recyclablePackage;
    }

    @Override
    public List<MesRecyclablePackagePart> findMesRecyclablePackagePartByPackageTypeCode(String organizeCode, String packageTypeCode) {
        if (StringUtil.isEmptyAndNull(organizeCode) || StringUtil.isEmptyAndNull(packageTypeCode)) {
            return null;
        }
        return mesRecyclablePackagePartRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PACKAGE_TYPE_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageTypeCode});

    }

    @Override
    public List<MesRecyclablePackagePart> checkMesRecyclablePackagePart(String organizeCode, String packageTypeCode) {
        List<MesRecyclablePackagePart> mesRecyclablePackagePartList = this.findMesRecyclablePackagePartByPackageTypeCode(organizeCode, packageTypeCode);
        if (CollectionUtils.isEmpty(mesRecyclablePackagePartList)) {
            MesPcnException.throwMesBusiException("回用包类型【%s】包装零件信息不存在", packageTypeCode);
        }
        return mesRecyclablePackagePartList;
    }

    @Override
    public MesRecyclablePackagePart getMesRecyclablePackagePartByPackageTypeCodeAndPartNo(String organizeCode, String packageTypeCode, String partNo) {
        if (StringUtil.isEmptyAndNull(organizeCode) || StringUtil.isEmptyAndNull(packageTypeCode) || StringUtil.isEmptyAndNull(partNo)) {
            return null;
        }
        return mesRecyclablePackagePartRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PACKAGE_TYPE_CODE, MesPcnExtConstWords.PART_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageTypeCode, partNo});

    }

    @Override
    public MesRecyclablePackageType getMesRecyclablePackageTypeByPackageTypeCode(String organizeCode, String packageTypeCode) {
        if (StringUtil.isEmptyAndNull(organizeCode) || StringUtil.isEmptyAndNull(packageTypeCode)) {
            return null;
        }
        return mesRecyclablePackageTypeRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PACKAGE_TYPE_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageTypeCode});
    }

    @Override
    public MesRecyclablePackageType checkMesRecyclablePackageType(String organizeCode, String packageTypeCode) {
        MesRecyclablePackageType recyclablePackageType = this.getMesRecyclablePackageTypeByPackageTypeCode(organizeCode, packageTypeCode);
        if (Objects.isNull(recyclablePackageType)) {
            MesPcnException.throwMesBusiException("回用包类型【%s】信息不存在", packageTypeCode);
        }
        return recyclablePackageType;
    }
}
