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.OroqenProductCategory;
import org.jeecg.modules.oroqen.service.IOroqenProductCategoryService;
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/productCategory")
@Slf4j
public class OroqenProductCategoryController extends JeecgController<OroqenProductCategory, IOroqenProductCategoryService> {

    @Autowired
    private IOroqenProductCategoryService oroqenProductCategoryService;

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

    /**
     * 获取树形结构的分类列表
     *
     * @return
     */
    @AutoLog(value = "产品分类-获取树形结构")
    @Operation(summary = "产品分类-获取树形结构")
    @GetMapping(value = "/tree")
    public Result<List<OroqenProductCategory>> queryTree() {
        List<OroqenProductCategory> list = oroqenProductCategoryService.getCategoryTree();
        return Result.OK(list);
    }

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

    /**
     * 编辑
     *
     * @param oroqenProductCategory
     * @return
     */
    @AutoLog(value = "产品分类-编辑")
    @Operation(summary = "产品分类-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OroqenProductCategory oroqenProductCategory) {
        // 检查分类编码是否存在
        if (oroqenProductCategoryService.checkCategoryCodeExists(oroqenProductCategory.getCategoryCode(), oroqenProductCategory.getId())) {
            return Result.error("分类编码已存在");
        }
        oroqenProductCategoryService.updateById(oroqenProductCategory);
        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) {
        oroqenProductCategoryService.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.oroqenProductCategoryService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

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

    /**
     * 导出excel
     *
     * @param request
     * @param oroqenProductCategory
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OroqenProductCategory oroqenProductCategory) {
        return super.exportXls(request, oroqenProductCategory, OroqenProductCategory.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, OroqenProductCategory.class);
    }
}