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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IPackageExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOuterPackage;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOuterPackageDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPackageExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOuterPackageDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOuterPackageRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPackageExtRepository;
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.enumutil.MesEnumUtil;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

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

    @Autowired
    private MesPackageDetailRepository packageDetailRepository;
    @Autowired
    private MesOuterPackageDetailRepository outerPackageDetailRepository;

    @Autowired
    private MesPackageExtRepository packageExtRepository;

    @Autowired
    private MesOuterPackageRepository outerPackageRepository;

    @Autowired
    private MesPackageRepository packageRepository;

    @Override
    public MesPackageDetail getPackageDetailBySn(String organizeCode, String serialNumber) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(serialNumber)) {
            return null;
        }
        return packageDetailRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.SERIAL_NUMBER},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), serialNumber});
    }

    @Override
    public MesPackageDetail getPackageDetailOnValidBySn(String organizeCode, String serialNumber) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(serialNumber)) {
            return null;
        }
        return packageDetailRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.SERIAL_NUMBER},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), serialNumber});
    }


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

    @Override
    public MesOuterPackageDetail getBigPackageDetailByPackNoAndWorkOrderNo(String organizeCode, String packageNo,String workOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packageNo)) {
            return null;
        }
        return outerPackageDetailRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.INNER_PACKAGENO,MesPcnExtConstWords.WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageNo,workOrderNo});
    }
    @Override
    public List<MesOuterPackageDetail> getBigPackageDetailByPackNo(String organizeCode, String packageNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packageNo)) {
            return null;
        }
        return outerPackageDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.OUTER_PACKAGE_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageNo});
    }

    @Override
    public List<MesPackageDetail> getPackageDetailListByPackNo(String organizeCode, String packageNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packageNo)) {
            return null;
        }
        return packageDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PACKAGENO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageNo});
    }

    @Override
    public MesPackageExt getPackageByPackNo(String organizeCode, String packageNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packageNo)) {
            return null;
        }
        return packageExtRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PACKAGENO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageNo});
    }
    @Override
    public MesOuterPackage getBigPackageByPackNo(String organizeCode, String packageNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packageNo)) {
            return null;
        }
        return outerPackageRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.OUTER_PACKAGE_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageNo});
    }

    @Override
    public MesPackage getPackageByPackageNo(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.PACKAGENO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packageNo});
    }

    @Override
    public MesPackage getPackageByCtNo(String organizeCode, String ctNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(ctNo)) {
            return null;
        }
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(ctNo, MesPcnExtConstWords.CT_NO, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{MesEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesPcnExtConstWords.MODIFY_DATE_TIME}, packBean);
        return packageRepository.getByProperty(packBean);
    }

    @Override
    public MesPackageExt getPackageOnValidByPackNo(String organizeCode, String packageNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packageNo)) {
            return null;
        }
        return packageExtRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.PACKAGENO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), packageNo});
    }

    @Override
    public List<MesPackageExt> getPackageListByWorkOrderNo(String organizeCode, String workOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo)) {
            return null;
        }
        return packageExtRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo});
    }

    @Override
    public List<MesPackageExt> getPackageListByLastWorkOrderNo(String organizeCode, String lastWorkOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(lastWorkOrderNo)) {
            return null;
        }
        return packageExtRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.LAST_WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), lastWorkOrderNo});
    }
}
