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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IErrorLineOffService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesErrorLineOff;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesErrorLineOffDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesErrorLineOffDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesErrorLineOffRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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 ErrorLineOffService implements IErrorLineOffService {

    @Autowired
    private MesErrorLineOffRepository errorLineOffRepository;

    @Autowired
    private MesErrorLineOffDetailRepository errorLineOffDetailRepository;

    @Override
    public List<MesErrorLineOff> getErrorLineOffListByWorkOrderNo(String organizeCode, String workOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo)) {
            return null;
        }
        return errorLineOffRepository.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 MesErrorLineOff getErrorLineOffBySn(String organizeCode, String serialNumber) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(serialNumber)) {
            return null;
        }
        return errorLineOffRepository.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 MesErrorLineOff getErrorLineOffBySnAndOrder(String organizeCode, String workOrderNo, String serialNumber) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(serialNumber) || StringUtils.isEmpty(workOrderNo)) {
            return null;
        }
        return errorLineOffRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.SERIAL_NUMBER, MesPcnExtConstWords.WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), serialNumber, workOrderNo});
    }


    @Override
    public List<MesErrorLineOffDetail> getErrorLineOffDetailListByPid(Long pid) {
        if (StringUtils.isEmpty(pid)) {
            return null;
        }
        return errorLineOffDetailRepository.findByProperty(new String[]{MesPcnExtConstWords.PID}, new Object[]{pid});
    }

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