TPpeController.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.ruoyi.project.affair.controller;
  2. import java.io.IOException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import com.alibaba.fastjson.JSON;
  8. import com.ruoyi.common.utils.DateUtils;
  9. import com.ruoyi.common.utils.file.ExcelUtils;
  10. import com.ruoyi.project.system.domain.SysDept;
  11. import com.ruoyi.project.system.domain.SysDictData;
  12. import com.ruoyi.project.system.service.ISysDeptService;
  13. import com.ruoyi.project.system.service.ISysDictTypeService;
  14. import org.apache.poi.ss.usermodel.*;
  15. import org.springframework.security.access.prepost.PreAuthorize;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.web.bind.annotation.*;
  18. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  19. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  20. import com.ruoyi.project.affair.domain.TPpe;
  21. import com.ruoyi.project.affair.service.ITPpeService;
  22. import com.ruoyi.framework.web.controller.BaseController;
  23. import com.ruoyi.framework.web.domain.AjaxResult;
  24. import com.ruoyi.common.utils.poi.ExcelUtil;
  25. import com.ruoyi.framework.web.page.TableDataInfo;
  26. import org.springframework.web.multipart.MultipartFile;
  27. /**
  28. * PPE发放登记Controller
  29. *
  30. * @author ruoyi
  31. * @date 2020-12-02
  32. */
  33. @RestController
  34. @RequestMapping("/affair/ppe")
  35. public class TPpeController extends BaseController
  36. {
  37. @Autowired
  38. private ITPpeService tPpeService;
  39. @Autowired
  40. private ISysDeptService iSysDeptService;
  41. @Autowired
  42. private ISysDictTypeService iSysDictTypeService;
  43. /**
  44. * 查询PPE发放登记列表
  45. */
  46. @PreAuthorize("@ss.hasPermi('affair:ppe:list')")
  47. @GetMapping("/list")
  48. public TableDataInfo list(TPpe tPpe)
  49. {
  50. startPage();
  51. List<TPpe> list = tPpeService.selectTPpeList(tPpe);
  52. return getDataTable(list);
  53. }
  54. /**
  55. * 导出PPE发放登记列表
  56. */
  57. @PreAuthorize("@ss.hasPermi('affair:ppe:export')")
  58. @Log(title = "PPE发放登记", businessType = BusinessType.EXPORT)
  59. @GetMapping("/export")
  60. public AjaxResult export(TPpe tPpe)
  61. {
  62. List<TPpe> list = tPpeService.selectTPpeList(tPpe);
  63. ExcelUtil<TPpe> util = new ExcelUtil<TPpe>(TPpe.class);
  64. return util.exportExcel(list, "ppe");
  65. }
  66. /**
  67. * 获取PPE发放登记详细信息
  68. */
  69. @PreAuthorize("@ss.hasPermi('affair:ppe:query')")
  70. @GetMapping(value = "/{id}")
  71. public AjaxResult getInfo(@PathVariable("id") Long id)
  72. {
  73. return AjaxResult.success(tPpeService.selectTPpeById(id));
  74. }
  75. /**
  76. * 新增PPE发放登记
  77. */
  78. @PreAuthorize("@ss.hasPermi('affair:ppe:add')")
  79. @Log(title = "PPE发放登记", businessType = BusinessType.INSERT)
  80. @PostMapping
  81. public AjaxResult add(@RequestBody TPpe tPpe)
  82. {
  83. tPpe.setCreaterCode(getUserId().toString());
  84. return toAjax(tPpeService.insertTPpe(tPpe));
  85. }
  86. /**
  87. * 修改PPE发放登记
  88. */
  89. @PreAuthorize("@ss.hasPermi('affair:ppe:edit')")
  90. @Log(title = "PPE发放登记", businessType = BusinessType.UPDATE)
  91. @PutMapping
  92. public AjaxResult edit(@RequestBody TPpe tPpe)
  93. {
  94. tPpe.setUpdaterCode(getUserId().toString());
  95. tPpe.setUpdatedate(new Date());
  96. return toAjax(tPpeService.updateTPpe(tPpe));
  97. }
  98. /**
  99. * 删除PPE发放登记
  100. */
  101. @PreAuthorize("@ss.hasPermi('affair:ppe:remove')")
  102. @Log(title = "PPE发放登记", businessType = BusinessType.DELETE)
  103. @DeleteMapping("/{ids}")
  104. public AjaxResult remove(@PathVariable Long[] ids)
  105. {
  106. return toAjax(tPpeService.deleteTPpeByIds(ids));
  107. }
  108. /**
  109. * 批量导入PPE发放登记
  110. */
  111. @PreAuthorize("@ss.hasPermi('affair:ppe:add')")
  112. @Log(title = "PPE发放登记", businessType = BusinessType.INSERT)
  113. @PostMapping("/importData")
  114. public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException
  115. {
  116. //获取操作人员ID
  117. Long userId = getUserId();
  118. Workbook workbook = ExcelUtils.getWorkBook(file);
  119. Sheet sheet = workbook.getSheetAt(0);
  120. List<TPpe> list = new ArrayList<TPpe>();
  121. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  122. //字典查询
  123. List<SysDictData> plant = iSysDictTypeService.selectDictDataByType("PLANT_DIVIDE");
  124. //部门查询
  125. List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
  126. int rowNum = sheet.getPhysicalNumberOfRows();
  127. int failNumber = 0;
  128. //报错行数统计
  129. List<Integer> failRow =new ArrayList<Integer>();
  130. for (int i = 1; i < rowNum; i++) {
  131. try {
  132. logger.info("读取行数:" + i);
  133. Row row = sheet.getRow(i);
  134. int cellNum = row.getLastCellNum();
  135. TPpe entity = new TPpe();
  136. for (int j = 0; j < cellNum; j++) {
  137. Cell cell = row.getCell(j);
  138. // cell.setCellType(CellType.STRING);
  139. String cellValue = ExcelUtils.getCellValue(cell);
  140. logger.info("cellValue:" + cellValue);
  141. if (j == 0) {
  142. for (SysDictData p : plant) {
  143. if (p.getDictLabel().equals(cellValue.trim())) {
  144. entity.setPlantCode(p.getDictValue());//装置名称
  145. }
  146. }
  147. } else if (j == 1) {
  148. entity.setName(cellValue);//姓名
  149. } else if (j == 2) {
  150. entity.setPpename(cellValue);//名称
  151. } else if (j == 3) {
  152. entity.setSpecification(cellValue);//规格型号
  153. } else if (j == 4) {
  154. if (cellValue != ""){
  155. entity.setQuantity(Long.parseLong(cellValue));//数量
  156. }
  157. } else if (j == 5) {
  158. if (cellValue.length() > 3) {
  159. entity.setServicetime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));//使用期限
  160. }
  161. } else if (j == 6) {
  162. if (cellValue.length() > 3) {
  163. entity.setReceivedate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));//领用日期
  164. }
  165. } else if (j == 7) {
  166. for (SysDept d : dept) {
  167. if (d.getDeptName().equals(cellValue.trim())) {
  168. entity.setDeptId(d.getDeptId());//部门编号
  169. }
  170. }
  171. } else if (j == 8) {
  172. entity.setRemarks(cellValue);//备注
  173. }
  174. }
  175. entity.setCreaterCode(userId.toString());
  176. logger.info("entity:" + entity);
  177. list.add(entity);
  178. }catch (Exception e){
  179. failNumber++;
  180. failRow.add(i+1);
  181. }
  182. }
  183. int successNumber = 0;
  184. int failNum = 0;
  185. for (TPpe t : list
  186. ) {
  187. failNum++;
  188. try {
  189. tPpeService.insertTPpe(t);
  190. successNumber++;
  191. }catch (Exception e){
  192. failNumber++;
  193. failRow.add(failNum+1);
  194. }
  195. }
  196. logger.info("list:" + JSON.toJSONString(list));
  197. logger.info("successNumber:" +String.valueOf(successNumber));
  198. logger.info("failNumber:" +String.valueOf(failNumber));
  199. logger.info("failRow:" +String.valueOf(failRow));
  200. return AjaxResult.success(String.valueOf(successNumber), failRow);
  201. }
  202. }