package com.hys.app.service.erp.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.mybatisplus.base.BaseServiceImpl;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.framework.util.BeanUtils;
import com.hys.app.mapper.erp.ProductStockMapper;
import com.hys.app.model.erp.dos.*;
import com.hys.app.model.erp.enums.ScanFormCodeTypeEnum;
import com.hys.app.model.erp.enums.SnCodeStatusEnum;
import com.hys.app.model.erp.vo.GoodsSnVO;
import com.hys.app.model.erp.vo.GoodsVO;
import com.hys.app.model.erp.vo.ProductStockVO;
import com.hys.app.model.erp.vo.ProductVO;
import com.hys.app.service.erp.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 企业业务层实现
 *
 * @author 张崧
 * 2024-03-25 15:44:10
 */
@Service
@AllArgsConstructor
public class ScanCodeManagerImpl extends BaseServiceImpl<ProductStockMapper, ProductStockDO> implements ScanCodeManager {

    private OrderSendManager orderSendManager;
    private OrderItemManager orderItemManager;
    private OrderManager orderManager;
    private WarehouseOutManager warehouseOutManager;
    private WarehouseOutItemManager warehouseOutItemManager;
    private StockDamageReportManager stockDamageReportManager;
    private StockDamageReportProductManager stockDamageReportProductManager;
    private StockInventoryManager stockInventoryManager;
    private StockInventoryProductManager stockInventoryProductManager;
    private OrderReturnManager orderReturnManager;
    private OrderReturnItemManager orderReturnItemManager;
    private StockTransferManager stockTransferManager;
    private StockTransferProductManager stockTransferProductManager;
    private WarehouseEntryManager warehouseEntryManager;
    private WarehouseEntryProductManager warehouseEntryProductManager;
    private ProductStockManager productStockManager;
    private GoodsManager goodsManager;
    private ProductManager productManager;
    private GoodsSnManager goodsSnManager;


    @Override
    public Map<String, Object> getListByScan(String orderCode) {
        Map<String, Object> resMap = new HashMap<>();
        List<ProductStockVO> list = new ArrayList<>();

        //自定义的订单号长度都是8为以上，前面8为使我们自定义的用来匹配
        if (orderCode.length() < 8) {
           throw new ServiceException("该单号未匹配到商品");
        }
        String substring = orderCode.substring(0, 8);
        if (!EnumUtil.contains(ScanFormCodeTypeEnum.class, substring)) {
            throw new ServiceException("该单号未匹配到商品");
        }
//        //发货单
//        if (ScanFormCodeTypeEnum.RYKS_FHD.getValue().equals(substring)) {
//            OrderSendDO sendDO = orderSendManager.getOne(new LambdaQueryWrapper<OrderSendDO>().eq(OrderSendDO::getSn, orderCode).last("limit 1"));
//            if (ObjectUtil.isNull(sendDO)) {
//                return resMap;
//            }
//            List<OrderItemDO> orderItemList = orderItemManager.list(new LambdaQueryWrapper<OrderItemDO>().eq(OrderItemDO::getOrderSendId, sendDO.getId()));
//            List<Long> collect = orderItemList.stream().map(OrderItemDO::getProductStockId).collect(Collectors.toList());
//            list = BeanUtils.toBean(productStockManager.list(new LambdaQueryWrapper<ProductStockDO>().in(ProductStockDO::getId, collect)), ProductStockVO.class);
//            resMap.put("order", sendDO);
//            resMap.put("orderType", ScanFormCodeTypeEnum.RYKS_FHD.getValue());
//        }
        //订单
        if (ScanFormCodeTypeEnum.RYKS_DDD.getValue().equals(substring)) {
            OrderDO orderDo = orderManager.getOne(new LambdaQueryWrapper<OrderDO>().eq(OrderDO::getSn, orderCode).last("limit 1"));
            if (ObjectUtil.isNull(orderDo)) {
                throw new ServiceException("该单号未匹配到商品");
            }
            List<OrderItemDO> orderItemList = orderItemManager.list(new LambdaQueryWrapper<OrderItemDO>().eq(OrderItemDO::getOrderId, orderDo.getId()));
            List<Long> collect = orderItemList.stream().map(OrderItemDO::getProductStockId).collect(Collectors.toList());
            list = BeanUtils.toBean(productStockManager.list(new LambdaQueryWrapper<ProductStockDO>().in(ProductStockDO::getId, collect)), ProductStockVO.class);
            for (ProductStockVO vo : list) {
                vo.setScanOrderItemId(orderItemList.stream().filter(item ->item.getProductStockId().equals(vo.getId())).findFirst().get().getId());
            }
            resMap.put("order", orderDo);
            resMap.put("orderType", ScanFormCodeTypeEnum.RYKS_DDD.getValue());
        }
        //入库单
        if (ScanFormCodeTypeEnum.RYKS_RKD.getValue().equals(substring)) {
            WarehouseOutDO outDo = warehouseOutManager.getOne(new LambdaQueryWrapper<WarehouseOutDO>().eq(WarehouseOutDO::getSn, orderCode).last("limit 1"));
            if (ObjectUtil.isNull(outDo)) {
                throw new ServiceException("该单号未匹配到商品");
            }
            List<WarehouseOutItemDO> orderItemList = warehouseOutItemManager.list(new LambdaQueryWrapper<WarehouseOutItemDO>().eq(WarehouseOutItemDO::getWarehouseOutId, outDo.getId()));
            List<Long> collect = orderItemList.stream().map(WarehouseOutItemDO::getProductStockId).collect(Collectors.toList());
            list = BeanUtils.toBean(productStockManager.list(new LambdaQueryWrapper<ProductStockDO>().in(ProductStockDO::getId, collect)), ProductStockVO.class);
            for (ProductStockVO vo : list) {
                vo.setScanOrderItemId(orderItemList.stream().filter(item ->item.getProductId().equals(vo.getId())).findFirst().get().getId());
            }
            resMap.put("order", outDo);
            resMap.put("orderType", ScanFormCodeTypeEnum.RYKS_RKD.getValue());
        }
        //盘点
        if (ScanFormCodeTypeEnum.RYKS_PDD.getValue().equals(substring)) {
            StockInventory stockDo = stockInventoryManager.getOne(new LambdaQueryWrapper<StockInventory>().eq(StockInventory::getSn, orderCode).last("limit 1"));
            if (ObjectUtil.isNull(stockDo)) {
                throw new ServiceException("该单号未匹配到商品");
            }
            List<StockInventoryProduct> orderItemList = stockInventoryProductManager.list(new LambdaQueryWrapper<StockInventoryProduct>().eq(StockInventoryProduct::getInventoryId, stockDo.getId()));
            List<Long> collect = orderItemList.stream().map(StockInventoryProduct::getProductStockId).collect(Collectors.toList());
            list = BeanUtils.toBean(productStockManager.list(new LambdaQueryWrapper<ProductStockDO>().in(ProductStockDO::getId, collect)), ProductStockVO.class);
            for (ProductStockVO vo : list) {
                vo.setScanOrderItemId(orderItemList.stream().filter(item ->item.getProductId().equals(vo.getId())).findFirst().get().getId());
            }
            resMap.put("order", stockDo);
            resMap.put("orderType", ScanFormCodeTypeEnum.RYKS_PDD.getValue());
        }
//        //退货单
//        if (ScanFormCodeTypeEnum.RYKS_THD.getValue().equals(substring)) {
//            OrderReturnDO returnDo = orderReturnManager.getOne(new LambdaQueryWrapper<OrderReturnDO>().eq(OrderReturnDO::getSn, orderCode).last("limit 1"));
//            if (ObjectUtil.isNull(returnDo)) {
//                return resMap;
//            }
//            List<OrderReturnItemDO> orderItemList = orderReturnItemManager.list(new LambdaQueryWrapper<OrderReturnItemDO>().eq(OrderReturnItemDO::getOrderReturnId, returnDo.getId()));
//            List<Long> collect = orderItemList.stream().map(OrderReturnItemDO::getProductStockId).collect(Collectors.toList());
//            list = BeanUtils.toBean(productStockManager.list(new LambdaQueryWrapper<ProductStockDO>().in(ProductStockDO::getId, collect)), ProductStockVO.class);
//            resMap.put("order", returnDo);
//            resMap.put("orderType", ScanFormCodeTypeEnum.RYKS_THD.getValue());
//        }

        if (ScanFormCodeTypeEnum.RYKS_DBD.getValue().equals(substring)) {
            StockTransferDO transDo = stockTransferManager.getOne(new LambdaQueryWrapper<StockTransferDO>().eq(StockTransferDO::getSn, orderCode).last("limit 1"));
            if (ObjectUtil.isNull(transDo)) {
                throw new ServiceException("该单号未匹配到商品");
            }
            List<StockTransferProductDO> orderItemList = stockTransferProductManager.list(new LambdaQueryWrapper<StockTransferProductDO>().eq(StockTransferProductDO::getStockTransferId, transDo.getId()));
            List<Long> collect = orderItemList.stream().map(StockTransferProductDO::getProductStockId).collect(Collectors.toList());
            list = BeanUtils.toBean(productStockManager.list(new LambdaQueryWrapper<ProductStockDO>().in(ProductStockDO::getId, collect)), ProductStockVO.class);
            for (ProductStockVO vo : list) {
                vo.setScanOrderItemId(orderItemList.stream().filter(item ->item.getProductId().equals(vo.getId())).findFirst().get().getId());
            }
            resMap.put("order", transDo);
            resMap.put("orderType", ScanFormCodeTypeEnum.RYKS_DBD.getValue());
        }


        //入库单
        if (ScanFormCodeTypeEnum.RYKS_CKD.getValue().equals(substring)) {
            WarehouseEntryDO entryDo = warehouseEntryManager.getOne(new LambdaQueryWrapper<WarehouseEntryDO>().eq(WarehouseEntryDO::getSn, orderCode).last("limit 1"));
            if (ObjectUtil.isNull(entryDo)) {
                throw new ServiceException("该单号未匹配到商品");
            }
            List<WarehouseEntryProductDO> orderItemList = warehouseEntryProductManager.list(new LambdaQueryWrapper<WarehouseEntryProductDO>().eq(WarehouseEntryProductDO::getWarehouseEntryId, entryDo.getId()));
            List<Long> collect = orderItemList.stream().map(WarehouseEntryProductDO::getProductStockId).collect(Collectors.toList());
            list = BeanUtils.toBean(productStockManager.list(new LambdaQueryWrapper<ProductStockDO>().in(ProductStockDO::getId, collect)), ProductStockVO.class);
            for (ProductStockVO vo : list) {
                vo.setScanOrderItemId(orderItemList.stream().filter(item ->item.getProductId().equals(vo.getId())).findFirst().get().getId());
            }
            resMap.put("order", entryDo);
            resMap.put("orderType", ScanFormCodeTypeEnum.RYKS_CKD.getValue());
        }


        if (ScanFormCodeTypeEnum.RYKS_BSD.getValue().equals(substring)) {
            StockDamageReport damageDo = stockDamageReportManager.getOne(new LambdaQueryWrapper<StockDamageReport>().eq(StockDamageReport::getSn, orderCode).last("limit 1"));
            if (ObjectUtil.isNull(damageDo)) {
                throw new ServiceException("该单号未匹配到商品");
            }
            List<StockDamageReportProduct> orderItemList = stockDamageReportProductManager.list(new LambdaQueryWrapper<StockDamageReportProduct>().eq(StockDamageReportProduct::getReportId, damageDo.getId()));
            List<Long> collect = orderItemList.stream().map(StockDamageReportProduct::getProductStockId).collect(Collectors.toList());
            list = BeanUtils.toBean(productStockManager.list(new LambdaQueryWrapper<ProductStockDO>().in(ProductStockDO::getId, collect)), ProductStockVO.class);
            for (ProductStockVO vo : list) {
                vo.setScanOrderItemId(orderItemList.stream().filter(item ->item.getProductId().equals(vo.getId())).findFirst().get().getId());
            }
            resMap.put("order", damageDo);
            resMap.put("orderType", ScanFormCodeTypeEnum.RYKS_BSD.getValue());
        }
        for (ProductStockVO vo : list) {
            vo.setGoodsVO(BeanUtil.copyProperties(goodsManager.getById(vo.getGoodsId()), GoodsVO.class));
            vo.setProductVO(BeanUtil.copyProperties(productManager.getById(vo.getProductId()), ProductVO.class));
        }
        resMap.put("list", list);
        return resMap;
    }

    @Override
    public ProductStockVO getProduct(String productCode, Long warehouseId) {
        ProductDO productDO = productManager.getOne(new LambdaQueryWrapper<ProductDO>().eq(ProductDO::getSku, productCode).last("limit 1"));
        AdminUserContext.compareCompany(productDO.getCompanyId());
        if (ObjectUtil.isNull(productDO)) {
            throw new ServiceException("请录入正确的sku");
        }
        ProductStockDO stockDO = productStockManager.getOne(new LambdaQueryWrapper<ProductStockDO>()
                .eq(ProductStockDO::getWarehouseId, warehouseId).eq(ProductStockDO::getProductId, productDO.getId()));
        ProductStockVO productStockVO = BeanUtil.copyProperties(stockDO, ProductStockVO.class);
        productStockVO.setProductVO(BeanUtil.copyProperties(productDO, ProductVO.class));
        productStockVO.setGoodsVO(BeanUtil.copyProperties(goodsManager.getById(stockDO.getGoodsId()), GoodsVO.class));
        return productStockVO;
    }

    @Override
    public GoodsSnVO getSnCode(String sku, String snCode, String type) {
        GoodsSnDO one = goodsSnManager.getOne(new LambdaQueryWrapper<GoodsSnDO>().eq(GoodsSnDO::getSku, sku).eq(GoodsSnDO::getSnCode, snCode).last("limit 1"));
        AdminUserContext.compareCompany(one.getCompanyId());
        if (ObjectUtil.isNull(one)) {
            throw new ServiceException("请录入正确的序列号");
        }
        if (StrUtil.isNotBlank(type) && "in".equals(type)) {
            if (one.getStatus().equals(SnCodeStatusEnum.IN_WAREHOUSE)) {
                throw new ServiceException("该序列号已入库");
            }
        }
        if (StrUtil.isNotBlank(type) && "out".equals(type)) {
            if (!one.getStatus().equals(SnCodeStatusEnum.IN_WAREHOUSE)) {
                throw new ServiceException("已入库的序列号才能出库");
            }
        }
        return BeanUtil.copyProperties(one, GoodsSnVO.class);
    }
}

