
package com.tmerclub.cloud.platform.controller.platform;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.multishop.feign.ShopDetailFeignClient;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.supplier.service.SupplierDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chiley
 * @date 2022/9/6 14:22
 */
@RestController("platformShopStatisticsController")
@RequestMapping("/p/shop_statistics")
@Tag(name = "platform-店铺统计信息")
public class ShopStatisticsController {

    @DubboReference
    private ShopDetailFeignClient shopDetailFeignClient;

    @Autowired
    private SupplierDetailService supplierDetailService;

    @GetMapping("/count_by_shop_status")
    @Operation(summary = "根据店铺状态查询店铺数量", description = "根据店铺状态查询店铺数量")
    public ServerResponseEntity<Long> countByShopStatus(@RequestParam(value = "shopStatus", required = false, defaultValue = "3") Integer shopStatus) {
        // 商家店铺数量
        ServerResponseEntity<List<Long>> shopIdsResponseEntity = shopDetailFeignClient.getShopIdsByShopStatus(shopStatus);
        if (!shopIdsResponseEntity.isSuccess()) {
            throw new LuckException(shopIdsResponseEntity.getMsg());
        }
        List<Long> merchantShopIdList = shopIdsResponseEntity.getData();
        long merchantShopCount = 0L;
        if (CollUtil.isNotEmpty(merchantShopIdList)) {
            merchantShopCount = merchantShopIdList.size();
        }
        // 供应商店铺数量
        List<Long> supplierIdList = supplierDetailService.getSupplierIdBySupplierStatus(shopStatus);
        long supplierShopCount = 0L;
        if (CollUtil.isNotEmpty(supplierIdList)) {
            supplierShopCount = supplierIdList.size();
        }
        return ServerResponseEntity.success(merchantShopCount + supplierShopCount);
    }
}
