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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.common.RepairErrorCodeConstants;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
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.service.DlRepairStiService;
import cn.iocoder.yudao.module.stockTransfer.vo.DlRepairStReqVO;
import cn.iocoder.yudao.module.stockTransfer.vo.DlRepairStRespVO;
import cn.iocoder.yudao.module.stockTransfer.vo.DlRepairStiQueryVO;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
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 cn.iocoder.yudao.module.stockTransfer.service.DlRepairStService;
import cn.iocoder.yudao.module.stockTransfer.mapper.DlRepairStMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 针对表【dl_repair_st(调拨单)】的数据库操作Service实现
 *
 * @author 小李
 * @date 17:23 2024/9/13
 **/
@Service
public class DlRepairStServiceImpl extends ServiceImpl<DlRepairStMapper, DlRepairSt>
        implements DlRepairStService {

    @Resource
    private DlRepairStiService repairStiService;

    @Resource
    private RepairWaresService waresService;

    /**
     * 调拨单 新增
     *
     * @param repairStRespVO 新增对象
     * @author 小李
     * @date 19:06 2024/9/18
     **/
    @Override
    @DSTransactional
    public void createSt(DlRepairStRespVO repairStRespVO) {
        // 获取当前登录用户的部门ID
        Long deptId = SecurityFrameworkUtils.getLoginUserDeptId();
        repairStRespVO.setDeptId(deptId);
        // 新增调拨单
        baseMapper.insert(repairStRespVO);
        // 新增调拨单子表
        if (CollectionUtil.isEmpty(repairStRespVO.getGoodsList())) {
            throw exception(RepairErrorCodeConstants.GOODS_IS_EMPTY);
        }
        repairStRespVO.getGoodsList().forEach(item -> item.setStId(repairStRespVO.getId()));
        repairStiService.saveBatch(repairStRespVO.getGoodsList());
    }

    /**
     * 调拨单（出库、入库） 分页
     *
     * @author 小李
     * @date 9:20 2024/9/19
     * @param repairStReqVO 查询对象
     **/
    @Override
    public IPage<DlRepairSt> getStPage(DlRepairStReqVO repairStReqVO, Page<DlRepairSt> page){
        return baseMapper.getStPage(repairStReqVO, page);
    }

    /**
     * 调拨单（出库、入库） 作废
     *
     * @author 小李
     * @date 15:11 2024/9/22
     * @param repairStReqVO 请求对象
     **/
    @Override
    public void voidSt(DlRepairStReqVO repairStReqVO){
        baseMapper.updateById(repairStReqVO);
    }

    /**
     * 调拨单（出库、入库） 详情查询
     *
     * @author 小李
     * @date 15:52 2024/9/22
     * @param id 查询ID
     **/
    @Override
    public DlRepairStRespVO getSt(String id){
        // 查主表
        DlRepairSt dlRepairSt = baseMapper.selectById(id);
        DlRepairStRespVO result = BeanUtil.toBean(dlRepairSt, DlRepairStRespVO.class);
        // 查子表
        List<DlRepairSti> repairStis = repairStiService.list(new LambdaQueryWrapper<DlRepairSti>().eq(DlRepairSti::getStId, result.getId()));
        Set<String> goodIds = repairStis.stream().map(DlRepairSti::getGoodsId).collect(Collectors.toSet());
        List<DlRepairStiQueryVO> items = repairStis.stream().map(item -> BeanUtil.toBean(item, DlRepairStiQueryVO.class)).collect(Collectors.toList());
        // 查商品
        List<RepairWares> repairWares = waresService.listByIds(goodIds);
        // 映射
        items.forEach(item -> {
            repairWares.stream().filter(i -> i.getId().equals(item.getGoodsId())).findFirst().ifPresent(item::setWares);
        });
        result.setItems(items);
        return result;
    }
}




