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.entity.product.po.ProductInfo;
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.entity.stock.vo.StockWaitVo;
import com.fjhx.wms.mapper.stock.StockJournalDetailsMapper;
import com.fjhx.wms.service.stock.StockJournalDetailsService;
import com.fjhx.wms.service.stock.StockWaitService;
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.Objects;
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 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.eq("w.id", dto.getWarehouseId());
        wrapper.eq("sj.to_warehouse_id", dto.getToWarehouseId());
        wrapper.eq("sjd.product_id", dto.getProductId());
        wrapper.orderByDesc("sj", StockJournal::getCreateTime);

        if (ObjectUtil.isNotEmpty(dto.getKeyword())) {
            //产品编号过滤
            List<Long> productIds = productInfoService.listObject(ProductInfo::getId, q -> q.like(ProductInfo::getCustomCode, dto.getKeyword()).or()
                    .like(ProductInfo::getName, dto.getKeyword()));
            wrapper.and(q -> q.like("w.name", dto.getKeyword()).or()
                    .like("sj.link_code", dto.getKeyword()).or()
                    .in("sjd.product_id", productIds)
            );
        }

        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);
        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().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());
            }
        }
        //给 到货质检 赋值关联单号
        List<Long> qIds = records.stream().map(StockJournalDetailsVo::getBusinessId).distinct().filter(Objects::nonNull).collect(Collectors.toList());
        IWrapper<StockWait> wrapper = IWrapper.getWrapper();
        wrapper.in("qi", StockWait::getId, qIds);
        List<StockWaitVo> stockWaitInfo = baseMapper.getStockWaitInfo(wrapper);
        Map<Long, String> stockWaitInfoMap = stockWaitInfo.stream().collect(Collectors.toMap(StockWait::getId, StockWait::getBusinessCode));
        for (StockJournalDetailsVo record : records) {
            String linkCode = stockWaitInfoMap.get(record.getBusinessId());
            if (ObjectUtil.isEmpty(linkCode)) {
                continue;
            }
            record.setLinkCode(linkCode);
        }
        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);
    }

}
