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

import cn.hutool.core.collection.CollUtil;
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.exception.LuckException;
import com.tmerclub.cloud.common.product.vo.BrandVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.dto.BrandDTO;
import com.tmerclub.cloud.product.dto.BrandSigningDTO;
import com.tmerclub.cloud.product.service.BrandService;
import com.tmerclub.cloud.product.service.BrandShopService;
import com.tmerclub.cloud.product.service.CategoryBrandService;
import com.tmerclub.cloud.product.service.CategoryService;
import com.tmerclub.cloud.product.vo.BrandSigningVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 品牌信息
 *
 * @author tmerclub
 * @date 2020-10-28 15:27:24
 */
@RestController("platformBrandController")
@RequestMapping("/p/brand")
@Tag(name = "platform-品牌信息")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Autowired
    private BrandShopService brandShopService;

    @GetMapping("/page")
    @Operation(summary = "获取品牌信息列表", description = "分页获取品牌信息列表")
    public ServerResponseEntity<PageVO<BrandVO>> page(@Valid PageDTO pageDTO, BrandDTO brandDTO) {
        brandDTO.setShopId(Constant.PLATFORM_SHOP_ID);
        PageVO<BrandVO> brandPage = brandService.page(pageDTO, brandDTO);
        return ServerResponseEntity.success(brandPage);
    }

    @GetMapping
    @Operation(summary = "获取品牌信息", description = "根据brandId获取品牌信息")
    public ServerResponseEntity<BrandVO> getByBrandId(@RequestParam Long brandId) {
        BrandVO brand = brandService.getInfo(brandId);
        categoryService.getPathNames(brand.getCategories());
        return ServerResponseEntity.success(brand);
    }

    @PostMapping
    @Operation(summary = "保存品牌信息", description = "保存品牌信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody BrandDTO brandDTO) {
        brandDTO.setShopId(Constant.PLATFORM_SHOP_ID);
        brandService.save(brandDTO);
        // 清除缓存
        // 获取当前节点所有父节点的分类ids，以及当前分类节点的父级节点的父级几点的分类ids
        List<Long> categoryIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        List<Long> parentCategoryIds = categoryService.getParentIdsByCategoryId(brandDTO.getCategoryIds());
        if (CollUtil.isNotEmpty(parentCategoryIds)) {
            categoryIds = parentCategoryIds;
        }
        if (CollUtil.isNotEmpty(brandDTO.getCategoryIds())) {
            categoryIds.addAll(brandDTO.getCategoryIds());
        }
        brandService.removeCache(categoryIds);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新品牌信息", description = "更新品牌信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody BrandDTO brandDTO) {
        // 找出所有关联分类的进行清除
        List<Long> categoryIds = categoryBrandService.getCategoryIdBrandId(brandDTO.getBrandId());
        brandService.update(brandDTO);
        if (CollUtil.isNotEmpty(brandDTO.getCategoryIds())) {
            categoryIds.addAll(brandDTO.getCategoryIds());
        }
        // 找出分类的第一级进行清除缓存
        // 获取当前节点所有父节点的分类ids，以及当前分类节点的父级节点的父级几点的分类ids
        List<Long> parentCategoryIds = categoryService.getParentIdsByCategoryId(categoryIds);
        if (CollUtil.isNotEmpty(parentCategoryIds)) {
            categoryIds.addAll(parentCategoryIds);
        }

        // 清除缓存
        brandService.removeCache(categoryIds);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除品牌信息", description = "根据品牌信息id删除品牌信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long brandId) {
        List<Long> categoryIds = categoryBrandService.getCategoryIdBrandId(brandId);
        brandService.deleteById(brandId);
        // 清除缓存
        // 获取当前节点所有父节点的分类ids，以及当前分类节点的父级节点的父级几点的分类ids
        List<Long> parentCategoryIds = categoryService.getParentIdsByCategoryId(categoryIds);
        if (CollUtil.isNotEmpty(parentCategoryIds)) {
            categoryIds.addAll(parentCategoryIds);
        }
        brandService.removeCache(categoryIds);
        return ServerResponseEntity.success();
    }

    @PutMapping("/update_brand_status")
    @Operation(summary = "更新品牌状态（启用或禁用）", description = "更新品牌状态（启用或禁用）")
    public ServerResponseEntity<Void> updateBrandStatus(@RequestBody BrandDTO brandDTO) {
        if (Objects.isNull(brandDTO.getStatus())) {
            throw new LuckException("状态不能为空");
        }
        if (Objects.isNull(brandDTO.getBrandId())) {
            throw new LuckException("品牌id不能为空");
        }
        brandService.updateBrandStatus(brandDTO);
        // 清楚缓存
        List<Long> categoryIds = categoryBrandService.getCategoryIdBrandId(brandDTO.getBrandId());
        // 获取当前节点所有父节点的分类ids，以及当前分类节点的父级节点的父级几点的分类ids
        List<Long> parentCategoryIds = categoryService.getParentIdsByCategoryId(categoryIds);
        if (CollUtil.isNotEmpty(parentCategoryIds)) {
            categoryIds.addAll(parentCategoryIds);
        }
        brandService.removeCache(CollUtil.isNotEmpty(categoryIds) ? categoryIds : new ArrayList<>(0));
        return ServerResponseEntity.success();
    }

    @GetMapping("/signing_info_by_shop_id")
    @Operation(summary = "根据店铺id获取签约的品牌列表", description = "根据店铺id获取签约的品牌列表")
    @Parameter(name = "sysType", description = "系统类型 0.普通用户系统 1.商家端 2.平台端 3.供应商端")
    public ServerResponseEntity<BrandSigningVO> signingInfoByShopId(@RequestParam Long shopId, @RequestParam("sysType") Integer sysType) {
        sysType = Objects.isNull(sysType) ? AuthUserContext.get().getSysType() : sysType;
        BrandSigningVO brandSigningVO = brandShopService.listSigningByShopId(shopId, sysType);
        return ServerResponseEntity.success(brandSigningVO);
    }

    @PutMapping("/signing")
    @Operation(summary = "根据店铺id更新店铺下的签约品牌", description = "根据店铺id更新店铺下的签约品牌")
    public ServerResponseEntity<Void> signingBrands(@RequestBody BrandSigningDTO brandSigningDTO, @RequestParam("shopId") Long shopId, @RequestParam("sysType") Integer sysType) {
        brandShopService.signingBrands(brandSigningDTO, shopId, sysType);
        return ServerResponseEntity.success();
    }

}
