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

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.business.domain.TpParaRelatedAction;
import com.ruoyi.project.business.domain.TpParaSelectItem;
import com.ruoyi.project.business.dto.TpParaRelatedActionDto;
import com.ruoyi.project.business.enums.InputTypeEnum;
import com.ruoyi.project.business.mapper.TpParaRelatedActionMapper;
import com.ruoyi.project.business.service.ITpParaRelatedActionService;
import com.ruoyi.project.business.service.ITpParaSelectItemService;
import org.springframework.stereotype.Service;

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

/**
 * 区域别梯型参数联动动作Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-08
 */
@Service
public class TpParaRelatedActionServiceImpl extends ServiceImpl<TpParaRelatedActionMapper, TpParaRelatedAction> implements ITpParaRelatedActionService {
    @Resource
    private TpParaRelatedActionMapper tpParaRelatedActionMapper;

    @Resource
    private ITpParaSelectItemService iTpParaSelectItemService;


    @Override
    public List<TpParaRelatedActionDto> queryMoreInfo(List<Long> caseIds) {
        List<TpParaRelatedActionDto> tpParaRelatedActionDtos = tpParaRelatedActionMapper.queryMoreInfo(caseIds);

        //为其中是下拉选项的 参数查询 选项值
        List<TpParaRelatedActionDto> selectInputType = tpParaRelatedActionDtos.stream().filter(item ->
                InputTypeEnum.SELECT.getCode().equals(item.getInputType()) || InputTypeEnum.INPUT_SELECT.getCode().equals(item.getInputType())
        ).collect(Collectors.toList());
        if(ObjectUtils.isNotEmpty(selectInputType)){
            List<Long> selectedIds = selectInputType.stream().map(TpParaRelatedActionDto::getParaId).collect(Collectors.toList());
            List<TpParaSelectItem> selectItems = iTpParaSelectItemService.list(
                    Wrappers.<TpParaSelectItem>lambdaQuery()
                            .in(TpParaSelectItem::getParaId, selectedIds)
            );
            Map<Long, List<TpParaSelectItem>> selectItemMap = selectItems.stream().collect(Collectors.groupingBy(TpParaSelectItem::getParaId));
            tpParaRelatedActionDtos.forEach(item -> {
                if (selectItemMap.containsKey(item.getParaId())) {
                    item.setOptions(selectItemMap.get(item.getParaId()));
                }
            });
        }

        return tpParaRelatedActionDtos;
    }

    @Override
    public void clearAll() {
        baseMapper.clearAll();
    }
}
