From 432a1b0c4dd251f686e9bb7950c4868aaf3e4ed7 Mon Sep 17 00:00:00 2001 From: liuxm <liuxm_a@163.com> Date: 星期四, 06 六月 2024 09:55:57 +0800 Subject: [PATCH] 添加验证和异常处理 --- pms-parent/pms-web-platform/src/main/java/com/dy/pmsPlatform/product/ProductCtrl.java | 78 ++++++++++++++++++++++++++++++++++---- 1 files changed, 69 insertions(+), 9 deletions(-) diff --git a/pms-parent/pms-web-platform/src/main/java/com/dy/pmsPlatform/product/ProductCtrl.java b/pms-parent/pms-web-platform/src/main/java/com/dy/pmsPlatform/product/ProductCtrl.java index f2e651e..4b54650 100644 --- a/pms-parent/pms-web-platform/src/main/java/com/dy/pmsPlatform/product/ProductCtrl.java +++ b/pms-parent/pms-web-platform/src/main/java/com/dy/pmsPlatform/product/ProductCtrl.java @@ -1,18 +1,23 @@ package com.dy.pmsPlatform.product; -import com.alibaba.fastjson2.JSON; +import com.alibaba.excel.converters.Converter; import com.dy.common.aop.SsoPowerAop; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.common.webUtil.QueryResultVo; import com.dy.pmsGlobal.aop.Log; import com.dy.pmsGlobal.pojoPlt.PltProduct; +import com.dy.pmsGlobal.util.QrCodeUtil; +import com.google.zxing.WriterException; +import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; +import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -32,7 +37,8 @@ this.proSv = proSv; } - + private static final String fileName = "浜у搧淇℃伅" ; + private static final String sheetName = "浜у搧淇℃伅" ; /** * 淇濆瓨浜у搧淇℃伅 @@ -100,12 +106,18 @@ @GetMapping(path="delete") @SsoPowerAop(power = "10300001") @Log("鍒犻櫎浜у搧淇℃伅") - public BaseResponse<PltProduct> delete(String id){ + public BaseResponse<Boolean> delete(String id){ + int count; try { - return BaseResponseUtils.buildSuccess(proSv.delete(Long.parseLong(id))); + count = proSv.delete(Long.parseLong(id)); }catch (Exception e){ log.error("鍒犻櫎浜у搧寮傚父", e); return BaseResponseUtils.buildException(e.getMessage()); + } + if (count <= 0) { + return BaseResponseUtils.buildFail("鏁版嵁搴撳瓨鍌ㄥけ璐�"); + } else { + return BaseResponseUtils.buildSuccess(true); } } @@ -121,9 +133,9 @@ try{ PltProduct pro=proSv.selectById(id); - return BaseResponseUtils.buildSuccess(JSON.toJSON(pro)); + return BaseResponseUtils.buildSuccess(pro); }catch (Exception e){ - log.error("鏌ヨ浜у搧寮傚父", e); + log.error("鏍规嵁ID鏌ヨ浜у搧寮傚父", e); return BaseResponseUtils.buildException(e.getMessage()); } } @@ -135,14 +147,62 @@ */ @PostMapping(path="some") @SsoPowerAop(power = "10300000") - @Log("鏌ヨ浜у搧淇℃伅") + @Log("鍒嗛〉鏌ヨ浜у搧淇℃伅") public BaseResponse<QueryResultVo<List<PltProduct>>> some(@RequestBody QueryVo vo){ try { - QueryResultVo<List<PltProduct>> list = proSv.selectSome(vo) ; + QueryResultVo<List<PltProduct>> list = proSv.selectSome(vo); return BaseResponseUtils.buildSuccess(list); }catch (Exception e){ - log.error("鏌ヨ浜у搧寮傚父", e); + log.error("鍒嗛〉鏌ヨ浜у搧寮傚父", e); return BaseResponseUtils.buildException(e.getMessage()); } } + + /** + * 鏌ヨ鎵�鏈変骇鍝� + * @return + */ + @GetMapping(path="all") + @SsoPowerAop(power = "10300000") + @Log("鏌ヨ鎵�鏈変骇鍝�") + public BaseResponse<List<PltProduct>> all(){ + try { + QueryVo vo = new QueryVo(); + return BaseResponseUtils.buildSuccess(proSv.selectAll(vo)); + }catch (Exception e){ + log.error("鏌ヨ鎵�鏈変骇鍝佸紓甯�", e); + return BaseResponseUtils.buildException(e.getMessage()); + } + } + + @PostMapping(path="export") + @SsoPowerAop(power = "10300000") + @Log("瀵煎嚭浜у搧淇℃伅") + public void export(@RequestBody QueryVo queryVo, HttpServletResponse response){ + try{ + List<Converter> list = new ArrayList<>() ; + + List<PltProduct> porList = proSv.selectAll(queryVo); + // 浣跨敤骞惰娴佹彁楂樻�ц兘 + porList.parallelStream().forEach(pro -> { + ExcelVo vo = new ExcelVo(); + vo.name = pro.name; + vo.type = pro.type; + vo.code = pro.code; + try { + vo.qrCode = QrCodeUtil.genQrCode(pro.code); + } catch (IOException e) { + e.printStackTrace(); + } catch (WriterException e) { + e.printStackTrace(); + } + list.add(vo); + }); + QrCodeUtil.downloadExcel(response, fileName,sheetName,list); + }catch (Exception e){ + log.error("瀵煎嚭浜у搧淇℃伅寮傚父", e); + } + } + + } -- Gitblit v1.8.0