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.MesUnit;
import org.jeecg.modules.mes.material.service.IMesUnitService;
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/unit")
public class MesUnitController extends JeecgController<MesUnit, IMesUnitService> {
  @Autowired private IMesUnitService mesUnitService;

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

  @AutoLog(value = "MES-单位信息-添加")
  @Operation(summary = "MES-单位信息-添加")
  @RequiresPermissions("mes:mes_unit:add")
  @PostMapping("/add")
  public Result<String> add(@RequestBody MesUnit model) {
    mesUnitService.save(model);
    return Result.OK("添加成功！");
  }

  @AutoLog(value = "MES-单位信息-编辑")
  @Operation(summary = "MES-单位信息-编辑")
  @RequiresPermissions("mes:mes_unit:edit")
  @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
  public Result<String> edit(@RequestBody MesUnit model) {
    mesUnitService.updateById(model);
    return Result.OK("编辑成功!");
  }

  @AutoLog(value = "MES-单位信息-通过id删除")
  @Operation(summary = "MES-单位信息-通过id删除")
  @RequiresPermissions("mes:mes_unit:delete")
  @DeleteMapping("/delete")
  public Result<String> delete(@RequestParam(name = "id") String id) {
    mesUnitService.removeById(id);
    return Result.OK("删除成功!");
  }

  @AutoLog(value = "MES-单位信息-批量删除")
  @Operation(summary = "MES-单位信息-批量删除")
  @RequiresPermissions("mes:mes_unit:deleteBatch")
  @DeleteMapping("/deleteBatch")
  public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
    mesUnitService.removeByIds(Arrays.asList(ids.split(",")));
    return Result.OK("批量删除成功!");
  }

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

  @RequiresPermissions("mes:mes_unit:exportXls")
  @RequestMapping("/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, MesUnit model) {
    return super.exportXls(request, model, MesUnit.class, "MES单位");
  }

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