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

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.product.vo.SkuStockVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.product.dto.ProductSearchDTO;
import com.tmerclub.cloud.common.product.vo.SkuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.ExcelUtil;
import com.tmerclub.cloud.product.model.SkuStock;
import com.tmerclub.cloud.product.service.SkuComboService;
import com.tmerclub.cloud.product.service.SkuService;
import com.tmerclub.cloud.product.service.SkuStockService;
import com.tmerclub.cloud.product.service.SpuService;
import com.tmerclub.cloud.product.vo.SkuExcelVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
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;
/**
 * @author lanhai
 */
@RestController("multishopSkuController")
@RequestMapping("/m/sku")
@Tag(name = "multishop-sku信息")
public class SkuController {

    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuStockService skuStockService;
    @Autowired
    private SkuComboService skuComboService;

    @GetMapping("/get_all_skuList")
    @Operation(summary = "通过prodId获取全部的规格列表", description = "通过prodId获取全部的规格列表")
    @Parameter(name = "prodId", description = "商品id")
    public ServerResponseEntity<List<SkuVO>> getSkuListByProdId(Long prodId) {
        List<SkuVO> skus = skuService.listSkuWithAttrBySpuId(prodId);
        List<Long> skuIds = skus.stream().map(SkuVO::getSkuId).toList();
        Map<Long, SkuStockVO> stockMap = skuStockService.mapSkuTotalStockBySkuIds(skuIds);
        for (SkuVO skuVO : skus) {
            if (stockMap.containsKey(skuVO.getSkuId())) {
                skuVO.setStock(stockMap.get(skuVO.getSkuId()).getStock());
            }
        }
        return ServerResponseEntity.success(skus);
    }

    @GetMapping("/get_stock_warning_num")
    @Operation(summary = "查询sku库存预警数量", description = "查询sku库存预警数量")
    public ServerResponseEntity<Integer> getStockWarningNum(ProductSearchDTO product) {
        product.setShopId(AuthUserContext.get().getTenantId());
        Integer count = skuService.getStockWarningNum(product);
        return ServerResponseEntity.success(count);
    }

    @GetMapping("/soldExcel")
    @Operation(summary = "导出sku信息", description = "导出sku信息")
    public ServerResponseEntity<Void> skuSoldExcel(HttpServletResponse response, ProductSearchDTO product) {
        product.setShopId(AuthUserContext.get().getTenantId());
        List<SkuExcelVO> skuList = skuService.exportSkuList(product);
        ExcelUtil.soleExcel(response, skuList, SkuExcelVO.EXCEL_NAME, SkuExcelVO.MERGE_ROW_INDEX, SkuExcelVO.MERGE_COLUMN_INDEX, SkuExcelVO.class);
        return ServerResponseEntity.success();
    }

    @GetMapping("/page_sku")
    @Operation(summary = "分页查询sku信息", description = "分页查询sku信息")
    public ServerResponseEntity<PageVO<SkuVO>> pageSku(PageDTO pageDTO, ProductSearchDTO product) {
        product.setShopId(AuthUserContext.get().getTenantId());
        PageVO<SkuVO> skuPage = skuService.pageSku(pageDTO, product);
        List<SkuVO> skuList = skuPage.getList();
        if (CollUtil.isEmpty(skuList)) {
            return ServerResponseEntity.success(skuPage);
        }
        for (SkuVO skuVO : skuList) {
            Integer spuMold = spuService.getBySpuId(skuVO.getSpuId()).getSpuMold();
            SkuStock skuStock = skuStockService.getBySkuId(skuVO.getSkuId(), spuMold);
            skuVO.setStock(skuStock.getStock());
        }
        return ServerResponseEntity.success(skuPage);
    }




}
