PartyBuildingMultiplierController.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.ims.eval.controller;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.ims.eval.config.CustomException;
  4. import com.ims.eval.entity.PartyBuildingMultiplier;
  5. import com.ims.eval.entity.dto.result.R;
  6. import com.ims.eval.service.IPartyBuildingMultiplierService;
  7. import com.ims.eval.util.ExcelUtil;
  8. import com.ims.eval.util.ExcelUtils;
  9. import io.swagger.annotations.ApiOperation;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.web.bind.annotation.*;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.beans.IntrospectionException;
  18. import java.io.*;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. /**
  24. * <p>
  25. * 前端控制器
  26. * </p>
  27. *
  28. * @author wang
  29. * @since 2023-03-29
  30. */
  31. @Slf4j
  32. @RestController
  33. @RequestMapping("//party-building-multiplier")
  34. public class PartyBuildingMultiplierController {
  35. @Autowired
  36. private IPartyBuildingMultiplierService iPartyBuildingMultiplierService;
  37. /**
  38. * 查询
  39. *
  40. * @param id 主键ID
  41. * @param departId 部门id
  42. * @param sectionId 板块id
  43. * @param year 年度
  44. * @return
  45. */
  46. @GetMapping(value = "/list")
  47. public R list(@RequestParam(value = "pageNum", required = false) Integer pageNum,
  48. @RequestParam(value = "pageSize", required = false) Integer pageSize,
  49. @RequestParam(value = "id", required = false) String id,
  50. @RequestParam(value = "departId", required = false) String departId,
  51. @RequestParam(value = "sectionId", required = false) String sectionId,
  52. @RequestParam(value = "year", required = false) String year) {
  53. IPage<PartyBuildingMultiplier> list = iPartyBuildingMultiplierService.listAll(pageNum,pageSize,id, departId, sectionId, year);
  54. return R.ok().data(list);
  55. }
  56. /**
  57. * 查询
  58. *
  59. * @param id 主键ID
  60. * @param departId 部门id
  61. * @param sectionId 板块id
  62. * @param year 年度
  63. * @return
  64. */
  65. @GetMapping(value = "/export1")
  66. public void export1(HttpServletResponse response,
  67. @RequestParam(value = "id", required = false) String id,
  68. @RequestParam(value = "departId", required = false) String departId,
  69. @RequestParam(value = "sectionId", required = false) String sectionId,
  70. @RequestParam(value = "year", required = false) String year) throws IOException, IllegalAccessException, IntrospectionException, InvocationTargetException {
  71. List<PartyBuildingMultiplier> list = iPartyBuildingMultiplierService.listByParam(id, departId, sectionId, year);
  72. if (list.size() > 0){
  73. OutputStream out = response.getOutputStream();
  74. List<String> titleList = new ArrayList<>();
  75. titleList.add("id");
  76. titleList.add("单位名称");
  77. titleList.add("板块名称");
  78. titleList.add("党建乘数");
  79. titleList.add("年度");
  80. titleList.add("备注");
  81. HSSFWorkbook workBook = ExcelUtils.getWorkBook(titleList, list);
  82. //响应到客户端
  83. try {
  84. this.setResponseHeader(response, "党建乘数.xls");
  85. OutputStream os = response.getOutputStream();
  86. BufferedOutputStream bufferOutput =new BufferedOutputStream(os);
  87. bufferOutput.flush();
  88. workBook.write(bufferOutput);
  89. bufferOutput.close();
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. }
  93. }else {
  94. throw new CustomException("党建乘数数据为空!");
  95. }
  96. }
  97. /**
  98. * 发送响应流方法
  99. */
  100. public void setResponseHeader(HttpServletResponse response, String fileName) {
  101. try {
  102. try {
  103. fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
  104. } catch (UnsupportedEncodingException e) {
  105. // TODO Auto-generated catch block
  106. e.printStackTrace();
  107. }
  108. response.setContentType("application/vnd.ms-excel;charset=UTF-8");
  109. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
  110. response.addHeader("Pargam", "no-cache");
  111. response.addHeader("Cache-Control", "no-cache");
  112. response.setHeader("Expires", " 0");
  113. } catch (Exception ex) {
  114. ex.printStackTrace();
  115. }
  116. }
  117. @PostMapping(value = "/import")
  118. @ResponseBody
  119. public R importAlertrule(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
  120. if (!file.isEmpty()) {
  121. try {
  122. //获取原始的文件名
  123. String originalFilename = file.getOriginalFilename();
  124. //获取文件类型
  125. String fileType = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
  126. //默认从第一行开始读取
  127. Integer startRows = 1;
  128. //获取输入流
  129. InputStream is = file.getInputStream();
  130. List<PartyBuildingMultiplier> bindingList = new ArrayList<>();
  131. //Excel导入导出的单元类
  132. List<String[]> strings = ExcelUtil.readData(fileType, startRows, true, is);
  133. //遍历Excel表每一行的数据
  134. for (String[] str : strings) {
  135. PartyBuildingMultiplier partyBuildingMultiplier = new PartyBuildingMultiplier();
  136. partyBuildingMultiplier.setId(str[0]);
  137. partyBuildingMultiplier.setMultiplier(str[3]);
  138. partyBuildingMultiplier.setYear(str[4]);
  139. partyBuildingMultiplier.setRemarks(str[5]);
  140. bindingList.add(partyBuildingMultiplier);
  141. }
  142. boolean b = iPartyBuildingMultiplierService.saveOrUpdateBatch(bindingList);
  143. if (b) {
  144. return R.ok().data(b);
  145. } else {
  146. return R.error().data("保存失败!");
  147. }
  148. } catch (Exception e) {
  149. log.error("错误", e);
  150. return R.customError(e.getMessage()).data("失败!");
  151. }
  152. }
  153. return R.customError("上传文件为空!");
  154. }
  155. /**
  156. * 添加
  157. *
  158. * @param partyBuildingMultiplier
  159. * @return
  160. */
  161. @PostMapping(value = "/save")
  162. @ApiOperation(value = "新增(修改)", notes = "新增(修改)")
  163. public R addAll(@RequestBody PartyBuildingMultiplier partyBuildingMultiplier) {
  164. try {
  165. boolean b = iPartyBuildingMultiplierService.saveOrUpdate(partyBuildingMultiplier);
  166. if (b) {
  167. return R.ok().data(b);
  168. } else {
  169. return R.error().data("保存失败!");
  170. }
  171. } catch (CustomException e){
  172. return R.customError(e.getMessage()).data("失败!");
  173. }
  174. }
  175. /**
  176. * 批量删除
  177. *
  178. * @param ids
  179. * @return
  180. */
  181. @PostMapping(value = "/remove/{ids}")
  182. @ApiOperation(value = "删除", notes = "删除")
  183. public R deleteAll(@PathVariable("ids") String ids) {
  184. String[] strings = ids.split(",");
  185. boolean b = iPartyBuildingMultiplierService.removeByIds(Arrays.asList(strings));
  186. if (b) {
  187. return R.ok().data(b);
  188. } else {
  189. return R.error().data("删除失败!");
  190. }
  191. }
  192. }