package com.wzj.soopin.goods.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.goods.business.IProductBusiness;
import com.wzj.soopin.goods.domain.bo.ProductBo;
import com.wzj.soopin.goods.domain.entity.Product;
import com.wzj.soopin.goods.domain.vo.ProductVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 商品信息Controller
 *
 * @author zcc
 * @date 2022-11-28
 */
@Tag(name ="商品")
@RestController
@RequestMapping("/pms/product")
@RequiredArgsConstructor
public class ProductController extends BaseController {

    private final IProductBusiness business;

    @Operation(summary = "查询商品信息列表")
    @PostMapping("list")
    public R<IPage<ProductVO>> list(@RequestBody ProductBo bo) {
        return R.ok(business.page(bo));
    }


    @Operation(summary = "导出商品信息列表")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    public R<String> export(ProductBo query) {
        List<ProductVO> list = business.list(query);
        ExcelUtil<ProductVO> util = new ExcelUtil<>(ProductVO.class);
        return R.ok(util.writeExcel(list, "商品信息数据"));
    }

    @Operation(summary = "获取商品信息详细信息")
    @GetMapping(value = "/{id}")
    public R<ProductVO> getInfo(@PathVariable("id") Long id) {
        return R.ok(business.info(id));
    }

    @Operation(summary = "新增商品信息")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R add(@RequestBody ProductBo bo) {
        return R.ok(business.save(bo));
    }

    @Operation
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R edit(@RequestBody ProductBo bo) {

        return R.ok(business.save(bo));
    }

    @Operation(summary = "删除商品信息")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id) {
        return R.ok(business.delete(id));
    }


    @Operation(summary = "审核商品")
    @Log(title = "审核商品", businessType = BusinessType.UPDATE)
    @GetMapping("/audit")
    public R audit( @RequestParam Long id,
                    @RequestParam Integer authFlag,
                    @RequestParam(required = false) String reasons) {
        return R.ok(business.audit(id,authFlag,reasons));
    }

    @Operation(summary = "上下架商品")
    @Log(title = "上下架商品", businessType = BusinessType.UPDATE)
    @GetMapping("/publish")
    public R publish( @RequestParam Long id,
                      @RequestParam Integer publishStatus) {
        return R.ok(business.publish(id,publishStatus));
    }
    @Operation(summary = "查询商品信息列表")
    @PostMapping("/recommend")
    public R<IPage<ProductVO>> recommend(@RequestBody  Page<Product> page) {
        return R.ok(business.getRecommendPage(page));
    }

}
