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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxOqcCheckService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOqcCheck;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOqcCheckDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOqcCheckPackageDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOqcCheckPackageIf;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOqcCheckDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOqcCheckPackageDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOqcCheckPackageIfRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOqcCheckRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * @Author : wangjie
 * @CreateDate : 2022-02-15
 * @Modify:
 **/
@Service
public class JxOqcCheckService implements IJxOqcCheckService {

    @Autowired
    private MesOqcCheckRepository oqcCheckRepository;

    @Autowired
    private MesOqcCheckDetailRepository oqcCheckDetailRepository;

    @Autowired
    private MesOqcCheckPackageDetailRepository oqcCheckPackageDetailRepository;

    @Autowired
    private MesOqcCheckPackageIfRepository oqcCheckPackageIfRepository;

    @Override
    public MesOqcCheck getOqcCheckByOqcNo(String organizeCode, String oqcNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(oqcNo)) {
            return null;
        }
        return oqcCheckRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.OQC_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), oqcNo});
    }

    @Override
    public List<MesOqcCheckPackageDetail> getOqcCheckPackageDetailListByWorkOrderNo(String organizeCode, String workOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo)) {
            return null;
        }
        return oqcCheckPackageDetailRepository.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<MesOqcCheck> getOqcCheckListByWorkOrderNo(String organizeCode, String workOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo)) {
            return null;
        }
        return oqcCheckRepository.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 MesOqcCheck getUnTeardownOqcCheckByWorkOrderNo(String organizeCode, String workOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo)) {
            return null;
        }
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workOrderNo, MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getNumNOEqualPack(MesPcnExtEnumUtil.OQC_CHECK_TYPE.TEARDOWN_INSPECTION.getValue(), MesPcnExtConstWords.OQC_NO_TYPE, packBean);
        return oqcCheckRepository.getByProperty(packBean);
    }

    @Override
    public MesOqcCheck getZjOqcCheckByWorkOrderNo(String organizeCode, String workOrderNo, String workTime, String shiftCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(workTime) || StringUtils.isEmpty(shiftCode)) {
            return null;
        }
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workOrderNo, MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(workTime, MesPcnExtConstWords.WORK_TIME, packBean);
        DdlPreparedPack.getStringEqualPack(shiftCode, MesPcnExtConstWords.SHIFT_CODE, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesPcnExtConstWords.CREATE_DATE_TIME}, packBean);
        return oqcCheckRepository.getByProperty(packBean);
    }

    @Override
    public List<MesOqcCheck> getOqcCheckListByOrderAndTime(String organizeCode, String workOrderNo, String workTime, String shiftCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(workTime) || StringUtils.isEmpty(shiftCode)) {
            return null;
        }
        return oqcCheckRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.WORK_TIME, MesPcnExtConstWords.SHIFT_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo, workTime, shiftCode});
    }

    @Override
    public List<MesOqcCheckDetail> getOqcCheckDetailListByOqcNo(String organizeCode, String oqcNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(oqcNo)) {
            return null;
        }
        return oqcCheckDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.OQC_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), oqcNo});
    }

    @Override
    public List<MesOqcCheckPackageDetail> getOqcCheckPackageDetailListByOrderNoAndWorkTime(String organizeCode, String workOrderNo, String workTime, String shiftCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(workTime) || StringUtils.isEmpty(shiftCode)) {
            return null;
        }
        return oqcCheckPackageDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.WORK_TIME, MesPcnExtConstWords.SHIFT_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo, workTime, shiftCode});
    }

    @Override
    public List<MesOqcCheckPackageDetail> getOqcCheckPackageDetailListByOqcNo(String organizeCode, String oqcNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(oqcNo)) {
            return null;
        }
        return oqcCheckPackageDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.OQC_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), oqcNo});
    }

    @Override
    public List<MesOqcCheckPackageIf> getOqcCheckPackageIfListByOqcNo(String organizeCode, String oqcNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(oqcNo)) {
            return null;
        }
        return oqcCheckPackageIfRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.OQC_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), oqcNo});
    }

    @Override
    public List<MesOqcCheckPackageIf> getOqcCheckPackageIfListByOrderNoAndWorkTimeAndShiftCode(String organizeCode, String workOrderNo, String workTime, String shiftCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(workTime) || StringUtils.isEmpty(shiftCode)) {
            return null;
        }
        return oqcCheckPackageIfRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.WORK_TIME, MesPcnExtConstWords.SHIFT_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo, workTime, shiftCode});
    }

    @Override
    public List<MesOqcCheck> getOqcCheckListBySn(String organizeCode, String workOrderNo, String serialNumber) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(serialNumber)) {
            return null;
        }
        return oqcCheckRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.SERIAL_NUMBER},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo, serialNumber},
                " order by modifyDatetime desc ");
    }

    @Override
    public List<MesOqcCheck> getOqcCheckListByProductSn(String organizeCode, String workOrderNo, String productSn) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(productSn)) {
            return null;
        }
        return oqcCheckRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.PRODUCT_SN},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo, productSn},
                " order by modifyDatetime desc ");
    }

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

    @Override
    public MesOqcCheckPackageDetail getOqcCheckPackageDetailByOrderNoAndProductSn(String organizeCode, String workOrderNo, String productSn) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(productSn)) {
            return null;
        }
        return oqcCheckPackageDetailRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.PRODUCT_SN},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo, productSn});
    }

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

}
