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

import com.cpop.api.cloudDb.core.dto.CloudBrandDto;
import com.cpop.api.cloudDb.handler.CloudBrandHandler;
import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.R;
import com.cpop.core.base.enums.SourceType;
import com.cpop.core.utils.SpringUtils;
import com.cpop.system.business.bo.BrandBo;
import com.cpop.system.business.vo.BrandPageVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.cpop.system.business.entity.Brand;
import com.cpop.system.business.service.BrandService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 系统-品牌表 控制层。
 *
 * @author DB
 * @since 2023-10-25
 */
@RestController
@Api(tags = "系统-品牌表接口")
@RequestMapping("/backstage/brand")
public class BackstageBrandController {

    @Autowired
    private BrandService brandService;

    /**
     * @descriptions 新增系统品牌
     * @author DB
     * @date 2023/10/25 16:53
     * @param bo 品牌参数
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PostMapping("/insertSysBrand")
    @ApiOperation("新增系统品牌")
    public R<Void> insertSysBrand(@RequestBody @ApiParam("系统-品牌") @Validated BrandBo bo) {
        Brand entity = BeanUtils.mapToClass(bo, Brand.class);
        entity.setSourceType(SourceType.COMMON.toString());
        brandService.save(entity);
        return R.ok();
    }

    /**
     * @descriptions 修改系统品牌
     * @author DB
     * @date 2023/11/10 9:21
     * @param bo 品牌参数
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @ApiOperation("修改系统品牌")
    @PutMapping("/updateSysBrand")
    public R<Void> updateSysBrand(@RequestBody @ApiParam("系统-品牌") @Validated BrandBo bo) {
        Brand entity = BeanUtils.mapToClass(bo, Brand.class);
        brandService.updateById(entity);
        //获取云品牌拓展
        Row row = Db.selectOneByQuery("cp_j_brand_extend", QueryWrapper.create().where("brand_id = ?", bo.getId()));
        if (row != null){
            CloudBrandDto cloudBrandDto = new CloudBrandDto();
            cloudBrandDto.setBrandCloudId(row.getString("brandCloudId"));
            cloudBrandDto.setBrandName(bo.getBrandName());
            SpringUtils.getBean(CloudBrandHandler.class).updateBrand(cloudBrandDto);
        }
        return R.ok();
    }

    /**
     * @descriptions 导入果酱品牌
     * @author DB
     * @date 2023/10/25 15:53
     * @param brandId 果酱品牌信息
     * @return: boolean
     */
    @PostMapping("/importJamboxBrand/{brandId}")
    @ApiOperation("导入果酱品牌")
    public R<Void> importJamboxBrand(@PathVariable String brandId) {
        brandService.importJamboxBrand(brandId);
        return R.ok();
    }

    /**
     * @descriptions 查询品牌分页列表
     * @author DB
     * @date 2023/09/13 17:55
     * @param brandName 请求参数
     * @return com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.system.business.vo.BrandPageVo>>
     */
    @ApiOperation("查询品牌分页列表")
    @GetMapping("/getBrandPage")
    @PreAuthorize("@aps.hasPermission('brandStore:brand:list')")
    public R<Page<BrandPageVo>> getBrandPageList(String brandName) {
        Page<BrandPageVo> pageVo = brandService.getBrandPage(brandName);
        return R.ok(pageVo);
    }

    /**
     * @descriptions 根据品牌id删除品牌
     * @author DB
     * @date 2023/11/09 10:31
     * @param id 主键
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @ApiOperation("根据品牌id删除品牌")
    @DeleteMapping("/removeBrandById/{id}")
    public R<Void> removeBrandById(@PathVariable String id) {
        brandService.removeBrandById(id);
        return R.ok();
    }

}
