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

import cn.hutool.core.bean.BeanUtil;
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.common.utils.Assert;
import com.fjhx.customer.entity.customer.po.Customer;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.supply.entity.supplier.po.SupplierInfo;
import com.fjhx.supply.service.supplier.SupplierInfoService;
import com.fjhx.wms.entity.arrival.po.ArrivalDetailPo;
import com.fjhx.wms.entity.contract.po.ContractPo;
import com.fjhx.wms.entity.purchase.po.PurchaseDetailPo;
import com.fjhx.wms.entity.purchase.po.PurchasePo;
import com.fjhx.wms.entity.stock.dto.StockJournalDto;
import com.fjhx.wms.entity.stock.dto.StockJournalSelectDto;
import com.fjhx.wms.entity.stock.emums.JournalType;
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.po.StockWaitDetails;
import com.fjhx.wms.entity.stock.vo.StockJournalDetailsVo;
import com.fjhx.wms.entity.stock.vo.StockJournalVo;
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.MyPurchaseService;
import com.fjhx.wms.service.MySaleService;
import com.fjhx.wms.service.stock.StockJournalDetailsService;
import com.fjhx.wms.service.stock.StockJournalService;
import com.fjhx.wms.service.stock.StockWaitDetailsService;
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;


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

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private StockJournalDetailsService stockJournalDetailsService;
    @Autowired
    private StockWaitService stockWaitService;
    @Autowired
    private MyPurchaseService myPurchaseService;
    @Autowired
    private SupplierInfoService supplierInfoService;
    @Autowired
    private MySaleService mySaleService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private StockWaitDetailsService stockWaitDetailsService;

    @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);
        //如果为调参类型记录赋值目标仓库名称
//        warehouseService.attributeAssign(records, StockJournalVo::getToWarehouseId, (item, warehouse) -> {
//            item.setToWarehouseName(warehouse.getName());
//        });
        return page;
    }

    @Override
    public StockJournalVo detail(Long id) {
        StockJournal stockJournal = this.getById(id);
        StockJournalVo stockJournalVo = BeanUtil.copyProperties(stockJournal, StockJournalVo.class);
        //赋值仓库名称
        Warehouse warehouse = warehouseService.getById(stockJournalVo.getWarehouseId());
        if (ObjectUtils.isNotEmpty(warehouse)) {
            stockJournalVo.setWarehouseName(warehouse.getName());
        }
        //查询明细
        List<StockJournalDetails> stockJournalDetailsList = stockJournalDetailsService.list(q -> q.eq(StockJournalDetails::getStockJournalId, id));
        List<StockJournalDetailsVo> stockJournalDetailsVos = BeanUtil.copyToList(stockJournalDetailsList, StockJournalDetailsVo.class);
        //赋值产品信息
        productInfoService.attributeAssign(stockJournalDetailsVos, StockJournalDetailsVo::getProductId, (item, productInfo) -> {
            item.setProductCode(productInfo.getCode());
            item.setProductUnit(productInfo.getUnit());
            item.setProductType(productInfo.getType());
            item.setProductName(productInfo.getName());
            item.setProductSpec(productInfo.getSpec());
        });
        stockJournalVo.setList(stockJournalDetailsVos);

        //流水打印数据赋值
        if (JournalType.PURCHASE_ARRIVAL_IN.getDetailType().equals(stockJournalVo.getType())) {
            StockWait stockWait = stockWaitService.getById(stockJournalVo.getBusinessId());
            Assert.notEmpty(stockWait, "查询不到待出入库信息");
            PurchasePo purchase = myPurchaseService.getPurchaseInfo(stockWait.getBusinessId());
            Assert.notEmpty(purchase, "查询不到采购信息");
            Map<Long, PurchaseDetailPo> purchaseDetailMap = myPurchaseService.grtPurchaseDetail(purchase.getId());
            //流水打印数据获取
            if (ObjectUtils.isEmpty(purchase.getDataResource())) {
                //采购合同
                SupplierInfo supplierInfo = supplierInfoService.getById(purchase.getSupplyId());
                if (ObjectUtils.isNotEmpty(supplierInfo)) {
                    stockJournalVo.setCorporateName(supplierInfo.getName());//供应商
                }
                stockJournalVo.setCode(purchase.getCode());//单号
            } else {
                //销售合同
                ContractPo contractInfo = mySaleService.getContractInfo(purchase.getDataResourceId());
                Customer customer = customerService.getById(contractInfo.getBuyCorporationId());
                if (ObjectUtils.isNotEmpty(customer)) {
                    stockJournalVo.setCorporateName(customer.getName());//客户名称
                }
                stockJournalVo.setCode(contractInfo.getCode());//单号
            }
            stockJournalVo.setCurrency(purchase.getCurrency());//币种
            //明细数据赋值
            for (StockJournalDetailsVo stockJournalDetailsVo : stockJournalVo.getList()) {
                stockJournalDetailsVo.setCurrency(purchase.getCurrency());//币种
                StockWaitDetails stockWaitDetails = stockWaitDetailsService.getById(stockJournalDetailsVo.getBusinessDetailsId());
                if (ObjectUtils.isEmpty(stockWaitDetails)) {
                    continue;
                }
                ArrivalDetailPo arrivalDetailInfo = myPurchaseService.getArrivalDetailInfo(stockWaitDetails.getBusinessDetailsId());
                if (ObjectUtils.isEmpty(arrivalDetailInfo)) {
                    continue;
                }
                PurchaseDetailPo purchaseDetail = purchaseDetailMap.get(arrivalDetailInfo.getPurchaseDetailId());
                if (ObjectUtils.isEmpty(purchaseDetail)) {
                    continue;
                }
                stockJournalDetailsVo.setPrice(purchaseDetail.getPrice());//单价
                stockJournalDetailsVo.setAmount(purchaseDetail.getAmount());//金额
            }
        }

        return stockJournalVo;
    }

    @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);
    }

}
