package cn.iocoder.yudao.module.tickets.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.project.entity.RepairWares;
import cn.iocoder.yudao.module.project.service.RepairWaresService;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.tickets.entity.DlTwItem;
import cn.iocoder.yudao.module.tickets.mapper.DlTwItemMapper;
import cn.iocoder.yudao.module.tickets.service.DlTwItemService;
import cn.iocoder.yudao.module.tickets.vo.DlTwItemReqVO;
import cn.iocoder.yudao.module.tickets.vo.DlTwItemRespVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

import static cn.iocoder.yudao.common.RepairCons.DICT_REPAIR_UNIT;

/**
 * 针对表【dl_tw_item(工单配件申请/退回子表)】的数据库操作Service实现
 *
 * @author 小李
 * @date 11:57 2024/10/15
**/
@Service
public class DlTwItemServiceImpl extends ServiceImpl<DlTwItemMapper, DlTwItem>
    implements DlTwItemService {

    @Resource
    private RepairWaresService repairWaresService;
    @Resource
    private DictDataApi dictDataApi;
    @Resource
    @Lazy
    private RepairWaresService waresService;

    /**
     * 根据主表查看全部
     *
     * @author 小李
     * @date 12:25 2024/10/15
     * @param reqVO 请求对象
     **/
    @Override
    public List<DlTwItemRespVO> listTwItem(DlTwItemReqVO reqVO){
        // 查记录
        List<DlTwItem> dlTwItems = baseMapper.listTwItem(reqVO);
        //所有有配件的分类
        List<Map<String,String>> typeMapList = waresService.getAllTypeList();
        Map<String,String> typeMap = new HashMap<>();
        for(Map<String,String> map:typeMapList){
            typeMap.put(map.get("id"),map.get("name"));
        }
        // 单位字典
        List<DictDataRespDTO> recordTypeList = dictDataApi.getDictDataList(DICT_REPAIR_UNIT);
        Map<String,String> unitMap = recordTypeList.stream().collect(Collectors.toMap(DictDataRespDTO::getValue,DictDataRespDTO::getLabel));
        // 查配件详情
        List<String> ids = dlTwItems.stream().map(DlTwItem::getWaresId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(ids)){
            List<RepairWares> repairWares = repairWaresService.listByIds(ids);
            return dlTwItems.stream().map(item -> {
                DlTwItemRespVO bean = BeanUtil.toBean(item, DlTwItemRespVO.class);
                repairWares.stream()
                        .filter(i -> i.getId().equals(bean.getWaresId()))
                        .findFirst()
                        .ifPresent(bean::setWares);
                bean.setTypeName(typeMap.get(bean.getWares().getType()));
                bean.getWares().setUnit(unitMap.get(bean.getWares().getUnit()));
                return bean;
            }).collect(Collectors.toList());
        }
        return null;
    }
}




