TMsdsController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package com.ruoyi.project.ehs.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.plant.domain.TStaffmgr;
  11. import com.ruoyi.project.plant.service.ITStaffmgrService;
  12. import com.ruoyi.project.system.domain.SysDept;
  13. import com.ruoyi.project.system.domain.SysDictData;
  14. import com.ruoyi.project.system.service.ISysDeptService;
  15. import com.ruoyi.project.system.service.ISysDictTypeService;
  16. import org.apache.poi.ss.usermodel.*;
  17. import org.springframework.security.access.prepost.PreAuthorize;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.annotation.*;
  20. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  21. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  22. import com.ruoyi.project.ehs.domain.TMsds;
  23. import com.ruoyi.project.ehs.service.ITMsdsService;
  24. import com.ruoyi.framework.web.controller.BaseController;
  25. import com.ruoyi.framework.web.domain.AjaxResult;
  26. import com.ruoyi.common.utils.poi.ExcelUtil;
  27. import com.ruoyi.framework.web.page.TableDataInfo;
  28. import org.springframework.web.multipart.MultipartFile;
  29. /**
  30. * MSDS管理Controller
  31. *
  32. * @author ruoyi
  33. * @date 2020-12-09
  34. */
  35. @RestController
  36. @RequestMapping("/ehs/msds")
  37. public class TMsdsController extends BaseController
  38. {
  39. @Autowired
  40. private ITMsdsService tMsdsService;
  41. @Autowired
  42. private ISysDictTypeService iSysDictTypeService;
  43. @Autowired
  44. private ISysDeptService iSysDeptService;
  45. @Autowired
  46. private ITStaffmgrService tStaffmgrService;
  47. /**
  48. * 查询MSDS管理列表
  49. */
  50. @PreAuthorize("@ss.hasPermi('ehs:msds:list')")
  51. @GetMapping("/list")
  52. public TableDataInfo list(TMsds tMsds)
  53. {
  54. startPage();
  55. logger.info("msds:" + tMsds);
  56. List<TMsds> list = tMsdsService.selectTMsdsList(tMsds);
  57. return getDataTable(list);
  58. }
  59. /**
  60. * 导出MSDS管理列表
  61. */
  62. @PreAuthorize("@ss.hasPermi('ehs:msds:export')")
  63. @Log(title = "MSDS管理", businessType = BusinessType.EXPORT)
  64. @GetMapping("/export")
  65. public AjaxResult export(TMsds tMsds)
  66. {
  67. List<TMsds> list = tMsdsService.selectTMsdsList(tMsds);
  68. ExcelUtil<TMsds> util = new ExcelUtil<TMsds>(TMsds.class);
  69. return util.exportExcel(list, "msds");
  70. }
  71. /**
  72. * 获取MSDS管理详细信息
  73. */
  74. @PreAuthorize("@ss.hasPermi('ehs:msds:query')")
  75. @GetMapping(value = "/{id}")
  76. public AjaxResult getInfo(@PathVariable("id") Long id)
  77. {
  78. return AjaxResult.success(tMsdsService.selectTMsdsById(id));
  79. }
  80. /**
  81. * 新增MSDS管理
  82. */
  83. @PreAuthorize("@ss.hasPermi('ehs:msds:add')")
  84. @Log(title = "MSDS管理", businessType = BusinessType.INSERT)
  85. @PostMapping
  86. public AjaxResult add(@RequestBody TMsds tMsds)
  87. {
  88. tMsds.setCreaterCode(getUserId().toString());
  89. return toAjax(tMsdsService.insertTMsds(tMsds));
  90. }
  91. /**
  92. * 修改MSDS管理
  93. */
  94. @PreAuthorize("@ss.hasPermi('ehs:msds:edit')")
  95. @Log(title = "MSDS管理", businessType = BusinessType.UPDATE)
  96. @PutMapping
  97. public AjaxResult edit(@RequestBody TMsds tMsds)
  98. {
  99. tMsds.setUpdaterCode(getUserId().toString());
  100. tMsds.setUpdatedate(new Date());
  101. return toAjax(tMsdsService.updateTMsds(tMsds));
  102. }
  103. /**
  104. * 删除MSDS管理
  105. */
  106. @PreAuthorize("@ss.hasPermi('ehs:msds:remove')")
  107. @Log(title = "MSDS管理", businessType = BusinessType.DELETE)
  108. @DeleteMapping("/{ids}")
  109. public AjaxResult remove(@PathVariable Long[] ids)
  110. {
  111. return toAjax(tMsdsService.deleteTMsdsByIds(ids));
  112. }
  113. /**
  114. * 批量导入MSDS管理
  115. */
  116. @PreAuthorize("@ss.hasPermi('ehs:msds:add')")
  117. @Log(title = "MSDS管理", businessType = BusinessType.INSERT)
  118. @PostMapping("/importData")
  119. public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException
  120. {
  121. //获取操作人员ID
  122. Long userId = getUserId();
  123. //报错行数统计
  124. List<Integer> failRow =new ArrayList<Integer>();
  125. Workbook workbook = ExcelUtils.getWorkBook(file);
  126. Sheet sheet = workbook.getSheetAt(0);
  127. List<TMsds> list = new ArrayList<TMsds>();
  128. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  129. //字典查询
  130. List<SysDictData> productCategory = iSysDictTypeService.selectDictDataByType("PRODUCT_CATEGORY");
  131. List<SysDictData> hazardPhrases = iSysDictTypeService.selectDictDataByType("HAZARD_PHRASES");
  132. List<SysDictData> cmr = iSysDictTypeService.selectDictDataByType("CMR");
  133. List<SysDictData> ghsPictogram = iSysDictTypeService.selectDictDataByType("GHS_PICTOGRAM");
  134. List<SysDictData> workArea = iSysDictTypeService.selectDictDataByType("WORK_AREA");
  135. List<SysDictData> reviewState = iSysDictTypeService.selectDictDataByType("REVIEW_STATE");
  136. //部门查询
  137. List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
  138. //人员查询
  139. List<TStaffmgr> staffmgrs = tStaffmgrService.selectTStaffmgrList(new TStaffmgr());
  140. int rowNum = sheet.getPhysicalNumberOfRows();
  141. int failNumber = 0;
  142. for (int i = 1; i < rowNum; i++) {
  143. try {
  144. logger.info("读取行数:" + i);
  145. Row row = sheet.getRow(i);
  146. int cellNum = row.getPhysicalNumberOfCells();
  147. TMsds entity = new TMsds();
  148. for (int j = 0; j < cellNum; j++) {
  149. Cell cell = row.getCell(j);
  150. cell.setCellType(CellType.STRING);
  151. String cellValue = ExcelUtils.getCellValue(cell);
  152. logger.info("cellValue:" + cellValue);
  153. if (j == 0) {
  154. entity.setItem(Long.parseLong(cellValue));//ITEM
  155. } else if (j == 1) {
  156. for (SysDictData p : productCategory) {
  157. if (p.getDictLabel().equals(cellValue.trim())) {
  158. entity.setProductcategory(p.getDictValue());//产品类别
  159. }
  160. }
  161. } else if (j == 2) {
  162. entity.setEnName(cellValue);//英文名称
  163. } else if (j == 3) {
  164. entity.setCnName(cellValue);//中文名称
  165. } else if (j == 4) {
  166. entity.setCasNo(cellValue);//CAS号
  167. } else if (j == 5) {
  168. if (!cellValue.equals("")) {
  169. String[] hazard = cellValue.split(",");
  170. String hazardNo = "";
  171. int m = 0;
  172. for (String h : hazard) {
  173. for (SysDictData p : hazardPhrases) {
  174. if (p.getDictLabel().equals(h)) {
  175. if (m == 0) {
  176. hazardNo = p.getDictValue();
  177. }else {
  178. hazardNo = hazardNo + "," + p.getDictValue();
  179. }
  180. }
  181. }
  182. m++;
  183. }
  184. entity.setHazardPhrases(hazardNo);//危险警句
  185. }
  186. } else if (j == 6) {
  187. if (!cellValue.equals("")) {
  188. String[] cmrSplit = cellValue.split(",");
  189. String cmrdNo = "";
  190. int m = 0;
  191. for (String choose : cmrSplit) {
  192. for (SysDictData c : cmr) {
  193. if (c.getDictLabel().equals(choose)) {
  194. if (m == 0) {
  195. cmrdNo = c.getDictValue();
  196. }else {
  197. cmrdNo = cmrdNo + "," + c.getDictValue();
  198. }
  199. }
  200. }
  201. m++;
  202. }
  203. entity.setCmr(cmrdNo);//是否三致物
  204. }
  205. } else if (j == 7) {
  206. if (!cellValue.equals("")) {
  207. String[] ghs = cellValue.split(",");
  208. String ghsNo = "";
  209. int m = 0;
  210. for (String choose : ghs) {
  211. for (SysDictData p : ghsPictogram) {
  212. if (p.getDictLabel().equals(choose)) {
  213. if (m == 0) {
  214. ghsNo = p.getDictValue();
  215. }else {
  216. ghsNo = ghsNo + "," + p.getDictValue();
  217. }
  218. }
  219. }
  220. m++;
  221. }
  222. entity.setGhsPictogram(ghsNo);//GHS符号
  223. }
  224. } else if (j == 8) {
  225. entity.setHoldup(cellValue);//保存量
  226. } else if (j == 9) {
  227. if (!cellValue.equals("")) {
  228. String[] area = cellValue.split(",");
  229. String areaNo = "";
  230. int m = 0;
  231. for (String choose : area) {
  232. for (SysDictData w : workArea) {
  233. if (w.getDictLabel().equals(choose)) {
  234. if (m == 0) {
  235. areaNo = w.getDictValue();
  236. }else {
  237. areaNo = areaNo + "," + w.getDictValue();
  238. }
  239. }
  240. }
  241. m++;
  242. }
  243. entity.setWorkArea(areaNo);//工作区域
  244. }
  245. } else if (j == 10) {
  246. entity.setForm(cellValue);//形态
  247. } else if (j == 11) {
  248. entity.setExplosionLimit(cellValue);//爆炸极限
  249. } else if (j == 12) {
  250. entity.setMeltingPoint(cellValue);//熔点
  251. } else if (j == 13) {
  252. entity.setBoilingPoint(cellValue);//沸点
  253. } else if (j == 14) {
  254. entity.setDensity(cellValue);//密度/分子量
  255. } else if (j == 15) {
  256. entity.setPurity(cellValue);//组成及纯度
  257. } else if (j == 16) {
  258. entity.setResponsibleCompany(cellValue);//归属单位
  259. } else if (j == 17) {
  260. if (cellValue.length() > 3) {
  261. entity.setIssueTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));//出版日期
  262. }
  263. } else if (j == 18) {
  264. entity.setVersion(cellValue);//版次
  265. } else if (j == 20) {
  266. for (TStaffmgr s : staffmgrs) {
  267. if (s.getStaffid().equals(cellValue.trim())) {
  268. entity.setReviewer(s.getStaffid());//回顾人
  269. }
  270. }
  271. } else if (j == 21) {
  272. if (cellValue.length() > 3) {
  273. entity.setReviewdate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));//回顾日期
  274. }
  275. } else if (j == 22) {
  276. for (SysDictData r : reviewState) {
  277. if (r.getDictLabel().equals(cellValue.trim())) {
  278. entity.setReviewState(r.getDictValue());//回顾状态
  279. }
  280. }
  281. } else if (j == 23) {
  282. if (cellValue.length() > 3) {
  283. entity.setNextReviewdate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));//下次回顾日期
  284. }
  285. } else if (j == 24) {
  286. entity.setRemarks(cellValue);//备注
  287. } else if (j == 25) {
  288. for (SysDept d : dept) {
  289. if (d.getDeptName().equals(cellValue.trim())) {
  290. entity.setDeptId(d.getDeptId());//部门编号
  291. }
  292. }
  293. }
  294. }
  295. entity.setCreaterCode(userId.toString());
  296. logger.info("entity:" + entity);
  297. list.add(entity);
  298. }catch (Exception e){
  299. failNumber++;
  300. failRow.add(i+1);
  301. }
  302. }
  303. int successNumber = 0;
  304. int failNum = 0;
  305. for (TMsds t : list
  306. ) {
  307. failNum++;
  308. try {
  309. tMsdsService.insertTMsds(t);
  310. successNumber++;
  311. }catch (Exception e){
  312. failNumber++;
  313. failRow.add(failNum+1);
  314. }
  315. }
  316. logger.info("list:" + JSON.toJSONString(list));
  317. logger.info("successNumber:" +String.valueOf(successNumber));
  318. logger.info("failNumber:" +String.valueOf(failNumber));
  319. logger.info("failRow:" +String.valueOf(failRow));
  320. return AjaxResult.success(String.valueOf(successNumber), failRow);
  321. }
  322. }