package com.vincent.rsf.server.manager.controller; import com.alibaba.fastjson.JSONObject; import com.vincent.rsf.framework.common.R; import com.vincent.rsf.framework.exception.CoolException; import com.vincent.rsf.server.common.annotation.OperationLog; import com.vincent.rsf.server.manager.entity.MatnrPrintTemplate; import com.vincent.rsf.server.manager.service.MatnrPrintTemplateService; import com.vincent.rsf.server.manager.utils.buildPageRowsUtils; import com.vincent.rsf.server.system.controller.BaseController; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import java.util.Arrays; import java.util.List; import java.util.Map; @RestController @Api(tags = "物料打印模板接口") public class MatnrPrintTemplateController extends BaseController { private final MatnrPrintTemplateService matnrPrintTemplateService; public MatnrPrintTemplateController(MatnrPrintTemplateService matnrPrintTemplateService) { this.matnrPrintTemplateService = matnrPrintTemplateService; } @PreAuthorize("hasAuthority('manager:matnrPrintTemplate:list')") @ApiOperation("查询物料打印模板列表") @PostMapping("/matnrPrintTemplate/list") public R list(@RequestBody(required = false) Map params) { List templates = matnrPrintTemplateService.listCurrentTenantTemplates(); return R.ok().add(buildPageRowsUtils.rowsMap(templates)); } @PreAuthorize("hasAuthority('manager:matnrPrintTemplate:list')") @ApiOperation("获取物料打印模板详情") @GetMapping("/matnrPrintTemplate/{id}") public R get(@PathVariable("id") Long id) { return R.ok(buildPageRowsUtils.rowsMap(matnrPrintTemplateService.getCurrentTenantTemplate(id))); } @PreAuthorize("hasAuthority('manager:matnrPrintTemplate:list')") @ApiOperation("获取默认物料打印模板") @GetMapping("/matnrPrintTemplate/default") public R getDefaultTemplate() { MatnrPrintTemplate template = matnrPrintTemplateService.getCurrentTenantDefaultTemplate(); if (template == null) { return R.ok(); } return R.ok(buildPageRowsUtils.rowsMap(template)); } @PreAuthorize("hasAuthority('manager:matnrPrintTemplate:save')") @OperationLog("Create 物料打印模板") @ApiOperation("保存物料打印模板") @PostMapping("/matnrPrintTemplate/save") public R save(@RequestBody Map params) { MatnrPrintTemplate template = JSONObject.parseObject(JSONObject.toJSONString(params), MatnrPrintTemplate.class); if (template == null) { throw new CoolException("模板参数不能为空"); } MatnrPrintTemplate saved = matnrPrintTemplateService.saveTemplate(template); return R.ok("Create Success").add(buildPageRowsUtils.rowsMap(saved)); } @PreAuthorize("hasAuthority('manager:matnrPrintTemplate:update')") @OperationLog("Update 物料打印模板") @ApiOperation("更新物料打印模板") @PostMapping("/matnrPrintTemplate/update") @Transactional(rollbackFor = Exception.class) public R update(@RequestBody Map params) { MatnrPrintTemplate template = JSONObject.parseObject(JSONObject.toJSONString(params), MatnrPrintTemplate.class); if (template == null || template.getId() == null) { throw new CoolException("模板ID不能为空"); } MatnrPrintTemplate updated = matnrPrintTemplateService.updateTemplate(template); return R.ok("Update Success").add(buildPageRowsUtils.rowsMap(updated)); } @PreAuthorize("hasAuthority('manager:matnrPrintTemplate:remove')") @OperationLog("Delete 物料打印模板") @ApiOperation("删除物料打印模板") @PostMapping("/matnrPrintTemplate/remove/{ids}") public R remove(@PathVariable Long[] ids) { matnrPrintTemplateService.removeTemplates(Arrays.asList(ids)); return R.ok("Delete Success").add(buildPageRowsUtils.rowsMap(ids)); } @PreAuthorize("hasAuthority('manager:matnrPrintTemplate:update')") @OperationLog("Set Default 物料打印模板") @ApiOperation("设置默认物料打印模板") @PostMapping("/matnrPrintTemplate/default/{id}") public R setDefault(@PathVariable("id") Long id) { matnrPrintTemplateService.setDefaultTemplate(id); return R.ok("Update Success"); } }