package com.hw.mes.service.impl;

import java.util.List;

import com.hw.common.core.exception.ServiceException;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.mes.domain.MesBaseProcessUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.hw.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hw.mes.domain.MesBaseProcessProdline;
import com.hw.mes.mapper.MesBaseProcessInfoMapper;
import com.hw.mes.domain.MesBaseProcessInfo;
import com.hw.mes.service.IMesBaseProcessInfoService;

/**
 * 工序信息Service业务层处理
 * 
 * @author Yinq
 * @date 2024-01-24
 */
@Service
public class MesBaseProcessInfoServiceImpl implements IMesBaseProcessInfoService 
{
    @Autowired
    private MesBaseProcessInfoMapper mesBaseProcessInfoMapper;

    /**
     * 查询工序信息
     * 
     * @param processId 工序信息主键
     * @return 工序信息
     */
    @Override
    public MesBaseProcessInfo selectMesBaseProcessInfoByProcessId(Long processId)
    {
        return mesBaseProcessInfoMapper.selectMesBaseProcessInfoByProcessId(processId);
    }

    /**
     * 获取工序用户信息详细信息
     *
     * @param processId 工序信息主键
     * @return 工序信息
     */
    @Override
    public MesBaseProcessInfo selectMesBaseProcessInfoUserByProcessId(Long processId)
    {
        return mesBaseProcessInfoMapper.selectMesBaseProcessInfoUserByProcessId(processId);
    }

    /**
     * 查询工序信息列表
     * 
     * @param mesBaseProcessInfo 工序信息
     * @return 工序信息
     */
    @Override
    public List<MesBaseProcessInfo> selectMesBaseProcessInfoList(MesBaseProcessInfo mesBaseProcessInfo)
    {
        return mesBaseProcessInfoMapper.selectMesBaseProcessInfoList(mesBaseProcessInfo);
    }

    /**
     * 新增工序信息
     * 
     * @param mesBaseProcessInfo 工序信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesBaseProcessInfo(MesBaseProcessInfo mesBaseProcessInfo)
    {
        mesBaseProcessInfo.setCreateTime(DateUtils.getNowDate());
        int rows = mesBaseProcessInfoMapper.insertMesBaseProcessInfo(mesBaseProcessInfo);
        //insertMesBaseProcessProdline(mesBaseProcessInfo);
        return rows;
    }

    /**
     * 修改工序信息
     * 
     * @param mesBaseProcessInfo 工序信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesBaseProcessInfo(MesBaseProcessInfo mesBaseProcessInfo)
    {
        mesBaseProcessInfo.setUpdateTime(DateUtils.getNowDate());
        mesBaseProcessInfoMapper.deleteMesBaseProcessProdlineByProcessId(mesBaseProcessInfo.getProcessId());
        try {
            insertMesBaseProcessProdline(mesBaseProcessInfo);
        } catch (Exception e) {
            throw new ServiceException("该工序已关联此产线！");
        }
        return mesBaseProcessInfoMapper.updateMesBaseProcessInfo(mesBaseProcessInfo);
    }

    /**
     * 修改工序关联人员信息
     *
     * @param mesBaseProcessInfo 工序信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesBaseProcessInfoUser(MesBaseProcessInfo mesBaseProcessInfo)
    {
        mesBaseProcessInfo.setUpdateTime(DateUtils.getNowDate());
        mesBaseProcessInfoMapper.deleteMesBaseProcessUserByProcessId(mesBaseProcessInfo.getProcessId());
        try {
            insertMesBaseProcessUser(mesBaseProcessInfo);
        } catch (Exception e) {
            throw new ServiceException("该工序已关联此用户！");
        }
        return mesBaseProcessInfoMapper.updateMesBaseProcessInfo(mesBaseProcessInfo);
    }

    /**
     * 批量删除工序信息
     * 
     * @param processIds 需要删除的工序信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesBaseProcessInfoByProcessIds(Long[] processIds)
    {
        mesBaseProcessInfoMapper.deleteMesBaseProcessProdlineByProcessIds(processIds);
        return mesBaseProcessInfoMapper.deleteMesBaseProcessInfoByProcessIds(processIds);
    }

    /**
     * 删除工序信息信息
     * 
     * @param processId 工序信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesBaseProcessInfoByProcessId(Long processId)
    {
        mesBaseProcessInfoMapper.deleteMesBaseProcessProdlineByProcessId(processId);
        return mesBaseProcessInfoMapper.deleteMesBaseProcessInfoByProcessId(processId);
    }

    /**
     * 新增工序关联产线信息
     * 
     * @param mesBaseProcessInfo 工序信息对象
     */
    public void insertMesBaseProcessProdline(MesBaseProcessInfo mesBaseProcessInfo)
    {
        List<MesBaseProcessProdline> mesBaseProcessProdlineList = mesBaseProcessInfo.getMesBaseProcessProdlineList();
        Long processId = mesBaseProcessInfo.getProcessId();
        if (StringUtils.isNotNull(mesBaseProcessProdlineList))
        {
            List<MesBaseProcessProdline> list = new ArrayList<MesBaseProcessProdline>();
            for (MesBaseProcessProdline mesBaseProcessProdline : mesBaseProcessProdlineList)
            {
                mesBaseProcessProdline.setProcessId(processId);
                mesBaseProcessProdline.setCreateBy(SecurityUtils.getUsername());
                mesBaseProcessProdline.setCreateTime(DateUtils.getNowDate());
                list.add(mesBaseProcessProdline);
            }
            if (list.size() > 0)
            {
                mesBaseProcessInfoMapper.batchMesBaseProcessProdline(list);
            }
        }
    }

    /**
     * 新增工序关联人员信息
     *
     * @param mesBaseProcessInfo 工序信息对象
     */
    public void insertMesBaseProcessUser(MesBaseProcessInfo mesBaseProcessInfo)
    {
        List<MesBaseProcessUser> mesBaseProcessUserList = mesBaseProcessInfo.getMesBaseProcessUserList();
        Long processId = mesBaseProcessInfo.getProcessId();
        if (StringUtils.isNotNull(mesBaseProcessUserList))
        {
            List<MesBaseProcessUser> list = new ArrayList<>();
            for (MesBaseProcessUser mesBaseProcessUser : mesBaseProcessUserList)
            {
                mesBaseProcessUser.setProcessId(processId);
                list.add(mesBaseProcessUser);
            }
            if (list.size() > 0)
            {
                mesBaseProcessInfoMapper.batchMesBaseProcessUser(list);
            }
        }
    }
}
