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.MesMaterialErpMap;
import org.jeecg.modules.mes.material.service.IMesMaterialErpMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Tag(name = "MES-物料ERP映射")
@RestController
@RequestMapping("/mes/material/erpMap")
public class MesMaterialErpMapController extends JeecgController<MesMaterialErpMap, IMesMaterialErpMapService> {
  @Autowired private IMesMaterialErpMapService mesMaterialErpMapService;

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

  @AutoLog(value = "MES-物料ERP映射-添加")
  @Operation(summary = "MES-物料ERP映射-添加")
  @RequiresPermissions("mes:mes_material_erp_map:add")
  @PostMapping("/add")
  public Result<String> add(@RequestBody MesMaterialErpMap model) {
    mesMaterialErpMapService.save(model);
    return Result.OK("添加成功！");
  }

  @AutoLog(value = "MES-物料ERP映射-编辑")
  @Operation(summary = "MES-物料ERP映射-编辑")
  @RequiresPermissions("mes:mes_material_erp_map:edit")
  @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
  public Result<String> edit(@RequestBody MesMaterialErpMap model) {
    mesMaterialErpMapService.updateById(model);
    return Result.OK("编辑成功!");
  }

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

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

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

  @RequiresPermissions("mes:mes_material_erp_map:exportXls")
  @RequestMapping("/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, MesMaterialErpMap model) {
    return super.exportXls(request, model, MesMaterialErpMap.class, "MES物料ERP映射");
  }

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