package zbooksoft.fams.controller; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.ruili.wcp.data.entity.management.User; import com.ruili.wcp.web.model.AjaxResponse; import com.ruili.wcp.web.model.ErrorInfo; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authz.annotation.RequiresUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import zbooksoft.fams.common.BusinessException; import zbooksoft.fams.service.LabelService; /** * 标签管理页面与接口:模板设计、批量打印、打印记录 * @author ycq * @date 2026-10-16 */ @Controller @RequestMapping("/fams/label") public class LabelController { private static final String JSON_UTF8 = "application/json; charset=utf-8"; private final ObjectMapper objectMapper = new ObjectMapper(); @Autowired private LabelService labelService; /** * 标签模板设计器页面 * @param templateId 模板ID * @return 设计器视图 */ @RequestMapping(value = "/design", method = RequestMethod.GET) @RequiresUser public ModelAndView design(@RequestParam Long templateId) throws JsonProcessingException { ModelAndView mv = new ModelAndView("fams/label/design"); mv.addObject("templateId", String.valueOf(templateId)); mv.addObject("fieldsJson", objectMapper.writeValueAsString(LabelService.LABEL_FIELDS)); return mv; } /** * 批量打印页面,选中的资产ID由列表页通过params.ids传入 * @return 打印视图 */ @RequestMapping(value = "/print", method = RequestMethod.GET) @RequiresUser public ModelAndView print() throws JsonProcessingException { ModelAndView mv = new ModelAndView("fams/label/print"); mv.addObject("fieldsJson", objectMapper.writeValueAsString(LabelService.LABEL_FIELDS)); return mv; } /** * 查单个模板 * @param templateId 模板ID * @return 模板数据 */ @RequestMapping(value = "/template", method = RequestMethod.GET, produces = JSON_UTF8) @ResponseBody @RequiresUser public Object template(@RequestParam Long templateId) { try { return new AjaxResponse(labelService.getTemplate(templateId)); } catch (BusinessException e) { return fail(e); } } /** * 查启用的标签模板 * @return 模板列表 */ @RequestMapping(value = "/templates", method = RequestMethod.GET, produces = JSON_UTF8) @ResponseBody @RequiresUser public Object templates() { return new AjaxResponse(labelService.listEnabledTemplates()); } /** * 保存模板布局 * @param templateId 模板ID * @param layoutJson 布局JSON * @return 保存结果 */ @RequestMapping(value = "/saveLayout", method = RequestMethod.POST, produces = JSON_UTF8) @ResponseBody @RequiresUser public Object saveLayout(@RequestParam Long templateId, @RequestParam String layoutJson) { try { labelService.saveLayout(templateId, layoutJson); return new AjaxResponse(true); } catch (BusinessException e) { return fail(e); } } /** * 查要打印标签的资产数据 * @param ids 逗号分隔的资产ID * @return 资产数据 */ @RequestMapping(value = "/printData", method = RequestMethod.POST, produces = JSON_UTF8) @ResponseBody @RequiresUser public Object printData(@RequestParam String ids) { try { return new AjaxResponse(labelService.loadPrintAssets(ids)); } catch (BusinessException e) { return fail(e); } } /** * 记录标签已打印 * @param ids 逗号分隔的资产ID * @param templateId 使用的模板ID * @return 记录条数 */ @RequestMapping(value = "/printed", method = RequestMethod.POST, produces = JSON_UTF8) @ResponseBody @RequiresUser public Object printed(@RequestParam String ids, @RequestParam Long templateId) { try { User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user"); return new AjaxResponse(labelService.recordPrint(ids, templateId, user == null ? null : user.getUserId(), user == null ? "" : user.getTrueName())); } catch (BusinessException e) { return fail(e); } } private static AjaxResponse fail(BusinessException e) { return new AjaxResponse(new ErrorInfo(e.getMessage()), false); } }