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

import cn.hutool.core.bean.BeanUtil;
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.victoriatourist.entity.stock.dto.StockTransferDetailsDto;
import com.fjhx.victoriatourist.entity.stock.dto.StockTransferDetailsSelectDto;
import com.fjhx.victoriatourist.entity.stock.po.StockTransferDetails;
import com.fjhx.victoriatourist.entity.stock.vo.StockTransferDetailsVo;
import com.fjhx.victoriatourist.mapper.stock.StockTransferDetailsMapper;
import com.fjhx.victoriatourist.service.stock.StockTransferDetailsService;
import com.fjhx.wms.entity.stock.vo.StockVo;
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;


/**
 * <p>
 * 调仓明细 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-17
 */
@Service
public class StockTransferDetailsServiceImpl extends ServiceImpl<StockTransferDetailsMapper, StockTransferDetails> implements StockTransferDetailsService {
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    WarehouseService warehouseService;

    @Override
    public Page<StockTransferDetailsVo> getPage(StockTransferDetailsSelectDto dto) {
        IWrapper<StockTransferDetails> wrapper = getWrapper();
        wrapper.orderByDesc("std", StockTransferDetails::getId);
        Page<StockTransferDetailsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        //赋值产品信息
        productInfoService.attributeAssign(page.getRecords(), StockTransferDetailsVo::getProductId, (item, productInfo) -> {
            item.setProductCode(productInfo.getCode());
            item.setProductUnit(productInfo.getUnit());
            item.setProductName(productInfo.getName());
            item.setProductSpec(productInfo.getSpec());
        });
        //赋值仓库名称
        warehouseService.attributeAssign(page.getRecords(), StockTransferDetailsVo::getInWarehouseId, (item, warehouse) -> {
            item.setInWarehouseName(warehouse.getName());
        });
        warehouseService.attributeAssign(page.getRecords(), StockTransferDetailsVo::getOutWarehouseId, (item, warehouse) -> {
            item.setOutWarehouseName(warehouse.getName());
        });
        //赋值操作人名称
        UserUtil.assignmentNickName(page.getRecords(),StockTransferDetailsVo::getUpdateUser,StockTransferDetailsVo::setOperatorName);
        return page;
    }

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

    @Override
    public void add(StockTransferDetailsDto stockTransferDetailsDto) {
        this.save(stockTransferDetailsDto);
    }

    @Override
    public void edit(StockTransferDetailsDto stockTransferDetailsDto) {
        this.updateById(stockTransferDetailsDto);
    }

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

}
