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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnTravelExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnTravelExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProduceSnTravelExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.ConvertBeanExt;
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.pojo.base.enumutil.CommonEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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 ProduceSnTravelExtService implements IProduceSnTravelExtService {

    @Autowired
    private MesProduceSnTravelExtRepository produceSnTravelExtRepository;

    @Override
    public MesProduceSnTravelExt insertProduceSnTravelExt(ProduceSnExtModel snModel) {
        MesProduceSnTravelExt produceSnTravelExt = new MesProduceSnTravelExt();
        BeanUtils.copyProperties(snModel, produceSnTravelExt);
        ConvertBean.serviceModelInitialize(produceSnTravelExt, snModel.getModifyUser());
        produceSnTravelExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(produceSnTravelExt).convertBean(produceSnTravelExt);
        return produceSnTravelExtRepository.insert(produceSnTravelExt);
    }

    @Override
    public MesProduceSnTravelExt insertEpProduceSnTravelExt(EpProduceSnExtModel snModel) {
        MesProduceSnTravelExt produceSnTravelExt = new MesProduceSnTravelExt();
        BeanUtils.copyProperties(snModel, produceSnTravelExt);
        ConvertBean.serviceModelInitialize(produceSnTravelExt, snModel.getModifyUser());
        produceSnTravelExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(produceSnTravelExt).convertBean(produceSnTravelExt);
        return produceSnTravelExtRepository.insert(produceSnTravelExt);
    }

    @Override
    public MesProduceSnTravelExt insertProduceSnTravelExt(MesProduceSnExt produceSnExt) {
        MesProduceSnTravelExt produceSnTravelExt = new MesProduceSnTravelExt();
        BeanUtils.copyProperties(produceSnExt, produceSnTravelExt, MesPcnExtConstWords.ID);
        ConvertBean.serviceModelInitialize(produceSnTravelExt, produceSnExt.getModifyUser());
        produceSnTravelExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(produceSnTravelExt).convertBean(produceSnTravelExt);
        return produceSnTravelExtRepository.insert(produceSnTravelExt);
    }

    @Override
    public List<MesProduceSnTravelExt> getProduceSnTravelExtListByOrderNo(String organizeCode, String workCenterCode, String workCellCode, String workOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workCenterCode) || StringUtils.isEmpty(workCellCode) || StringUtils.isEmpty(workOrderNo)) {
            return null;
        }
        return produceSnTravelExtRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE, MesPcnExtConstWords.WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        workCenterCode, workCellCode, workOrderNo});
    }

    @Override
    public List<MesProduceSnTravelExt> getProduceSnTravelExtListByOrderNoAndProcessCode(String organizeCode, String processCode, String workCellCode, String workOrderNo) {
        if (StringUtils.isEmpty(processCode)  || StringUtils.isEmpty(workCellCode) || StringUtils.isEmpty(workOrderNo)) {
            return null;
        }
        return produceSnTravelExtRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.PROCESS_CODE, MesPcnExtConstWords.WORK_CELL_CODE, MesPcnExtConstWords.WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        processCode, workCellCode, workOrderNo});
    }

    @Override
    public List<MesProduceSnTravelExt> getProduceSnTravelExtListByLastOrderNo(String organizeCode, String workCenterCode, String workCellCode, String workOrderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workCenterCode) || StringUtils.isEmpty(workCellCode) || StringUtils.isEmpty(workOrderNo)) {
            return null;
        }
        return produceSnTravelExtRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE, MesPcnExtConstWords.LAST_WORK_ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        workCenterCode, workCellCode, workOrderNo});
    }

    @Override
    public MesProduceSnTravelExt getProduceSnTravelExtByOrderNoAndWoAndSn(String organizeCode, String workCenterCode, String workCellCode, String workOrderNo, String serialNumber) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workCenterCode) || StringUtils.isEmpty(workCellCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(serialNumber)) {
            return null;
        }
        return produceSnTravelExtRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE, MesPcnExtConstWords.LAST_WORK_ORDER_NO, MesPcnExtConstWords.SERIAL_NUMBER},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        workCenterCode, workCellCode, workOrderNo, serialNumber});
    }

}
