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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.customer.service.customer.CustomerService;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.supply.service.supplier.SupplierInfoService;
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.po.StockJournalDetails;
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.SecurityUtils;
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;


/**
 * <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();

        //根据类型过滤
        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());

        //是否归还过滤
        Integer isRestitution = dto.getIsRestitution();
        if (ObjectUtil.isNotEmpty(isRestitution)) {
            if (ObjectUtil.equals(isRestitution, 0)) {
                wrapper.isNull("sj.restitution_id");
            } else {
                wrapper.isNotNull("sj.restitution_id");
            }
        }

        //是否寻回过滤
        Integer isSeek = dto.getIsSeek();
        if (ObjectUtil.isNotEmpty(isSeek)) {
            if (ObjectUtil.equals(isSeek, 0)) {
                wrapper.isNull("sj.seek_id");
            } else {
                wrapper.isNotNull("sj.seek_id");
            }
        }

        //权限过滤：出入库流水
        wrapper.in("sj.company_id", SecurityUtils.getCompanyIds());
        wrapper.eq("sj.company_id", dto.getCompanyId());


        wrapper.orderByDesc("sj", StockJournal::getCreateTime);
        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) {
        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.getCustomCode());
            item.setProductUnit(productInfo.getUnit());
            item.setProductType(productInfo.getType());
            item.setProductName(productInfo.getName());
            item.setProductSpec(productInfo.getSpec());

            item.setProductLength(productInfo.getLength());
            item.setProductWidth(productInfo.getWidth());
            item.setProductHeight(productInfo.getHeight());
        });
        stockJournalVo.setList(stockJournalDetailsVos);

//        //流水打印数据赋值
//        if (JournalType.PURCHASE_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);
    }

}
