package com.cpop.mall.business.controller.mini;

import com.cpop.core.base.R;
import com.cpop.core.utils.SpringUtils;
import com.cpop.jambox.business.entity.BrandExtend;
import com.cpop.jambox.business.service.BrandExtendService;
import com.cpop.mall.business.service.StaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.cpop.jambox.business.entity.table.BrandExtendTableDef.BRAND_EXTEND;
import static com.cpop.mall.business.entity.table.RoleBrandTableDef.ROLE_BRAND;
import static com.cpop.mall.business.entity.table.StaffTableDef.STAFF;

/**
 * @author DB
 * @createTime 2023/11/01 16:36
 * @description
 */
@RestController
@Api(tags = "小程序商城-品牌相关接口")
@RequestMapping("/mini/brand")
public class MiniBrandController {

    @Autowired
    private BrandExtendService brandExtendService;

    /**
     * @descriptions 分页查询商城-商品
     * @author DB
     * @date 2023/10/23 11:56
     * @param cloudBrandId 云品牌id
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
     */
    @GetMapping("/jamboxBrandIsInMall/{cloudBrandId}")
    @ApiOperation("小程序-果酱品牌是否在商城内")
    public R<Boolean> jamboxBrandIsInMall(@PathVariable String cloudBrandId) {
        BrandExtend brandExtend = brandExtendService.queryChain().where(BRAND_EXTEND.BRAND_CLOUD_ID.eq(cloudBrandId)).one();
        if (brandExtend == null) {
            return R.ok(false);
        }
        long count = SpringUtils.getBean(StaffService.class).queryChain()
                .leftJoin(ROLE_BRAND).on(ROLE_BRAND.ID.eq(STAFF.ROLE_BRAND_ID))
                .where(ROLE_BRAND.BRAND_ID.eq(brandExtend.getBrandId())).count();
        return R.ok(count > 0);
    }
}
