package org.jeecg.modules.oroqen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.oroqen.entity.OroqenProduct;
import org.jeecg.modules.oroqen.service.IOroqenProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 产品
 * @Author: jeecg-boot
 * @Date: 2025-01-08
 * @Version: V1.0
 */
@Tag(name = "产品", description = "产品")
@RestController
@RequestMapping("/oroqen/product")
@Slf4j
public class OroqenProductController extends JeecgController<OroqenProduct, IOroqenProductService> {

    @Autowired
    private IOroqenProductService oroqenProductService;

    /**
     * 分页列表查询
     *
     * @param oroqenProduct
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "产品-分页列表查询")
    @Operation(summary = "产品-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<OroqenProduct>> queryPageList(OroqenProduct oroqenProduct,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<OroqenProduct> queryWrapper = QueryGenerator.initQueryWrapper(oroqenProduct, req.getParameterMap());
        Page<OroqenProduct> page = new Page<OroqenProduct>(pageNo, pageSize);
        IPage<OroqenProduct> pageList = oroqenProductService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据分类ID查询产品列表
     *
     * @param categoryId
     * @return
     */
    @AutoLog(value = "产品-根据分类ID查询")
    @Operation(summary = "产品-根据分类ID查询")
    @GetMapping(value = "/listByCategory")
    public Result<List<OroqenProduct>> queryByCategory(@RequestParam(name = "categoryId", required = true) String categoryId) {
        List<OroqenProduct> list = oroqenProductService.getProductsByCategoryId(categoryId);
        return Result.OK(list);
    }

    /**
     * 查询推荐产品
     *
     * @param limit
     * @return
     */
    @AutoLog(value = "产品-查询推荐产品")
    @Operation(summary = "产品-查询推荐产品")
    @GetMapping(value = "/featured")
    public Result<List<OroqenProduct>> queryFeatured(@RequestParam(name = "limit", defaultValue = "10") Integer limit) {
        List<OroqenProduct> list = oroqenProductService.getFeaturedProducts(limit);
        return Result.OK(list);
    }

    /**
     * 查询热销产品
     *
     * @param limit
     * @return
     */
    @AutoLog(value = "产品-查询热销产品")
    @Operation(summary = "产品-查询热销产品")
    @GetMapping(value = "/hot")
    public Result<List<OroqenProduct>> queryHot(@RequestParam(name = "limit", defaultValue = "10") Integer limit) {
        List<OroqenProduct> list = oroqenProductService.getHotProducts(limit);
        return Result.OK(list);
    }

    /**
     * 添加
     *
     * @param oroqenProduct
     * @return
     */
    @AutoLog(value = "产品-添加")
    @Operation(summary = "产品-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody OroqenProduct oroqenProduct) {
        // 检查产品编码是否存在
        if (oroqenProductService.checkProductCodeExists(oroqenProduct.getProductCode(), null)) {
            return Result.error("产品编码已存在");
        }
        oroqenProductService.save(oroqenProduct);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param oroqenProduct
     * @return
     */
    @AutoLog(value = "产品-编辑")
    @Operation(summary = "产品-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OroqenProduct oroqenProduct) {
        // 检查产品编码是否存在
        if (oroqenProductService.checkProductCodeExists(oroqenProduct.getProductCode(), oroqenProduct.getId())) {
            return Result.error("产品编码已存在");
        }
        oroqenProductService.updateById(oroqenProduct);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "产品-通过id删除")
    @Operation(summary = "产品-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        oroqenProductService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "产品-批量删除")
    @Operation(summary = "产品-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.oroqenProductService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "产品-通过id查询")
    @Operation(summary = "产品-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OroqenProduct> queryById(@RequestParam(name = "id", required = true) String id) {
        OroqenProduct oroqenProduct = oroqenProductService.getById(id);
        if (oroqenProduct == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(oroqenProduct);
    }

    /**
     * 检查库存
     *
     * @param productId
     * @param quantity
     * @return
     */
    @AutoLog(value = "产品-检查库存")
    @Operation(summary = "产品-检查库存")
    @GetMapping(value = "/checkStock")
    public Result<Boolean> checkStock(@RequestParam(name = "productId", required = true) String productId,
                                      @RequestParam(name = "quantity", required = true) Integer quantity) {
        boolean result = oroqenProductService.checkStock(productId, quantity);
        return Result.OK(result);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param oroqenProduct
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OroqenProduct oroqenProduct) {
        return super.exportXls(request, oroqenProduct, OroqenProduct.class, "产品");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OroqenProduct.class);
    }
}