package com.fjhx.wms.service.stock.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.item.entity.product.po.ProductInfo;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.wms.entity.stock.dto.StockJournalDto;
import com.fjhx.wms.entity.stock.dto.StockJournalSelectDto;
import com.fjhx.wms.entity.stock.po.StockJournal;
import com.fjhx.wms.entity.stock.vo.StockJournalVo;
import com.fjhx.wms.entity.stock.vo.StockVo;
import com.fjhx.wms.entity.warehouse.po.Warehouse;
import com.fjhx.wms.entity.warehouse.vo.WarehouseVo;
import com.fjhx.wms.mapper.stock.StockJournalMapper;
import com.fjhx.wms.service.stock.StockJournalService;
import com.fjhx.wms.service.warehouse.WarehouseService;
import com.obs.services.internal.ServiceException;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 出入库记录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-03-20
 */
@Service
public class StockJournalServiceImpl extends ServiceImpl<StockJournalMapper, StockJournal> implements StockJournalService {

    @Autowired
    ProductInfoService productInfoService;

    @Autowired
    WarehouseService warehouseService;

    @Override
    public Page<StockJournalVo> getPage(StockJournalSelectDto dto) {
        IWrapper<StockJournal> wrapper = getWrapper();
        wrapper.orderByDesc("sj", StockJournal::getCreateTime);
        if (ObjectUtils.isNotEmpty(dto.getType())) {
            String[] split = dto.getType().split(",");
            wrapper.in("sj", StockJournal::getType, split);
        }
        wrapper.eq("sj", StockJournal::getOpType, dto.getOpType());
        wrapper.like("w", WarehouseVo::getName, dto.getKeyword());
        Page<StockJournalVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<StockJournalVo> records = page.getRecords();
        //根据id赋值操作人名称
        UserUtil.assignmentNickName(records, StockJournalVo::getCreateUser, StockJournalVo::setUserName);
        return page;
    }

    @Override
    public StockJournalVo detail(Long id) {
        IWrapper<StockJournal> wrapper = getWrapper();
        wrapper.eq("sj", StockJournal::getId, id);
        StockJournalVo result = baseMapper.detail(wrapper);
        //查询商品信息
        List<StockVo> stockVos = result.getList();
        List<Long> productIds = stockVos.stream().map(StockVo::getProductId).collect(Collectors.toList());
        List<ProductInfo> productInfos = productInfoService.listByIds(productIds);

        Map<Long, ProductInfo> productInfoMap = productInfos.stream().collect(Collectors.groupingBy(ProductInfo::getId,
                Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))));
        for (StockVo stockVo : stockVos) {
            ProductInfo productInfo = productInfoMap.get(stockVo.getProductId());
            if (productInfo == null) {
                throw new ServiceException("未知商品商品id->" + stockVo.getProductId());
            }
            stockVo.setProductName(productInfo.getName());
        }

        //如果为调参记录
//        if (result.getType() == 3) {
//            //赋值目标仓库名称
//            Warehouse byId = warehouseService.getById(result.getToWarehouseId());
//            result.setToWarehouseName(byId.getName());
//        }

        return result;
    }

    @Override
    public void add(StockJournalDto stockJournalDto) {
        this.save(stockJournalDto);
    }

    @Override
    public void edit(StockJournalDto stockJournalDto) {
        this.updateById(stockJournalDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
