package com.microservices.pms.product.service.impl;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.pms.product.domain.PmsProductPriority;
import com.microservices.pms.product.domain.vo.PmsTypeDataVo;
import com.microservices.pms.product.mapper.PmsProductPriorityMapper;
import com.microservices.pms.product.service.IPmsProductPriorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 产品优先级Service业务层处理
 * 
 * @author otto
 * @date 2023-10-16
 */
@Service
public class PmsProductPriorityServiceImpl implements IPmsProductPriorityService 
{
    @Autowired
    private PmsProductPriorityMapper pmsProductPriorityMapper;

    /**
     * 查询产品优先级
     * 
     * @param id 产品优先级主键
     * @return 产品优先级
     */
    @Override
    public PmsTypeDataVo selectPmsProductPriorityById(Long id)
    {
        PmsProductPriority pmsProductPriority=pmsProductPriorityMapper.selectPmsProductPriorityById(id);
        if(pmsProductPriority==null){
            throw new ServiceException("该优先级不存在(优先级ID[%d])",id);
        }
        return pmsProductPriority.toPmsTypeDataVo();
    }

    /**
     * 查询产品优先级列表
     * 
     * @param pmsProductPriority 产品优先级
     * @return 产品优先级
     */
    @Override
    public List<PmsTypeDataVo> selectPmsProductPriorityList(PmsProductPriority pmsProductPriority)
    {
        List<PmsProductPriority> pmsProductPriorityList=pmsProductPriorityMapper.selectPmsProductPriorityList(pmsProductPriority);
        if(pmsProductPriorityList!=null){
            return pmsProductPriorityList.stream().map(PmsProductPriority::toPmsTypeDataVo).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 新增产品优先级
     * 
     * @param pmsProductPriority 产品优先级
     * @return 结果
     */
    @Override
    public int insertPmsProductPriority(PmsProductPriority pmsProductPriority)
    {
        pmsProductPriority.setCreateTime(DateUtils.getNowDate());
        return pmsProductPriorityMapper.insertPmsProductPriority(pmsProductPriority);
    }

    /**
     * 修改产品优先级
     * 
     * @param pmsProductPriority 产品优先级
     * @return 结果
     */
    @Override
    public int updatePmsProductPriority(PmsProductPriority pmsProductPriority)
    {
        pmsProductPriority.setUpdateTime(DateUtils.getNowDate());
        return pmsProductPriorityMapper.updatePmsProductPriority(pmsProductPriority);
    }

    /**
     * 批量删除产品优先级
     * 
     * @param ids 需要删除的产品优先级主键
     * @return 结果
     */
    @Override
    public int deletePmsProductPriorityByIds(Long[] ids)
    {
        return pmsProductPriorityMapper.deletePmsProductPriorityByIds(ids);
    }

    /**
     * 删除产品优先级信息
     * 
     * @param id 产品优先级主键
     * @return 结果
     */
    @Override
    public int deletePmsProductPriorityById(Long id)
    {
        return pmsProductPriorityMapper.deletePmsProductPriorityById(id);
    }

    @Override
    public Long selectPmsProductPriorityIdByInputId(Long priorityId, Integer type) {

        if(priorityId==null){
            PmsProductPriority pmsProductPriority=pmsProductPriorityMapper.selectPmsProductDefaultPriorityByType(type);
            return pmsProductPriority.getId();
        }
        PmsTypeDataVo pmsTypeDataVo=selectPmsProductPriorityById(priorityId);
        if(pmsTypeDataVo.getType().equals(type)){
            return pmsTypeDataVo.getId();
        }
        throw new ServiceException("该优先级不存在(优先级Id[%d])",priorityId);
    }
}
