package com.hw.mes.service.impl;

import java.util.HashMap;
import java.util.List;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.security.utils.SecurityUtils;
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.MesBaseRouteProcess;
import com.hw.mes.mapper.MesBaseRouteMapper;
import com.hw.mes.domain.MesBaseRoute;
import com.hw.mes.service.IMesBaseRouteService;

/**
 * 工艺路线Service业务层处理
 * 
 * @author Yinq
 * @date 2024-01-25
 */
@Service
public class MesBaseRouteServiceImpl implements IMesBaseRouteService 
{
    @Autowired
    private MesBaseRouteMapper mesBaseRouteMapper;

    /**
     * 查询工艺路线
     * 
     * @param routeId 工艺路线主键
     * @return 工艺路线
     */
    @Override
    public MesBaseRoute selectMesBaseRouteByRouteId(Long routeId)
    {
        return mesBaseRouteMapper.selectMesBaseRouteByRouteId(routeId);
    }

    /**
     * 查询工艺路线列表
     * 
     * @param mesBaseRoute 工艺路线
     * @return 工艺路线
     */
    @Override
    public List<MesBaseRoute> selectMesBaseRouteList(MesBaseRoute mesBaseRoute)
    {
        return mesBaseRouteMapper.selectMesBaseRouteList(mesBaseRoute);
    }

    /**
     * 新增工艺路线
     * 
     * @param mesBaseRoute 工艺路线
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesBaseRoute(MesBaseRoute mesBaseRoute)
    {
        mesBaseRoute.setCreateTime(DateUtils.getNowDate());
        mesBaseRoute.setCreateBy(SecurityUtils.getUsername());
        int rows = mesBaseRouteMapper.insertMesBaseRoute(mesBaseRoute);
        insertMesBaseRouteProcess(mesBaseRoute);
        return rows;
    }

    /**
     * 修改工艺路线
     * 
     * @param mesBaseRoute 工艺路线
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesBaseRoute(MesBaseRoute mesBaseRoute)
    {
        mesBaseRoute.setUpdateTime(DateUtils.getNowDate());
        mesBaseRoute.setUpdateBy(SecurityUtils.getUsername());
        mesBaseRouteMapper.deleteMesBaseRouteProcessByRouteId(mesBaseRoute.getRouteId());
        insertMesBaseRouteProcess(mesBaseRoute);
        return mesBaseRouteMapper.updateMesBaseRoute(mesBaseRoute);
    }

    /**
     * 批量删除工艺路线
     * 
     * @param routeIds 需要删除的工艺路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesBaseRouteByRouteIds(Long[] routeIds)
    {
        mesBaseRouteMapper.deleteMesBaseRouteProcessByRouteIds(routeIds);
        return mesBaseRouteMapper.deleteMesBaseRouteByRouteIds(routeIds);
    }

    /**
     * 删除工艺路线信息
     * 
     * @param routeId 工艺路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesBaseRouteByRouteId(Long routeId)
    {
        mesBaseRouteMapper.deleteMesBaseRouteProcessByRouteId(routeId);
        return mesBaseRouteMapper.deleteMesBaseRouteByRouteId(routeId);
    }

    /**
     * 新增工艺路线组成信息信息
     * 
     * @param mesBaseRoute 工艺路线对象
     */
    public void insertMesBaseRouteProcess(MesBaseRoute mesBaseRoute)
    {
        List<MesBaseRouteProcess> mesBaseRouteProcessList = mesBaseRoute.getMesBaseRouteProcessList();
        Long routeId = mesBaseRoute.getRouteId();
        if (StringUtils.isNotNull(mesBaseRouteProcessList))
        {
            List<MesBaseRouteProcess> list = new ArrayList<MesBaseRouteProcess>();
            for (MesBaseRouteProcess mesBaseRouteProcess : mesBaseRouteProcessList)
            {
                mesBaseRouteProcess.setRouteId(routeId);
                mesBaseRouteProcess.setCreateTime(DateUtils.getNowDate());
                mesBaseRouteProcess.setCreateBy(SecurityUtils.getUsername());
                list.add(mesBaseRouteProcess);
            }
            if (list.size() > 0)
            {
                mesBaseRouteMapper.batchMesBaseRouteProcess(list);
            }
        }
    }

    /**
     * 根据工艺路线ID查询工位信息
     * @param routeId
     * @return
     */
    @Override
    public List<HashMap<String, Object>> getStationByRouteId(Long routeId) {
        return mesBaseRouteMapper.getStationByRouteId(routeId);
    }
}
