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


import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.delivery.feign.StationFeignClient;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.order.constant.StockModeEnum;
import com.tmerclub.cloud.common.product.dto.SkuStockPointSearchDTO;
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 jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
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("multishopStockPointController")
@RequestMapping("/m/stock_point_sku")
@Tag(name = "商家端库存点关联商品")
public class StockPointSkuController {
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private StockPointSkuService stockPointSkuService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @DubboReference
    private StationFeignClient stationFeignClient;

    @GetMapping("/page")
    @Operation(summary = "分页查询库存点商品信息", description = "分页查询库存点商品信息")
    public ServerResponseEntity<PageVO<SpuVO>> pageStockPointSpuSku(PageDTO pageDTO, StockPointSkuDTO spuDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        spuDTO.setShopId(shopId);
        // 判断是否默认仓库或是共享库存模式的门店，是就获取全部商品
        if (Objects.equals(spuDTO.getType(), 0) || Objects.equals(spuDTO.getStockMode(), StockModeEnum.ALL_STOCK.value())) {
            Long warehouseId = warehouseService.getDefaultWarehouseByShopId(shopId, 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()) {
            // TODO stock
            spuVO.setSkus(skuMap.get(spuVO.getSpuId()));
        }
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping("/page_supplier_stock_point_spu")
    @Operation(summary = "分页查询供应商库存点商品")
    public ServerResponseEntity<PageVO<SpuVO>> pageSupplierStockPointSpu(@Valid PageDTO pageDTO,
                                                               SkuStockPointSearchDTO searchDTO) {
        searchDTO.setShopId(AuthUserContext.get().getTenantId());
        searchDTO.setSysType(SysTypeEnum.MULTISHOP.value());
        PageVO<SpuVO> pageVO = spuService.pageSupplierStockPointSpu(pageDTO, searchDTO);
        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);
        Integer stationSize = stationFeignClient.getStationSize(shopId);
        stockPointVO.setStationSize(stationSize);
        return ServerResponseEntity.success(stockPointVO);
    }
}
