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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.wms.entity.stock.dto.StockJournalDetailsDto;
import com.fjhx.wms.entity.stock.dto.StockJournalSelectDto;
import com.fjhx.wms.entity.stock.po.StockJournal;
import com.fjhx.wms.entity.stock.po.StockJournalDetails;
import com.fjhx.wms.entity.stock.po.StockWait;
import com.fjhx.wms.entity.stock.vo.StockJournalDetailsVo;
import com.fjhx.wms.mapper.stock.StockJournalDetailsMapper;
import com.fjhx.wms.service.stock.StockJournalDetailsService;
import com.fjhx.wms.service.stock.StockWaitService;
import com.fjhx.wms.service.warehouse.WarehouseService;
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 StockJournalDetailsServiceImpl extends ServiceImpl<StockJournalDetailsMapper, StockJournalDetails> implements StockJournalDetailsService {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private StockWaitService stockWaitService;

    @Override
    public Page<StockJournalDetailsVo> getPage(StockJournalSelectDto dto) {
        IWrapper<StockJournalDetails> wrapper = getWrapper();
        wrapper.eq("sj.op_type", dto.getOpType());
        wrapper.eq("sj.type", dto.getType());
        wrapper.like("w.name", dto.getKeyword());
        wrapper.eq("w.id", dto.getWarehouseId());
        wrapper.eq("sj.warehouse_id", dto.getWarehouseId());
        wrapper.eq("sjd.product_id", dto.getProductId());
        wrapper.orderByDesc("sj", StockJournal::getCreateTime);
        Page<StockJournalDetailsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<StockJournalDetailsVo> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return page;
        }
        // 赋值产品属性
        productInfoService.attributeAssign(records, StockJournalDetailsVo::getProductId, (item, product) -> {
            item.setProductCode(product.getCode());
            item.setProductUnit(product.getUnit());
            item.setProductType(product.getType());
            item.setProductName(product.getName());
            item.setProductSpec(product.getSpec());
            item.setProductCustomCode(product.getCustomCode());
        });
        //赋值操作人名称
        UserUtil.assignmentNickName(records, StockJournalDetailsVo::getCreateUser, StockJournalDetailsVo::setOpUserName);
        //赋值目标仓库名称
        warehouseService.attributeAssign(records, StockJournalDetailsVo::getToWarehouseId, (item, warehouse) -> {
            item.setToWarehouseName(warehouse.getName());
        });
        return page;
    }

    @Override
    public Page<StockJournalDetailsVo> getPageByWdly(StockJournalSelectDto dto) {
        Page<StockJournalDetailsVo> page = getPage(dto);
        List<StockJournalDetailsVo> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return page;
        }
        //给采购退货的数据赋值关联单号
        List<Long> swIds = records.stream().filter(x -> x.getType() == 13).map(StockJournalDetailsVo::getBusinessId).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(swIds)) {
            Map<Long, StockWait> stockWaitMap = stockWaitService.mapKEntity(StockWait::getId, q -> q.in(StockWait::getId, swIds));
            for (StockJournalDetailsVo record : records) {
                StockWait stockWait = stockWaitMap.get(record.getBusinessId());
                if(ObjectUtil.isEmpty(stockWait)){
                    continue;
                }
                record.setLinkCode(stockWait.getBusinessCode());
            }
        }
        return page;
    }

    @Override
    public StockJournalDetailsVo detail(Long id) {
        StockJournalDetails StockJournalDetails = this.getById(id);
        StockJournalDetailsVo result = BeanUtil.toBean(StockJournalDetails, StockJournalDetailsVo.class);
        return result;
    }

    @Override
    public void add(StockJournalDetailsDto stockJournalDetailsDto) {
        this.save(stockJournalDetailsDto);
    }

    @Override
    public void edit(StockJournalDetailsDto stockJournalDetailsDto) {
        this.updateById(stockJournalDetailsDto);
    }

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

}
