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.conf.entity.BaseType;
import cn.iocoder.yudao.module.conf.service.BaseTypeService;
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 cn.iocoder.yudao.module.stockOperate.vo.DlRepairSoiRespVO;
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 com.sun.xml.bind.v2.TODO;
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.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;

    @Resource
    private BaseTypeService typeService;

    /**
     * 采购单领料单子表 分页
     *
     * @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);
    }

    /**
     * 根据主表的ID得到子表的所有数据，按类型区分的map，支持条件查询
     *
     * @author 小李
     * @date 15:51 2024/11/19
     * @param id id
     **/
    @Override
    public Map<String, List<DlRepairSoiRespVO>> getMapBySoIdAndQuery(String id, String query){
        // 查出该单据的子项和其对应的配件信息
        List<DlRepairSoiRespVO> respVOList = baseMapper.getMapBySoIdAndQuery(id, query);
        if (CollectionUtil.isEmpty(respVOList)){
            return null;
        }
        // 按配件信息的type进行分组
        Map<String, List<DlRepairSoiRespVO>> map = respVOList.stream().collect(Collectors.groupingBy(item -> {
            String key = item.getWares().getType();
            if (key == null || key.isEmpty()) {
                key = "default";
            }
            return key;
        }));
        // 查分类的信息
        List<BaseType> baseTypes = typeService.listByIds(map.keySet());
        Map<String, String> typeMap = baseTypes.stream().collect(Collectors.toMap(BaseType::getId, BaseType::getName));
        // 映射
        Map<String, List<DlRepairSoiRespVO>> result = new HashMap<>();
        for (Map.Entry<String, List<DlRepairSoiRespVO>> entry : map.entrySet()) {
            String key = entry.getKey();
            String newKey = typeMap.get(key);
            if (newKey == null || newKey.isEmpty()) {
                newKey = "未分类";
            }
            result.put(newKey, entry.getValue());
        }
        return result;
    }

    /**
     * 根据供应商查询该供应商采购过的配件
     *
     * @author 小李
     * @date 11:49 2024/11/28
     * @param reqVO 查询对象
     **/
    @Override
    public IPage<DlRepairSoiRespVO> getSoBySupplier(DlRepairSoiReqVO reqVO, Page<DlRepairSoiRespVO> page){
        return baseMapper.getSoBySupplier(reqVO, page);
    }
}




