123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- package com.ruoyi.project.ehs.controller;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import com.alibaba.fastjson.JSON;
- import com.ruoyi.common.utils.DateUtils;
- import com.ruoyi.common.utils.file.ExcelUtils;
- import com.ruoyi.project.plant.domain.TStaffmgr;
- import com.ruoyi.project.plant.service.ITStaffmgrService;
- import com.ruoyi.project.system.domain.SysDept;
- import com.ruoyi.project.system.domain.SysDictData;
- import com.ruoyi.project.system.service.ISysDeptService;
- import com.ruoyi.project.system.service.ISysDictTypeService;
- import org.apache.poi.ss.usermodel.*;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import com.ruoyi.framework.aspectj.lang.annotation.Log;
- import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
- import com.ruoyi.project.ehs.domain.TMsds;
- import com.ruoyi.project.ehs.service.ITMsdsService;
- import com.ruoyi.framework.web.controller.BaseController;
- import com.ruoyi.framework.web.domain.AjaxResult;
- import com.ruoyi.common.utils.poi.ExcelUtil;
- import com.ruoyi.framework.web.page.TableDataInfo;
- import org.springframework.web.multipart.MultipartFile;
- /**
- * MSDS管理Controller
- *
- * @author ruoyi
- * @date 2020-12-09
- */
- @RestController
- @RequestMapping("/ehs/msds")
- public class TMsdsController extends BaseController
- {
- @Autowired
- private ITMsdsService tMsdsService;
- @Autowired
- private ISysDictTypeService iSysDictTypeService;
- @Autowired
- private ISysDeptService iSysDeptService;
- @Autowired
- private ITStaffmgrService tStaffmgrService;
- /**
- * 查询MSDS管理列表
- */
- @PreAuthorize("@ss.hasPermi('ehs:msds:list')")
- @GetMapping("/list")
- public TableDataInfo list(TMsds tMsds)
- {
- startPage();
- logger.info("msds:" + tMsds);
- List<TMsds> list = tMsdsService.selectTMsdsList(tMsds);
- return getDataTable(list);
- }
- /**
- * 导出MSDS管理列表
- */
- @PreAuthorize("@ss.hasPermi('ehs:msds:export')")
- @Log(title = "MSDS管理", businessType = BusinessType.EXPORT)
- @GetMapping("/export")
- public AjaxResult export(TMsds tMsds)
- {
- List<TMsds> list = tMsdsService.selectTMsdsList(tMsds);
- ExcelUtil<TMsds> util = new ExcelUtil<TMsds>(TMsds.class);
- return util.exportExcel(list, "msds");
- }
- /**
- * 获取MSDS管理详细信息
- */
- @PreAuthorize("@ss.hasPermi('ehs:msds:query')")
- @GetMapping(value = "/{id}")
- public AjaxResult getInfo(@PathVariable("id") Long id)
- {
- return AjaxResult.success(tMsdsService.selectTMsdsById(id));
- }
- /**
- * 新增MSDS管理
- */
- @PreAuthorize("@ss.hasPermi('ehs:msds:add')")
- @Log(title = "MSDS管理", businessType = BusinessType.INSERT)
- @PostMapping
- public AjaxResult add(@RequestBody TMsds tMsds)
- {
- tMsds.setCreaterCode(getUserId().toString());
- return toAjax(tMsdsService.insertTMsds(tMsds));
- }
- /**
- * 修改MSDS管理
- */
- @PreAuthorize("@ss.hasPermi('ehs:msds:edit')")
- @Log(title = "MSDS管理", businessType = BusinessType.UPDATE)
- @PutMapping
- public AjaxResult edit(@RequestBody TMsds tMsds)
- {
- tMsds.setUpdaterCode(getUserId().toString());
- tMsds.setUpdatedate(new Date());
- return toAjax(tMsdsService.updateTMsds(tMsds));
- }
- /**
- * 删除MSDS管理
- */
- @PreAuthorize("@ss.hasPermi('ehs:msds:remove')")
- @Log(title = "MSDS管理", businessType = BusinessType.DELETE)
- @DeleteMapping("/{ids}")
- public AjaxResult remove(@PathVariable Long[] ids)
- {
- return toAjax(tMsdsService.deleteTMsdsByIds(ids));
- }
- /**
- * 批量导入MSDS管理
- */
- @PreAuthorize("@ss.hasPermi('ehs:msds:add')")
- @Log(title = "MSDS管理", businessType = BusinessType.INSERT)
- @PostMapping("/importData")
- public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException
- {
- //获取操作人员ID
- Long userId = getUserId();
- //报错行数统计
- List<Integer> failRow =new ArrayList<Integer>();
- Workbook workbook = ExcelUtils.getWorkBook(file);
- Sheet sheet = workbook.getSheetAt(0);
- List<TMsds> list = new ArrayList<TMsds>();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- //字典查询
- List<SysDictData> productCategory = iSysDictTypeService.selectDictDataByType("PRODUCT_CATEGORY");
- List<SysDictData> hazardPhrases = iSysDictTypeService.selectDictDataByType("HAZARD_PHRASES");
- List<SysDictData> cmr = iSysDictTypeService.selectDictDataByType("CMR");
- List<SysDictData> ghsPictogram = iSysDictTypeService.selectDictDataByType("GHS_PICTOGRAM");
- List<SysDictData> workArea = iSysDictTypeService.selectDictDataByType("WORK_AREA");
- List<SysDictData> reviewState = iSysDictTypeService.selectDictDataByType("REVIEW_STATE");
- //部门查询
- List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
- //人员查询
- List<TStaffmgr> staffmgrs = tStaffmgrService.selectTStaffmgrList(new TStaffmgr());
- int rowNum = sheet.getPhysicalNumberOfRows();
- int failNumber = 0;
- for (int i = 1; i < rowNum; i++) {
- try {
- logger.info("读取行数:" + i);
- Row row = sheet.getRow(i);
- int cellNum = row.getPhysicalNumberOfCells();
- TMsds entity = new TMsds();
- for (int j = 0; j < cellNum; j++) {
- Cell cell = row.getCell(j);
- cell.setCellType(CellType.STRING);
- String cellValue = ExcelUtils.getCellValue(cell);
- logger.info("cellValue:" + cellValue);
- if (j == 0) {
- entity.setItem(Long.parseLong(cellValue));//ITEM
- } else if (j == 1) {
- for (SysDictData p : productCategory) {
- if (p.getDictLabel().equals(cellValue.trim())) {
- entity.setProductcategory(p.getDictValue());//产品类别
- }
- }
- } else if (j == 2) {
- entity.setEnName(cellValue);//英文名称
- } else if (j == 3) {
- entity.setCnName(cellValue);//中文名称
- } else if (j == 4) {
- entity.setCasNo(cellValue);//CAS号
- } else if (j == 5) {
- if (!cellValue.equals("")) {
- String[] hazard = cellValue.split(",");
- String hazardNo = "";
- int m = 0;
- for (String h : hazard) {
- for (SysDictData p : hazardPhrases) {
- if (p.getDictLabel().equals(h)) {
- if (m == 0) {
- hazardNo = p.getDictValue();
- }else {
- hazardNo = hazardNo + "," + p.getDictValue();
- }
- }
- }
- m++;
- }
- entity.setHazardPhrases(hazardNo);//危险警句
- }
- } else if (j == 6) {
- if (!cellValue.equals("")) {
- String[] cmrSplit = cellValue.split(",");
- String cmrdNo = "";
- int m = 0;
- for (String choose : cmrSplit) {
- for (SysDictData c : cmr) {
- if (c.getDictLabel().equals(choose)) {
- if (m == 0) {
- cmrdNo = c.getDictValue();
- }else {
- cmrdNo = cmrdNo + "," + c.getDictValue();
- }
- }
- }
- m++;
- }
- entity.setCmr(cmrdNo);//是否三致物
- }
- } else if (j == 7) {
- if (!cellValue.equals("")) {
- String[] ghs = cellValue.split(",");
- String ghsNo = "";
- int m = 0;
- for (String choose : ghs) {
- for (SysDictData p : ghsPictogram) {
- if (p.getDictLabel().equals(choose)) {
- if (m == 0) {
- ghsNo = p.getDictValue();
- }else {
- ghsNo = ghsNo + "," + p.getDictValue();
- }
- }
- }
- m++;
- }
- entity.setGhsPictogram(ghsNo);//GHS符号
- }
- } else if (j == 8) {
- entity.setHoldup(cellValue);//保存量
- } else if (j == 9) {
- if (!cellValue.equals("")) {
- String[] area = cellValue.split(",");
- String areaNo = "";
- int m = 0;
- for (String choose : area) {
- for (SysDictData w : workArea) {
- if (w.getDictLabel().equals(choose)) {
- if (m == 0) {
- areaNo = w.getDictValue();
- }else {
- areaNo = areaNo + "," + w.getDictValue();
- }
- }
- }
- m++;
- }
- entity.setWorkArea(areaNo);//工作区域
- }
- } else if (j == 10) {
- entity.setForm(cellValue);//形态
- } else if (j == 11) {
- entity.setExplosionLimit(cellValue);//爆炸极限
- } else if (j == 12) {
- entity.setMeltingPoint(cellValue);//熔点
- } else if (j == 13) {
- entity.setBoilingPoint(cellValue);//沸点
- } else if (j == 14) {
- entity.setDensity(cellValue);//密度/分子量
- } else if (j == 15) {
- entity.setPurity(cellValue);//组成及纯度
- } else if (j == 16) {
- entity.setResponsibleCompany(cellValue);//归属单位
- } else if (j == 17) {
- if (cellValue.length() > 3) {
- entity.setIssueTime(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));//出版日期
- }
- } else if (j == 18) {
- entity.setVersion(cellValue);//版次
- } else if (j == 20) {
- for (TStaffmgr s : staffmgrs) {
- if (s.getStaffid().equals(cellValue.trim())) {
- entity.setReviewer(s.getStaffid());//回顾人
- }
- }
- } else if (j == 21) {
- if (cellValue.length() > 3) {
- entity.setReviewdate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));//回顾日期
- }
- } else if (j == 22) {
- for (SysDictData r : reviewState) {
- if (r.getDictLabel().equals(cellValue.trim())) {
- entity.setReviewState(r.getDictValue());//回顾状态
- }
- }
- } else if (j == 23) {
- if (cellValue.length() > 3) {
- entity.setNextReviewdate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));//下次回顾日期
- }
- } else if (j == 24) {
- entity.setRemarks(cellValue);//备注
- } else if (j == 25) {
- for (SysDept d : dept) {
- if (d.getDeptName().equals(cellValue.trim())) {
- entity.setDeptId(d.getDeptId());//部门编号
- }
- }
- }
- }
- entity.setCreaterCode(userId.toString());
- logger.info("entity:" + entity);
- list.add(entity);
- }catch (Exception e){
- failNumber++;
- failRow.add(i+1);
- }
- }
- int successNumber = 0;
- int failNum = 0;
- for (TMsds t : list
- ) {
- failNum++;
- try {
- tMsdsService.insertTMsds(t);
- successNumber++;
- }catch (Exception e){
- failNumber++;
- failRow.add(failNum+1);
- }
- }
- logger.info("list:" + JSON.toJSONString(list));
- logger.info("successNumber:" +String.valueOf(successNumber));
- logger.info("failNumber:" +String.valueOf(failNumber));
- logger.info("failRow:" +String.valueOf(failRow));
- return AjaxResult.success(String.valueOf(successNumber), failRow);
- }
- }
|