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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesPackageExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.MesPackage;
import cn.estsh.i3plus.pojo.mes.bean.MesPackageDetail;
import cn.estsh.i3plus.pojo.mes.repository.MesPackageDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesPackageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description : 包装信息
 * @Reference :
 * @Author : wangjie
 * @CreateDate 2024/6/13 15:18
 * @Modify:
 **/
@Service
public class MesPackageExtService implements IMesPackageExtService {

    @Autowired
    private MesPackageRepository packageRepository;

    @Autowired
    private MesPackageDetailRepository packageDetailRepository;

    @Override
    public MesPackage getMesPackage(String organizeCode, Long id) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(id)) return null;
        return packageRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), id});
    }

    @Override
    public MesPackage getPackageDb(String organizeCode, String packageNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packageNo)) return null;
        return packageRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PACKAGE_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageNo});
    }

    @Override
    public MesPackageDetail getPackageDetailByPackageNo(String organizeCode, String packageNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packageNo)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(packageNo, MesPcnExtConstWords.PACKAGE_NO, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesPcnExtConstWords.CREATE_DATE_TIME}, packBean);
        return packageDetailRepository.getByProperty(packBean);
    }

    @Override
    public MesPackageDetail getPackageDetailByProductSn(String organizeCode, String productSn) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(productSn)) return null;
        MesPackageDetail packageDetail = getPackageDetailByProductSnWithProperty(organizeCode, productSn, MesPcnExtConstWords.SERIAL_NUMBER);
        if (null == packageDetail) packageDetail = getPackageDetailByProductSnWithProperty(organizeCode, productSn, MesPcnExtConstWords.PRODUCT_SN);
        if (null == packageDetail || StringUtils.isEmpty(packageDetail.getPackageNo())) return null;
        return packageDetail;
    }

    @Override
    public MesPackageDetail getPackageDetailByProductSnWithProperty(String organizeCode, String productSn, String propertyName) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(productSn, propertyName, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesPcnExtConstWords.CREATE_DATE_TIME}, packBean);
        return packageDetailRepository.getByProperty(packBean);
    }
}
