package cn.iocoder.yudao.module.stockOperate.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.stockOperate.entity.DlRepairSo;
import cn.iocoder.yudao.module.stockOperate.entity.DlRepairSoi;
import cn.iocoder.yudao.module.stockOperate.mapper.DlRepairSoiMapper;
import cn.iocoder.yudao.module.stockOperate.service.DlRepairSoService;
import cn.iocoder.yudao.module.stockOperate.service.DlRepairSoiService;
import cn.iocoder.yudao.module.stockOperate.vo.DlRepairSoiQueryRespVO;
import cn.iocoder.yudao.module.stockOperate.vo.DlRepairSoiReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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_soi(采购单领料单子表)】的数据库操作Service实现
 *
 * @author 小李
 * @date 9:10 2024/9/13
 **/
@Service
public class DlRepairSoiServiceImpl extends ServiceImpl<DlRepairSoiMapper, DlRepairSoi>
        implements DlRepairSoiService {

    @Resource
    private RepairWaresService waresService;

    @Resource
    @Lazy
    private DlRepairSoService soService;

    /**
     * 采购单领料单子表 分页
     *
     * @param repairSoiReqVO 查询对象
     * @author 小李
     * @date 10:44 2024/9/18
     **/
    @Override
    public IPage<DlRepairSoi> getRepairSoiPage(DlRepairSoiReqVO repairSoiReqVO, Page<DlRepairSoi> page) {
        return baseMapper.getRepairSoiPage(repairSoiReqVO, page);
    }

    /**
     * 采购单领料单子表详细 按ID查
     *
     * @param ids ids
     * @author 小李
     * @date 11:24 2024/9/22
     **/
    @Override
    public List<DlRepairSoiQueryRespVO> getRepairSoiByIds(List<String> ids) {
        List<DlRepairSoi> repairSois = baseMapper.selectBatchIds(ids);
        List<DlRepairSoiQueryRespVO> result = repairSois.stream()
                .map(item -> BeanUtil.toBean(item, DlRepairSoiQueryRespVO.class))
                .collect(Collectors.toList());
        List<String> goodsIds = result.stream().map(DlRepairSoiQueryRespVO::getGoodsId).collect(Collectors.toList());
        List<RepairWares> repairWares = waresService.listByIds(goodsIds);
        result.forEach(item -> {
            List<RepairWares> wares = repairWares.stream().filter(i -> i.getId().equals(item.getGoodsId())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(wares)){
                item.setRepairWares(wares.get(0));
            }
        });

        // 反查主表（时间紧迫，直接先这样）
        Set<String> soIds = repairSois.stream().map(DlRepairSoi::getSoId).collect(Collectors.toSet());
        List<DlRepairSo> dlRepairSos = soService.listByIds(soIds);
        result.forEach(item -> {
            dlRepairSos.stream().filter(i -> i.getId().equals(item.getSoId())).findFirst().ifPresent(item::setSo);
        });
        return result;
    }


    /**
     * 按主表ID查
     *
     * @author 小李
     * @date 11:22 2024/10/21
     * @param soId 主表ID
     **/
    @Override
    public List<DlRepairSoiQueryRespVO> getRepairSoiBySoId(String soId){
        List<DlRepairSoi> repairSois = baseMapper.selectList(new LambdaQueryWrapper<DlRepairSoi>().eq(DlRepairSoi::getSoId, soId));
        List<DlRepairSoiQueryRespVO> result = repairSois.stream()
                .map(item -> BeanUtil.toBean(item, DlRepairSoiQueryRespVO.class))
                .collect(Collectors.toList());
        List<String> goodsIds = result.stream().map(DlRepairSoiQueryRespVO::getGoodsId).collect(Collectors.toList());
        List<RepairWares> repairWares = waresService.listByIds(goodsIds);
        result.forEach(item -> {
            List<RepairWares> wares = repairWares.stream().filter(i -> i.getId().equals(item.getGoodsId())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(wares)){
                item.setRepairWares(wares.get(0));
            }
        });
        return result;
    }

    /**
     * 根据关联配件id查询关联配件
     *
     * @param ids 配件id集合
     * @return java.util.List<cn.iocoder.yudao.module.stockOperate.entity.DlRepairSoi>
     * @author PQZ
     * @date 14:39 2024/10/24
     **/
    @Override
    public List<DlRepairSoi> listByIds(List<String> ids) {
        return baseMapper.selectBatchIds(ids);
    }
}




