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

import com.tmerclub.cloud.api.product.util.ProductLangUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.product.dto.SpuDTO;
import com.tmerclub.cloud.common.product.vo.SkuVO;
import com.tmerclub.cloud.common.product.vo.SpuAttrValueVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.service.*;
import com.tmerclub.cloud.product.service.*;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author lanhai
 */
@RestController("multishopSpuController")
@RequestMapping("/m/spu")
@Tag(name = "商品接口")
public class SpuController {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuAttrValueService spuAttrValueService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/pageSpuSku")
    @Operation(summary = "分页查询当前商家的spu及其sku信息", description = "分页查询当前商家spu及其sku信息")
    public ServerResponseEntity<PageVO<SpuVO>> pageSpuSku(PageDTO pageDTO, SpuDTO spuDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        spuDTO.setShopId(shopId);
        PageVO<SpuVO> page = spuService.pageSpuSku(pageDTO, spuDTO);
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/getSupplierSpu")
    @Operation(summary = "获取供应商spu信息", description = "根据spuId获取spu信息")
    public ServerResponseEntity<SpuVO> getBySpuId(@RequestParam Long spuId) {
        // 获取spu信息
        SpuVO spuVO = spuService.getBySpuId(spuId);
        ProductLangUtil.spu(spuVO);
        ProductLangUtil.spuDetail(spuVO);
        List<SpuAttrValueVO> spuAttrsBySpuId = spuAttrValueService.getSpuAttrsBySpuId(spuId);
        spuVO.setSpuAttrValues(spuAttrsBySpuId);
        // 品牌信息
        spuVO.setBrand(brandService.getByBrandId(spuVO.getBrandId()));
        // sku信息
        spuVO.setSkus(skuService.listSkuAllInfoBySpuId(spuId, spuVO, false));
        spuService.loadSpuAttrs(spuVO);
        // 插入商品库存和销量
        int totalStock = 0;
        int saleNum = 0;
        for (SkuVO skus : spuVO.getSkus()) {
            totalStock += skus.getStock();
            saleNum += Objects.isNull(skus.getSaleNum()) ? 0 : skus.getSaleNum();
        }
        spuVO.setTotalStock(totalStock);
        spuVO.setSaleNum(saleNum);
        // 平台分类、供应商分类信息
        spuVO.setCategory(categoryService.getInfo(spuVO.getCategoryId()));
        ProductLangUtil.category(spuVO.getCategory());
        if (Objects.nonNull(spuVO.getSupplierCategoryId()) && !Objects.equals(spuVO.getSupplierCategoryId(), Constant.DEFAULT_ID)) {
            spuVO.setSupplierCategory(categoryService.getInfo(spuVO.getSupplierCategoryId()));
            ProductLangUtil.category(spuVO.getSupplierCategory());
        }
        return ServerResponseEntity.success(spuVO);
    }
}
