package com.tmerclub.cloud.product.controller.supplier;


import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.product.dto.StockPointSkuDTO;
import com.tmerclub.cloud.common.product.vo.SkuVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.product.vo.StockPointVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.service.SkuService;
import com.tmerclub.cloud.product.service.SpuService;
import com.tmerclub.cloud.product.service.StockPointSkuService;
import com.tmerclub.cloud.product.service.WarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lanhai
 */
@RestController("supplierStockPointController")
@RequestMapping("/s/stock_point_sku")
@Tag(name = "供应商库存点关联商品")
public class StockPointSkuController {
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private StockPointSkuService stockPointSkuService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;

    @GetMapping("/page")
    @Operation(summary = "分页查询库存点商品信息", description = "分页查询库存点商品信息")
    public ServerResponseEntity<PageVO<SpuVO>> pageStockPointSpuSku(PageDTO pageDTO, StockPointSkuDTO spuDTO) {
        Long supplierId = AuthUserContext.get().getTenantId();
        spuDTO.setSupplierId(supplierId);
        // 判断是否默认仓库，是就获取全部商品
        if (Objects.equals(spuDTO.getType(), 0)) {
            Long warehouseId = warehouseService.getDefaultWarehouseByShopId(supplierId, AuthUserContext.get().getSysType()).getWarehouseId();
            spuDTO.setStockPointId(warehouseId);
        }
        PageVO<SpuVO> pageVO = spuService.pageStockPointSpu(pageDTO, spuDTO);
        if (CollectionUtil.isEmpty(pageVO.getList())) {
            return ServerResponseEntity.success(pageVO);
        }
        List<SkuVO> skuVOList = skuService.listSkuAndStockByStockPointId(spuDTO.getStockPointId(), spuDTO.getInStockPointId());
        Map<Long, List<SkuVO>> skuMap = skuVOList.stream().collect(Collectors.groupingBy(SkuVO::getSpuId));
        for (SpuVO spuVO : pageVO.getList()) {
            spuVO.setSkus(skuMap.get(spuVO.getSpuId()));
        }
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping("/stock_point")
    @Operation(summary = "获取店铺库存点数据")
    public ServerResponseEntity<StockPointVO> pointList() {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        StockPointVO stockPointVO = warehouseService.getWarehousePoint(shopId, sysType);
        return ServerResponseEntity.success(stockPointVO);
    }
}
