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 java.util.List;
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.MesMaterial;
import org.jeecg.modules.mes.material.service.IMesMaterialService;
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/material")
public class MesMaterialController extends JeecgController<MesMaterial, IMesMaterialService> {
  @Autowired private IMesMaterialService mesMaterialService;

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

  @AutoLog(value = "MES-物料信息-添加")
  @Operation(summary = "MES-物料信息-添加")
  @RequiresPermissions("mes:mes_material:add")
  @PostMapping("/add")
  public Result<String> add(@RequestBody MesMaterial model) {
    mesMaterialService.save(model);
    mesMaterialService.notifyMaterialChanged("material.created", model);
    return Result.OK("添加成功！");
  }

  @AutoLog(value = "MES-物料信息-编辑")
  @Operation(summary = "MES-物料信息-编辑")
  @RequiresPermissions("mes:mes_material:edit")
  @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
  public Result<String> edit(@RequestBody MesMaterial model) {
    mesMaterialService.updateById(model);
    mesMaterialService.notifyMaterialChanged("material.updated", model);
    return Result.OK("编辑成功!");
  }

  @AutoLog(value = "MES-物料信息-通过id删除")
  @Operation(summary = "MES-物料信息-通过id删除")
  @RequiresPermissions("mes:mes_material:delete")
  @DeleteMapping("/delete")
  public Result<String> delete(@RequestParam(name = "id") String id) {
    MesMaterial model = mesMaterialService.getById(id);
    mesMaterialService.removeById(id);
    if (model != null) {
      mesMaterialService.notifyMaterialChanged("material.disabled", model);
    }
    return Result.OK("删除成功!");
  }

  @AutoLog(value = "MES-物料信息-批量删除")
  @Operation(summary = "MES-物料信息-批量删除")
  @RequiresPermissions("mes:mes_material:deleteBatch")
  @DeleteMapping("/deleteBatch")
  public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
    List<String> idList = Arrays.asList(ids.split(","));
    List<MesMaterial> deletedMaterials = mesMaterialService.listByIds(idList);
    mesMaterialService.removeByIds(idList);
    for (MesMaterial material : deletedMaterials) {
      mesMaterialService.notifyMaterialChanged("material.disabled", material);
    }
    return Result.OK("批量删除成功!");
  }

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

  @RequiresPermissions("mes:mes_material:exportXls")
  @RequestMapping("/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, MesMaterial model) {
    return super.exportXls(request, model, MesMaterial.class, "MES物料信息");
  }

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