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

import com.cpop.core.base.R;
import com.cpop.core.utils.SpringUtils;
import com.cpop.mall.business.bo.ProductBo;
import com.cpop.mall.business.bo.ProductPageBo;
import com.cpop.mall.business.service.ProductRecordService;
import com.cpop.mall.business.service.ProductService;
import com.cpop.mall.business.service.ProductSpecificationService;
import com.cpop.mall.framework.task.ShoppingCartAsyncTask;
import com.cpop.mall.business.vo.JamboxCardTemplateListVo;
import com.cpop.mall.business.vo.ProductInfoVo;
import com.cpop.mall.business.vo.ProductPageVo;
import com.cpop.mall.business.vo.StoreListVo;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.cpop.mall.business.entity.table.ProductRecordTableDef.PRODUCT_RECORD;
import static com.cpop.mall.business.entity.table.ProductSpecificationTableDef.PRODUCT_SPECIFICATION;
import static com.cpop.mall.business.entity.table.ProductTableDef.PRODUCT;

/**
 * 商城-商品表 控制层。
 *
 * @author DB
 * @since 2023-10-23
 */
@RestController
@Api(tags = "商城-商品管理")
@RequestMapping("/backstage/product")
public class BackstageProductController {

    @Autowired
    private ProductService productService;

    /**
     * @descriptions 分页查询商城-商品
     * @author DB
     * @date 2023/10/23 11:56
     * @param bo 分页参数
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
     */
    @GetMapping("/getProductPage")
    @ApiOperation("分页查询商城-商品")
    public R<Page<ProductPageVo>> getProductPage(@ApiParam("分页参数") ProductPageBo bo) {
        Page<ProductPageVo> page = productService.getProductPage(bo);
        return R.ok(page);
    }

    /**
     * @descriptions 查询商城-商品详情
     * @author DB
     * @date 2023/11/09 11:00
     * @param id 主键
     * @return: com.cpop.core.base.R<com.cpop.mall.business.vo.ProductInfoVo>
     */
    @GetMapping("/getProductInfo/{id}")
    @ApiOperation("查询商城-商品详情")
    public R<ProductInfoVo> getProductInfo(@PathVariable @ApiParam("主键") String id) {
        ProductInfoVo info = productService.getProductInfo(id);
        return R.ok(info);
    }

    /**
     * @descriptions 选择品牌下的店铺/校区
     * @author DB
     * @date 2023/10/23 11:56
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
     */
    @GetMapping("/getStoreList")
    @ApiOperation("选择品牌下的店铺/校区")
    public R<List<StoreListVo>> getStoreList() {
        List<StoreListVo> list = productService.getStoreList();
        return R.ok(list);
    }

    /**
     * @descriptions 获取果酱课卡模板
     * @author DB
     * @date 2023/10/23 11:56
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
     */
    @GetMapping("/getJamboxCardTemplate")
    @ApiOperation("获取果酱课卡模板")
    public R<List<JamboxCardTemplateListVo>> getJamboxCardTemplate() {
        List<JamboxCardTemplateListVo> list = productService.getJamboxCardTemplate();
        return R.ok(list);
    }

    /**
     * @descriptions 创建规格
     * @author DB
     * @date 2023/10/23 12:15
     * @param specificationGroups 规格集合
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PostMapping("/createSpecification")
    @ApiOperation("创建规格")
    public R<List<String>> createSpecification(@RequestBody @ApiParam("规格集合") List<List<String>> specificationGroups) {
        List<String> list = productService.createSpecification(specificationGroups);
        return R.ok(list);
    }

    /**
     * @descriptions 保存商城-商品
     * @author DB
     * @date 2023/10/23 12:15
     * @param bo 商城-商品
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PostMapping("/insertProduct")
    @ApiOperation("保存商城-商品")
    public R<Void> insertProduct(@RequestBody @Validated @ApiParam("商城-商品") ProductBo bo) {
        productService.insertProduct(bo);
        return R.ok();
    }

    /**
     * @descriptions 根据主键重置商城商品
     * @author DB
     * @date 2023/10/23 12:15
     * @param id 商城-商品id
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PutMapping("/resetProduct/{id}")
    @ApiOperation("根据主键重置商城商品")
    public R<Void> update(@PathVariable("id") String id) {
        productService.resetProduct(id);
        return R.ok();
    }

    /**
     * @descriptions 根据主键重置商城商品
     * @author DB
     * @date 2023/10/23 12:15
     * @param bo 商城-商品
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PutMapping("/updateProduct")
    @ApiOperation("根据主键更新商城-商品")
    public R<Void> updateProduct(@RequestBody @Validated @ApiParam("商城-商品") ProductBo bo) {
        productService.updateProduct(bo);
        return R.ok();
    }

    /**
     * 根据主键删除商城-商品表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据主键删除商城-商品")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> removeById(@PathVariable("id") @ApiParam("商城-商品主键") String id) {
        productService.removeById(id);
        //删规格
        ProductSpecificationService productSpecificationService = SpringUtils.getBean(ProductSpecificationService.class);
        productSpecificationService.updateChain().where(PRODUCT_SPECIFICATION.PRODUCT_ID.eq(id)).remove();
        //删商品记录详情
        ProductRecordService specificationRecordService = SpringUtils.getBean(ProductRecordService.class);
        specificationRecordService.updateChain().where(PRODUCT_RECORD.PRODUCT_ID.eq(id)).remove();
        //删除购物车
        SpringUtils.getBean(ShoppingCartAsyncTask.class).asyncRemoveShoppingCart(id);
        return R.ok();
    }

    /**
     * @descriptions 商城商品上架
     * @author DB
     * @date 2023/10/23 12:15
     * @param productId 商城-商品id
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PutMapping("/productUpOrDown")
    @ApiOperation("商城商品上下架")
    public R<Void> productUpOrDown(@RequestParam("productId") @ApiParam(value = "商城-商品Id",required = true) String productId) {
        productService.updateChain().setRaw(PRODUCT.IS_UP, "if(is_up = 0, 1, 0)").where(PRODUCT.ID.eq(productId)).update();
        return R.ok();
    }

    /**
     * @descriptions 商城商品置顶
     * @author DB
     * @date 2023/10/23 12:15
     * @param productId 商城-商品id
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PutMapping("/productIsTop")
    @ApiOperation("商城商品置顶")
    public R<Void> productIsTop(@RequestParam("productId") @ApiParam(value = "商城-商品Id",required = true) String productId) {
        productService.updateChain().setRaw(PRODUCT.IS_TOP, "if(is_top = 0, 1, 0)").where(PRODUCT.ID.eq(productId)).update();
        return R.ok();
    }

}
