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

import com.tmerclub.cloud.common.constant.IsPassShopEnum;
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.vo.BrandSigningVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
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 lth
 * @Date 2021/4/30 13:26
 */
@RestController("multishopBrandController")
@RequestMapping("/m/apply_shop/brand")
@Tag(name = "multishop-品牌信息")
public class BrandController {

    @Autowired
    private BrandShopService brandShopService;

    @Autowired
    private BrandService brandService;

    @PostMapping("/signing")
    @Operation(summary = "签约品牌", description = "签约品牌")
    public ServerResponseEntity<Void> signingBrands(@Valid @RequestBody BrandSigningDTO brandSigningDTO) {
        Integer sysType = AuthUserContext.get().getSysType();
        if (Objects.equals(AuthUserContext.get().getIsPassShop(), IsPassShopEnum.YES.value())) {
            brandSigningDTO.setCustomizeBrandList(new ArrayList<>(0));
        }
        brandShopService.signingBrands(brandSigningDTO, AuthUserContext.get().getTenantId(), sysType);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list_signing")
    @Operation(summary = "获取店铺下已签约的品牌列表", description = "获取店铺下已签约的品牌列表")
    public ServerResponseEntity<BrandSigningVO> listSigning() {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        BrandSigningVO brandSigningVO = brandShopService.listSigningByShopId(shopId, sysType);
        return ServerResponseEntity.success(brandSigningVO);
    }

    @GetMapping("/list_available_by_category_and_name")
    @Parameters({
            @Parameter(name = "categoryId", description = "分类id"),
            @Parameter(name = "brandName", description = "品牌名称")
    })
    @Operation(summary = "根据分类id与品牌名称获取分类下的品牌与店铺签约的品牌", description = "根据分类id与品牌名称获取分类下的品牌与店铺签约的品牌")
    public ServerResponseEntity<List<BrandVO>> listAvailableBrandByCategoryIdAndBrandName(@RequestParam Long categoryId, @RequestParam(defaultValue = "") String brandName) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        return ServerResponseEntity.success(brandService.listAvailableBrandByCategoryIdAndBrandNameAndShopId(categoryId, brandName, shopId, sysType));
    }

    @DeleteMapping("/delete_signing_brand")
    @Operation(summary = "根据签约信息Id删除签约品牌", description = "根据签约信息Id删除签约品牌")
    public ServerResponseEntity<Void> deleteSigningCategory(@RequestParam("brandShopId") Long brandShopId) {
        brandShopService.deleteSigningBrand(brandShopId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list_apply_signing_brand")
    @Operation(summary = "获取可以签约的品牌列表（已经签约的品牌不会返回）", description = "获取可以签约的品牌列表（已经签约的品牌不会返回）")
    public ServerResponseEntity<List<BrandVO>> listApplySigningBrand(BrandDTO brandDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        return ServerResponseEntity.success(brandService.listApplySigningBrand(brandDTO, shopId, sysType));
    }

    @PostMapping("/add_signing_brand")
    @Operation(summary = "新增签约品牌", description = "新增签约品牌")
    public ServerResponseEntity<Void> addSigningBrand(@Valid @RequestBody BrandSigningDTO brandSigningDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        brandShopService.addSigningBrand(brandSigningDTO, shopId, sysType);
        return ServerResponseEntity.success();
    }

}
