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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IPartExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.jx.ISxOqcRuleNoteService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOqcRuleNoteDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.jx.JxOqcRuleNoteDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOqcRuleNoteDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
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;
import org.springframework.util.StringUtils;

import java.util.List;

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

    @Autowired
    private MesOqcRuleNoteDetailRepository oqcRuleNoteDetailRepository;

    @Autowired
    private IPartExtService partService;

    @Override
    public List<MesOqcRuleNoteDetail> getOqcRuleNoteDetailListByPartNo(String organizeCode, String partNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(partNo)) {
            return null;
        }
        return oqcRuleNoteDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PART_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), partNo},
                " order by seq asc ");
    }

    @Override
    public List<MesOqcRuleNoteDetail> getOqcRuleNoteDetailListByOqcNoType(String organizeCode, Integer oqcNoType, String partNo, String categoryCode3) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(oqcNoType)) {
            return null;
        }
        if (MesPcnExtEnumUtil.OQC_CHECK_TYPE.DAILY_INSPECTION.getValue() == oqcNoType) {
            if (StringUtils.isEmpty(partNo)) {
                return null;
            }
            return oqcRuleNoteDetailRepository.findByProperty(
                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.OQC_NO_TYPE, MesPcnExtConstWords.PART_NO},
                    new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), oqcNoType, partNo},
                    " order by seq asc ");
        } else {
            if (StringUtils.isEmpty(categoryCode3)) {
                return null;
            }
            return oqcRuleNoteDetailRepository.findByProperty(
                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.OQC_NO_TYPE, MesPcnExtConstWords.CATEGORY_CODE3},
                    new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), oqcNoType, categoryCode3},
                    " order by seq asc ");
        }
    }
}
