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

import com.alibaba.fastjson.JSONObject;
import com.cpop.core.base.R;
import com.cpop.core.utils.SecurityUtils;
import com.cpop.core.utils.SpringUtils;
import com.cpop.mall.business.bo.CarouselBo;
import com.cpop.mall.business.service.ProductService;
import com.cpop.mall.business.vo.CarouselPageVo;
import com.cpop.mall.business.vo.ProductInfoVo;
import com.cpop.mall.business.vo.ProductSimpleVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.cpop.mall.business.entity.Carousel;
import com.cpop.mall.business.service.CarouselService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.Serializable;
import java.util.List;

import static com.cpop.mall.business.entity.table.CarouselTableDef.CAROUSEL;
import static com.cpop.mall.business.entity.table.ProductTableDef.PRODUCT;

/**
 * 商城轮播图 控制层。
 *
 * @author DB
 * @since 2024-01-15
 */
@RestController
@Api(tags = "商城轮播图接口")
@RequestMapping("/backstage/carousel")
public class BackstageCarouselController {

    @Autowired
    private CarouselService carouselService;

    /**
     * 分页查询商城轮播图
     * @author DB
     * @since 2024/1/15
     * @param upStatus 上架状态
     * @return R<Page<CarouselPageVo>>
     */
    @GetMapping("/getCarouselPage")
    @ApiOperation("分页查询商城轮播图")
    public R<Page<CarouselPageVo>> getCarouselPage(@ApiParam("上架状态") @RequestParam(value = "upStatus", required = false) Boolean upStatus) {
        Page<CarouselPageVo> page = carouselService.getCarouselPage(upStatus);
        return R.ok(page);
    }

    /**
     * 新增轮播图
     * @author DB
     * @since 2024/1/15
     * @param bo 请求参数
     * @return R<Void>
     */
    @PostMapping("/insertCarousel")
    @ApiOperation("新增商城轮播图")
    public R<Void> insertCarousel(@RequestBody CarouselBo bo) {
        carouselService.insertCarousel(bo);
        return R.ok();
    }

    /**
     * 更新轮播图
     *
     * @param bo 请求参数
     * @return R<Void>
     * @author DB
     * @since 2024/1/15
     */
    @PutMapping("/updateCarousel")
    @ApiOperation("修改商城轮播图")
    public R<Void> updateCarousel(@RequestBody CarouselBo bo) {
        carouselService.updateCarousel(bo);
        return R.ok();
    }

    /**
     * 轮播图上下架
     * @param id 主键
     * @return R<Void>
     * @author DB
     * @since 2024/1/15
     */
    @PutMapping("/carouselUnmount/{id}")
    @ApiOperation("轮播图上下架")
    public R<Void> carouselUnmount(@PathVariable String id) {
        carouselService.updateChain()
                .setRaw(CAROUSEL.UP_STATUS, "if(up_status = 0, 1, 0)")
                .where(CAROUSEL.ID.eq(id))
                .update();
        return R.ok();
    }

    /**
     * 删除轮播图
     *
     * @param id 主键
     * @return R<Void>
     * @author DB
     * @since 2024/1/15
     */
    @DeleteMapping("/removeCarouselById/{id}")
    @ApiOperation("删除轮播图")
    public R<Void> removeCarouselById(@PathVariable String id) {
        carouselService.updateChain().where(CAROUSEL.ID.eq(id)).remove();
        return R.ok();
    }

    /**
     * 获取上架商品
     * @author DB
     * @since 2024/1/15
     * @param brandId 品牌id
     * @return R<List<ProductSimpleVo>>
     */
    @GetMapping("/getUpProductList")
    @ApiOperation("获取上架商品")
    public R<List<ProductSimpleVo>> getUpProductList() {
        JSONObject loginUserInfo = SecurityUtils.getInstance().getLoginUserInfo();
        List<ProductSimpleVo> productSimpleVos = SpringUtils.getBean(ProductService.class).listAs(QueryWrapper.create()
                        .select(PRODUCT.ID, PRODUCT.PRODUCT_NAME)
                        .where(PRODUCT.BRAND_ID.eq(loginUserInfo.getString("brandId")))
                        .and(PRODUCT.IS_UP.eq(true))
                , ProductSimpleVo.class);
        return R.ok(productSimpleVos);
    }

}
