package com.cpop.oam.business.controller.backstage;

import com.cpop.core.base.entity.R;
import com.cpop.oam.business.bo.BrandManagerBo;
import com.cpop.oam.business.bo.BrandManagerPageBo;
import com.cpop.oam.business.bo.StoreListByBrandBo;
import com.cpop.oam.business.entity.table.StoreTableDef;
import com.cpop.oam.business.service.BrandManagerService;
import com.cpop.oam.business.vo.BrandListVo;
import com.cpop.oam.business.vo.BrandManagePageVo;
import com.cpop.oam.business.vo.StoreListByBrandVo;
import com.mybatisflex.core.datasource.DataSourceKey;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.row.DbChain;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import static com.cpop.oam.business.entity.table.BrandTableDef.BRAND;
import static com.cpop.oam.business.entity.table.StoreTableDef.STORE;


/**
 * 品牌管理员表 控制层。
 *
 * @author DB
 * @since 2023-12-07
 */
@RestController
@Tag(name = "品牌管理员表接口")
@RequestMapping("/backstage/brandManage")
public class BackstageBrandManagerController {

    @Autowired
    private BrandManagerService brandManageService;

    /**
     * 查询品牌管理员分页
     * @author DB
     * @since 2023/12/07
     * @param bo 请求参数
     * @return R<Page<BrandManagePageVo>>
     */
    @Operation(summary = "查询品牌管理员分页")
    @GetMapping("/getBrandManagePage")
    public R<Page<BrandManagePageVo>> getBrandManagePage(BrandManagerPageBo bo) {
        Page<BrandManagePageVo> page = brandManageService.getBrandManagerPage(bo);
        return R.ok(page);
    }

    /**
     * 查询品牌列表
     * @author DB
     * @since 2023/09/07 18:07
     * @return R<List<BrandListVo>>
     */
    @Operation(summary = "查询品牌列表")
    @GetMapping("/getBrandList")
    public R<List<BrandListVo>> getBrandList(String query) {
        AtomicReference<List<BrandListVo>> list = new AtomicReference<>();
        try {
            DataSourceKey.use("jambox", () -> {
                list.set(DbChain.table(BRAND).and(BRAND.BRAND_NAME.like(query)).listAs(BrandListVo.class));
            });
        } finally {
            DataSourceKey.clear();
        }
        return R.ok(list.get());
    }

    /**
     * 查询品牌列表
     * @return R<PageVo<BrandPageListVo>>
     * @author DB
     * @since 2023/6/2 17:37
     **/
    @Operation(summary = "根据品牌查询校区列表")
    @PostMapping("/getStoreListByBrand")
    public R<List<StoreListByBrandVo>> getCampusListByBrand(@RequestBody StoreListByBrandBo bo) {
        AtomicReference<List<StoreListByBrandVo>> vos = new AtomicReference<>();
        try {
            DataSourceKey.use("jambox", () -> {
                vos.set(DbChain.table(STORE).and(StoreTableDef.STORE.BRAND_ID.in(bo.getBrandIds())).listAs(StoreListByBrandVo.class));
            });
        } finally {
            DataSourceKey.clear();
        }
        return R.ok(vos.get());
    }

    /**
     * 删除品牌管理员
     * @author DB
     * @since 2023/12/07
     * @param id 主键
     * @return R<Void>
     */
    @Operation(summary = "删除品牌管理员")
    @DeleteMapping("/removeBrandManageById/{id}")
    public R<Void> removeBrandManageById(@PathVariable String id) {
        brandManageService.removeBrandManagerById(id);
        return R.ok();
    }

    /**
     * 新增品牌管理员
     * @author DB
     * @since 2023/12/07
     * @param bo 请求
     * @return R<Void>
     */
    @Operation(summary = "新增品牌管理员")
    @PostMapping("insertBrandManage")
    public R<Void> insertBrandManage(@RequestBody @Validated BrandManagerBo bo) {
        brandManageService.insertBrandManager(bo);
        return R.ok();
    }

}
