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

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.module.project.entity.RepairWares;
import cn.iocoder.yudao.module.project.service.RepairWaresService;
import cn.iocoder.yudao.module.stockTransfer.entity.DlRepairSt;
import cn.iocoder.yudao.module.stockTransfer.entity.DlRepairSti;
import cn.iocoder.yudao.module.stockTransfer.mapper.DlRepairStiMapper;
import cn.iocoder.yudao.module.stockTransfer.service.DlRepairStService;
import cn.iocoder.yudao.module.stockTransfer.service.DlRepairStiService;
import cn.iocoder.yudao.module.stockTransfer.vo.DlRepairStiQueryVO;
import cn.iocoder.yudao.module.stockTransfer.vo.DlRepairStiReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 针对表【dl_repair_sti(调拨单子表)】的数据库操作Service实现
 *
 * @author 小李
 * @date 17:22 2024/9/13
 **/
@Service
public class DlRepairStiServiceImpl extends ServiceImpl<DlRepairStiMapper, DlRepairSti>
        implements DlRepairStiService {

    @Resource
    private RepairWaresService waresService;

    @Resource
    @Lazy
    private DlRepairStService stService;

    /**
     * 调拨单子表 分页
     *
     * @author 小李
     * @date 14:48 2024/9/19
     * @param repairStiReqVO 查询对象
     **/
    @Override
    public IPage<DlRepairSti> getStiPage(DlRepairStiReqVO repairStiReqVO, Page<DlRepairSti> page){
        return baseMapper.getStiPage(repairStiReqVO, page);
    }

    /**
     * 调拨单子表 配件详情
     *
     * @author 小李
     * @date 18:33 2024/9/22
     * @param ids ids
     **/
    @Override
    public List<DlRepairStiQueryVO> getStIsWares(List<String> ids){
        // 查子表
        List<DlRepairSti> dlRepairStis = baseMapper.selectBatchIds(ids);
        List<DlRepairStiQueryVO> result = dlRepairStis.stream().map(item -> BeanUtil.toBean(item, DlRepairStiQueryVO.class)).collect(Collectors.toList());
        // 查配件
        Set<String> goodsIds = result.stream().map(DlRepairStiQueryVO::getGoodsId).collect(Collectors.toSet());
        List<RepairWares> wares = waresService.listByIds(goodsIds);
        // 查主表
        Set<String> stIds = result.stream().map(DlRepairStiQueryVO::getStId).collect(Collectors.toSet());
        List<DlRepairSt> dlRepairSts = stService.listByIds(stIds);
        // 映射
        result.forEach(item -> {
            wares.stream().filter(i -> i.getId().equals(item.getGoodsId())).findFirst().ifPresent(item::setWares);
            dlRepairSts.stream().filter(i -> i.getId().equals(item.getStId())).findFirst().ifPresent(item::setSt);
        });
        return result;
    }

}




