TTargetreviewController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package com.ruoyi.project.plant.controller;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6. import com.alibaba.fastjson.JSON;
  7. import com.ruoyi.common.utils.StringUtils;
  8. import com.ruoyi.common.utils.document.DocumentHandler;
  9. import com.ruoyi.common.utils.document.PDFTemplateUtil;
  10. import com.ruoyi.common.utils.file.ExcelUtils;
  11. import com.ruoyi.framework.config.RuoYiConfig;
  12. import com.ruoyi.project.plant.domain.TStaffmgr;
  13. import com.ruoyi.project.plant.domain.TTargetlist;
  14. import com.ruoyi.project.plant.service.ITStaffmgrService;
  15. import com.ruoyi.project.plant.service.ITTargetlistService;
  16. import com.ruoyi.project.system.domain.SysDept;
  17. import com.ruoyi.project.system.domain.SysDictData;
  18. import com.ruoyi.project.system.service.ISysDeptService;
  19. import com.ruoyi.project.system.service.ISysDictTypeService;
  20. import freemarker.template.Template;
  21. import org.apache.commons.collections4.CollectionUtils;
  22. import org.apache.poi.ss.usermodel.*;
  23. import org.springframework.security.access.prepost.PreAuthorize;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.web.bind.annotation.*;
  26. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  27. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  28. import com.ruoyi.project.plant.domain.TTargetreview;
  29. import com.ruoyi.project.plant.service.ITTargetreviewService;
  30. import com.ruoyi.framework.web.controller.BaseController;
  31. import com.ruoyi.framework.web.domain.AjaxResult;
  32. import com.ruoyi.common.utils.poi.ExcelUtil;
  33. import com.ruoyi.framework.web.page.TableDataInfo;
  34. import org.springframework.web.multipart.MultipartFile;
  35. import javax.servlet.http.HttpServletRequest;
  36. import javax.servlet.http.HttpServletResponse;
  37. /**
  38. * 目标回顾Controller
  39. *
  40. * @author ruoyi
  41. * @date 2020-11-25
  42. */
  43. @RestController
  44. @RequestMapping("/plant/targetreview")
  45. public class TTargetreviewController extends BaseController
  46. {
  47. @Autowired
  48. private ITTargetreviewService tTargetreviewService;
  49. @Autowired
  50. private ITTargetlistService tTargetlistService;
  51. @Autowired
  52. private ISysDeptService iSysDeptService;
  53. @Autowired
  54. private ISysDictTypeService iSysDictTypeService;
  55. @Autowired
  56. private ITStaffmgrService tStaffmgrService;
  57. /**
  58. * 查询目标回顾列表
  59. */
  60. @PreAuthorize("@ss.hasPermi('plant:targetreview:list')")
  61. @GetMapping("/list")
  62. public TableDataInfo list(TTargetreview tTargetreview)
  63. {
  64. startPage();
  65. List<TTargetreview> list;
  66. if (tTargetreview.getYear()== null) {
  67. Calendar calendar = Calendar.getInstance();
  68. tTargetreview.setYear(Long.valueOf(calendar.get(Calendar.YEAR)));
  69. list = tTargetreviewService.selectTTargetreviewList(tTargetreview);
  70. if (CollectionUtils.isEmpty(list)){
  71. tTargetreview.setYear(Long.valueOf(calendar.get(Calendar.YEAR)-1));
  72. list = tTargetreviewService.selectTTargetreviewList(tTargetreview);
  73. }
  74. } else {
  75. list = tTargetreviewService.selectTTargetreviewList(tTargetreview);
  76. }
  77. for (TTargetreview t : list) {
  78. if (t.getPrincipal() != null) {
  79. String[] principal = t.getPrincipal().split(",");
  80. String name = "";
  81. for (int i = 0; i < principal.length; i++) {
  82. TStaffmgr tStaffmgr = tStaffmgrService.selectTStaffmgrByStaffId(principal[i]);
  83. if (i == 0) {
  84. if (tStaffmgr!=null) {
  85. name = tStaffmgr.getName();
  86. }
  87. }else {
  88. name = name + "," + tStaffmgr.getName();
  89. }
  90. }
  91. t.setPrincipalName(name);
  92. }
  93. }
  94. return getDataTable(list);
  95. }
  96. /**
  97. * 导出目标回顾列表
  98. */
  99. @PreAuthorize("@ss.hasPermi('plant:targetreview:export')")
  100. @Log(title = "目标回顾", businessType = BusinessType.EXPORT)
  101. @GetMapping("/export")
  102. public AjaxResult export(TTargetreview tTargetreview)
  103. {
  104. List<TTargetreview> list = tTargetreviewService.selectTTargetreviewList(tTargetreview);
  105. ExcelUtil<TTargetreview> util = new ExcelUtil<TTargetreview>(TTargetreview.class);
  106. return util.exportExcel(list, "targetreview");
  107. }
  108. /**
  109. * 获取目标回顾详细信息
  110. */
  111. @PreAuthorize("@ss.hasPermi('plant:targetreview:query')")
  112. @GetMapping(value = "/{id}")
  113. public AjaxResult getInfo(@PathVariable("id") Long id)
  114. {
  115. return AjaxResult.success(tTargetreviewService.selectTTargetreviewById(id));
  116. }
  117. /**
  118. * 新增目标回顾
  119. */
  120. @PreAuthorize("@ss.hasPermi('plant:targetreview:add')")
  121. @Log(title = "目标回顾", businessType = BusinessType.INSERT)
  122. @PostMapping
  123. public AjaxResult add(@RequestBody TTargetreview tTargetreview)
  124. {
  125. tTargetreview.setCreaterCode(getUserId().toString());
  126. return toAjax(tTargetreviewService.insertTTargetreview(tTargetreview));
  127. }
  128. /**
  129. * 修改目标回顾
  130. */
  131. @PreAuthorize("@ss.hasPermi('plant:targetreview:edit')")
  132. @Log(title = "目标回顾", businessType = BusinessType.UPDATE)
  133. @PutMapping
  134. public AjaxResult edit(@RequestBody TTargetreview tTargetreview)
  135. {
  136. tTargetreview.setUpdaterCode(getUserId().toString());
  137. tTargetreview.setUpdatedate(new Date());
  138. return toAjax(tTargetreviewService.updateTTargetreview(tTargetreview));
  139. }
  140. /**
  141. * 删除目标回顾
  142. */
  143. @PreAuthorize("@ss.hasPermi('plant:targetreview:remove')")
  144. @Log(title = "目标回顾", businessType = BusinessType.DELETE)
  145. @DeleteMapping("/{ids}")
  146. public AjaxResult remove(@PathVariable Long[] ids)
  147. {
  148. return toAjax(tTargetreviewService.deleteTTargetreviewByIds(ids));
  149. }
  150. /**
  151. * 批量导入目标回顾
  152. */
  153. @PreAuthorize("@ss.hasPermi('plant:targetreview:add')")
  154. @Log(title = "目标回顾", businessType = BusinessType.INSERT)
  155. @PostMapping("/importData")
  156. public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException
  157. {
  158. //获取操作人员ID
  159. Long userId = getUserId();
  160. //报错行数统计
  161. List<Integer> failRow =new ArrayList<Integer>();
  162. Workbook workbook = ExcelUtils.getWorkBook(file);
  163. Sheet sheet = workbook.getSheetAt(0);
  164. List<TTargetreview> list = new ArrayList<TTargetreview>();
  165. //字典查询
  166. List<SysDictData> plant = iSysDictTypeService.selectDictDataByType("PLANT_DIVIDE");
  167. //部门查询
  168. List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
  169. int rowNum = sheet.getPhysicalNumberOfRows();
  170. int failNumber = 0;
  171. for (int i = 1; i < rowNum; i++) {
  172. try {
  173. logger.info("读取行数:" + i);
  174. Row row = sheet.getRow(i);
  175. int cellNum = row.getPhysicalNumberOfCells();
  176. TTargetreview entity = new TTargetreview();
  177. for (int j = 0; j < cellNum; j++) {
  178. Cell cell = row.getCell(j);
  179. cell.setCellType(CellType.STRING);
  180. String cellValue = ExcelUtils.getCellValue(cell);
  181. logger.info("cellValue:" + cellValue);
  182. if (j == 0) {
  183. for (SysDictData p : plant) {
  184. if (p.getDictLabel().equals(cellValue.trim())) {
  185. entity.setPlantCode(p.getDictValue());//装置名称
  186. }
  187. }
  188. } else if (j == 1) {
  189. entity.setItem(cellValue);//序号
  190. } else if (j == 2) {
  191. entity.setDescription(cellValue);//内容
  192. } else if (j == 3) {
  193. entity.setTargets(cellValue);//目标
  194. } else if (j == 4) {
  195. if (cellValue != "") {
  196. entity.setYear(Long.parseLong(cellValue));//年度
  197. }
  198. } else if (j == 5) {
  199. entity.setHalfyear(cellValue);//半年
  200. } else if (j == 6) {
  201. entity.setAnnual(cellValue);//全年
  202. } else if (j == 7) {
  203. for (SysDept d : dept) {
  204. if (d.getDeptName().equals(cellValue.trim())) {
  205. entity.setDeptId(d.getDeptId());//部门编号
  206. }
  207. }
  208. } else if (j == 8) {
  209. entity.setRemarks(cellValue);//备注
  210. }
  211. }
  212. entity.setCreaterCode(userId.toString());
  213. logger.info("entity:" + entity);
  214. list.add(entity);
  215. }catch (Exception e){
  216. failNumber++;
  217. failRow.add(i+1);
  218. }
  219. }
  220. int successNumber = 0;
  221. int failNum = 0;
  222. for (TTargetreview t : list
  223. ) {
  224. failNum++;
  225. try {
  226. tTargetreviewService.insertTTargetreview(t);
  227. successNumber++;
  228. }catch (Exception e){
  229. failNumber++;
  230. failRow.add(failNum+1);
  231. }
  232. }
  233. logger.info("list:" + JSON.toJSONString(list));
  234. logger.info("successNumber:" +String.valueOf(successNumber));
  235. logger.info("failNumber:" +String.valueOf(failNumber));
  236. logger.info("failRow:" +String.valueOf(failRow));
  237. return AjaxResult.success(String.valueOf(successNumber), failRow);
  238. }
  239. @RequestMapping("/exportPDF")
  240. public String exportPDF(@RequestParam String year, HttpServletRequest request, HttpServletResponse response) {
  241. OutputStream out= null;
  242. try {
  243. out = response.getOutputStream();
  244. //获取信息,就是上面的结构
  245. HashMap<String, Object> map = new HashMap<>();
  246. DocumentHandler dh = new DocumentHandler();
  247. Template t = dh.getTemplate();
  248. TTargetlist targetlist = new TTargetlist();
  249. targetlist.setYear(year);
  250. targetlist.setParentId(Long.parseLong("0"));
  251. List<TTargetlist> tTargetlists = tTargetlistService.selectTTargetlistList(targetlist);
  252. for (TTargetlist list : tTargetlists) {
  253. }
  254. map.put("targetlists", tTargetlists);
  255. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  256. PDFTemplateUtil.exportPdf("measuresFMaker.ftl", year + "目标回顾.pdf","file:/"+ RuoYiConfig.getProfile(), map, response);
  257. } catch (Exception e) {
  258. e.printStackTrace();
  259. }finally {
  260. if(out!=null)
  261. try {
  262. out.close();
  263. } catch (IOException e) {
  264. e.printStackTrace();
  265. }
  266. }
  267. return null;
  268. }
  269. }