package com.ruoyi.project.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.business.domain.TpParaSelectItem;
import com.ruoyi.project.business.mapper.TpParaSelectItemMapper;
import com.ruoyi.project.business.service.ITpParaSelectItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 参数选项Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-23
 */
@Service
public class TpParaSelectItemServiceImpl extends ServiceImpl<TpParaSelectItemMapper, TpParaSelectItem> implements ITpParaSelectItemService {
    @Resource
    private TpParaSelectItemMapper tpParaSelectItemMapper;

    /**
     * 查询参数选项
     *
     * @param id 参数选项主键
     * @return 参数选项
     */
    @Override
    public TpParaSelectItem selectTpParaSelectItemById(Long id) {
        return tpParaSelectItemMapper.selectTpParaSelectItemById(id);
    }

    /**
     * 查询参数选项列表
     *
     * @param tpParaSelectItem 参数选项
     * @return 参数选项
     */
    @Override
    public List<TpParaSelectItem> selectTpParaSelectItemList(TpParaSelectItem tpParaSelectItem) {
        return tpParaSelectItemMapper.selectTpParaSelectItemList(tpParaSelectItem);
    }

    /**
     * 新增参数选项
     *
     * @param tpParaSelectItem 参数选项
     * @return 结果
     */
    @Override
    public int insertTpParaSelectItem(TpParaSelectItem tpParaSelectItem) {
        TpParaSelectItem selector = new TpParaSelectItem();
        selector.setParaId(tpParaSelectItem.getParaId());
        //获取所有参数选项转化为MAP
        List<TpParaSelectItem> paraSelectItemList = tpParaSelectItemMapper.selectTpParaSelectItemList(selector);
        Map<String, String> paraMap = paraSelectItemList.stream().collect(Collectors.toMap(TpParaSelectItem::getValue, TpParaSelectItem::getLabel, (key1, key2) -> key1));

        if (paraMap.containsKey(tpParaSelectItem.getValue())) {
            throw new ServiceException("该当选项值已存在");
        }
        Date nowDate = DateUtils.getNowDate();
        tpParaSelectItem.setCreateBy(SecurityUtils.getUserId().toString());
        tpParaSelectItem.setCreateTime(nowDate);
        tpParaSelectItem.setUpdateBy(SecurityUtils.getUserId().toString());
        tpParaSelectItem.setUpdateTime(nowDate);
        return tpParaSelectItemMapper.insertTpParaSelectItem(tpParaSelectItem);
    }

    /**
     * 修改参数选项
     *
     * @param tpParaSelectItem 参数选项
     * @return 结果
     */
    @Override
    public int updateTpParaSelectItem(TpParaSelectItem tpParaSelectItem) {
        TpParaSelectItem selector = new TpParaSelectItem();
        selector.setParaId(tpParaSelectItem.getParaId());
        //获取所有参数选项转化为MAP
        List<TpParaSelectItem> paraSelectItemList = tpParaSelectItemMapper.selectTpParaSelectItemList(selector);
        List<TpParaSelectItem> newparaSelectItemList = paraSelectItemList.stream().filter(it-> !tpParaSelectItem.getId().equals(it.getId())).collect(Collectors.toList());
        Map<String, String> paraMap = newparaSelectItemList.stream().collect(Collectors.toMap(TpParaSelectItem::getValue, TpParaSelectItem::getLabel, (key1, key2) -> key1));
        if (paraMap.containsKey(tpParaSelectItem.getValue())) {
            //需排除自己
            throw new ServiceException("该选项值已存在");
        }
        Date nowDate = DateUtils.getNowDate();
        tpParaSelectItem.setUpdateBy(SecurityUtils.getUserId().toString());
        tpParaSelectItem.setUpdateTime(nowDate);
        return tpParaSelectItemMapper.updateTpParaSelectItem(tpParaSelectItem);
    }

    /**
     * 批量删除参数选项
     *
     * @param ids 需要删除的参数选项主键
     * @return 结果
     */
    @Override
    public int deleteTpParaSelectItemByIds(Long[] ids) {
        return tpParaSelectItemMapper.deleteTpParaSelectItemByIds(ids);
    }

    /**
     * 删除参数选项信息
     *
     * @param id 参数选项主键
     * @return 结果
     */
    @Override
    public int deleteTpParaSelectItemById(Long id) {
        return tpParaSelectItemMapper.deleteTpParaSelectItemById(id);
    }
}
