package com.op.mes.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.constant.UserConstants;
import com.op.common.core.utils.DateUtils;
import com.op.common.core.utils.StringUtils;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.mes.domain.ProRouteProcess;
import com.op.mes.mapper.ProRouteMapper;
import com.op.mes.mapper.ProRouteProcessMapper;
import com.op.mes.service.IProRouteProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 工艺组成Service业务层处理
 *
 * @author yinjinlu
 * @date 2022-05-13
 */
@Service
public class ProRouteProcessServiceImpl implements IProRouteProcessService
{
    @Autowired
    private ProRouteMapper proRouteMapper;

    @Autowired
    private ProRouteProcessMapper proRouteProcessMapper;

    /**
     * 查询工艺组成
     *
     * @param recordId 工艺组成主键
     * @return 工艺组成
     */
    @Override
    @DS("#header.poolName")
    public ProRouteProcess selectProRouteProcessByRecordId(String recordId)
    {
        return proRouteProcessMapper.selectProRouteProcessByRecordId(recordId);
    }

    /**
     * 查询工艺组成列表
     *
     * @param proRouteProcess 工艺组成
     * @return 工艺组成
     */
    @Override
    @DS("#header.poolName")
    public List<ProRouteProcess> selectProRouteProcessList(ProRouteProcess proRouteProcess)
    {
        return proRouteProcessMapper.selectProRouteProcessList(proRouteProcess);
    }

    @Override
    @DS("#header.poolName")
    public Boolean checkOrderNumExists(ProRouteProcess proRouteProcess) {
        ProRouteProcess process = proRouteProcessMapper.checkOrderNumExists(proRouteProcess);
        String recordId = proRouteProcess.getRecordId();
        if(StringUtils.isNotNull(process) && process.getRecordId().equals(recordId)){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    @DS("#header.poolName")
    public Boolean checkProcessExists(ProRouteProcess proRouteProcess) {
        ProRouteProcess process = proRouteProcessMapper.checkProcessExists(proRouteProcess);
        String recordId = proRouteProcess.getRecordId();
        if(StringUtils.isNotNull(process) && process.getRecordId().equals(recordId) ){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    @DS("#header.poolName")
    public Boolean checkUpdateFlagUnique(ProRouteProcess proRouteProcess) {
        ProRouteProcess process = proRouteProcessMapper.checkUpdateFlagUnique(proRouteProcess);
        String recordId = proRouteProcess.getRecordId();
        if(StringUtils.isNotNull(process) && process.getRecordId().equals(recordId)){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

//    @Override
//    public boolean checkKeyProcess(ProFeedback feedback) {
//        //根据当前生产的产品获取对应的工艺路线
//        String routeId =-1L,processId = -1L;
//        ProRouteProduct param = new ProRouteProduct();
//        param.setItemId(feedback.getItemId());
//        List<ProRouteProduct> products = proRouteProductMapper.selectProRouteProductList(param);
//        if(CollectionUtil.isNotEmpty(products)){
//            products = products.stream().filter(item -> proRouteMapper.selectProRouteByRouteId(item.getRouteId()).getEnableFlag().equals(UserConstants.YES)).collect(Collectors.toList());
//            if (CollectionUtil.isNotEmpty(products)){
//                routeId = products.get(0).getRouteId();
//            }
//        }
//
//        //根据工作站获取工序
//        MdWorkstation workstation = mdWorkstationMapper.selectMdWorkstationByWorkstationId(feedback.getWorkstationId());
//        processId = workstation.getProcessId();
//
//        //再判断当前的工序在此工艺路线中是否是关键工序
//        ProRouteProcess param2 = new ProRouteProcess();
//        param2.setRouteId(routeId);
//        param2.setProcessId(processId);
//        param2.setKeyFlag(UserConstants.YES);
//        List<ProRouteProcess> processes = proRouteProcessMapper.selectProRouteProcessList(param2);
//        if(CollectionUtil.isNotEmpty(processes)){
//            return true;
//        }
//        return false;
//    }

    @Override
    @DS("#header.poolName")
    public ProRouteProcess findPreProcess(ProRouteProcess proRouteProcess) {
        return proRouteProcessMapper.findPreProcess(proRouteProcess);
    }

    @Override
    @DS("#header.poolName")
    public ProRouteProcess findNextProcess(ProRouteProcess proRouteProcess) {
        return proRouteProcessMapper.findNextProcess(proRouteProcess);
    }

    /**
     * 新增工艺组成
     *
     * @param proRouteProcess 工艺组成
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int insertProRouteProcess(ProRouteProcess proRouteProcess)
    {
        proRouteProcess.setCreateTime(DateUtils.getNowDate());
        proRouteProcess.setRecordId(IdUtils.fastSimpleUUID());
        return proRouteProcessMapper.insertProRouteProcess(proRouteProcess);
    }

    /**
     * 修改工艺组成
     *
     * @param proRouteProcess 工艺组成
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int updateProRouteProcess(ProRouteProcess proRouteProcess)
    {
        proRouteProcess.setUpdateTime(DateUtils.getNowDate());
        return proRouteProcessMapper.updateProRouteProcess(proRouteProcess);
    }

    /**
     * 批量删除工艺组成
     *
     * @param recordIds 需要删除的工艺组成主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int deleteProRouteProcessByRecordIds(String[] recordIds)
    {
        return proRouteProcessMapper.deleteProRouteProcessByRecordIds(recordIds);
    }

    /**
     * 删除工艺组成信息
     *
     * @param recordId 工艺组成主键
     * @return 结果
     */
    @Override
    @DS("#header.poolName")
    public int deleteProRouteProcessByRecordId(String recordId)
    {
        return proRouteProcessMapper.deleteProRouteProcessByRecordId(recordId);
    }

    @Override
    @DS("#header.poolName")
    public int deleteByRouteId(String routeId) {
        return proRouteProcessMapper.deleteByRouteId(routeId);
    }
}
