package org.jeecg.modules.mes.material.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 jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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.mes.material.entity.MesMaterialCategory;
import org.jeecg.modules.mes.material.service.IMesMaterialCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Tag(name = "MES-物料分类")
@RestController
@RequestMapping("/mes/material/category")
public class MesMaterialCategoryController extends JeecgController<MesMaterialCategory, IMesMaterialCategoryService> {
  @Autowired private IMesMaterialCategoryService mesMaterialCategoryService;

  @GetMapping("/list")
  public Result<IPage<MesMaterialCategory>> queryPageList(
      MesMaterialCategory model,
      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
      HttpServletRequest req) {
    QueryWrapper<MesMaterialCategory> queryWrapper = QueryGenerator.initQueryWrapper(model, req.getParameterMap());
    IPage<MesMaterialCategory> pageList = mesMaterialCategoryService.page(new Page<>(pageNo, pageSize), queryWrapper);
    return Result.OK(pageList);
  }

  @AutoLog(value = "MES-物料分类-添加")
  @Operation(summary = "MES-物料分类-添加")
  @RequiresPermissions("mes:mes_material_category:add")
  @PostMapping("/add")
  public Result<String> add(@RequestBody MesMaterialCategory model) {
    mesMaterialCategoryService.save(model);
    return Result.OK("添加成功！");
  }

  @AutoLog(value = "MES-物料分类-编辑")
  @Operation(summary = "MES-物料分类-编辑")
  @RequiresPermissions("mes:mes_material_category:edit")
  @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
  public Result<String> edit(@RequestBody MesMaterialCategory model) {
    mesMaterialCategoryService.updateById(model);
    return Result.OK("编辑成功!");
  }

  @AutoLog(value = "MES-物料分类-通过id删除")
  @Operation(summary = "MES-物料分类-通过id删除")
  @RequiresPermissions("mes:mes_material_category:delete")
  @DeleteMapping("/delete")
  public Result<String> delete(@RequestParam(name = "id") String id) {
    mesMaterialCategoryService.removeById(id);
    return Result.OK("删除成功!");
  }

  @AutoLog(value = "MES-物料分类-批量删除")
  @Operation(summary = "MES-物料分类-批量删除")
  @RequiresPermissions("mes:mes_material_category:deleteBatch")
  @DeleteMapping("/deleteBatch")
  public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
    mesMaterialCategoryService.removeByIds(Arrays.asList(ids.split(",")));
    return Result.OK("批量删除成功!");
  }

  @GetMapping("/queryById")
  public Result<MesMaterialCategory> queryById(@RequestParam(name = "id") String id) {
    return Result.OK(mesMaterialCategoryService.getById(id));
  }

  @RequiresPermissions("mes:mes_material_category:exportXls")
  @RequestMapping("/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, MesMaterialCategory model) {
    return super.exportXls(request, model, MesMaterialCategory.class, "MES物料分类");
  }

  @RequiresPermissions("mes:mes_material_category:importExcel")
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
    return super.importExcel(request, response, MesMaterialCategory.class);
  }
}
