|  | @ -0,0 +1,246 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.base.service.word;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.base.dao.word.BaseImportSensitiveDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.base.dao.word.BaseSensitiveDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.base.service.doctor.excelImport.BaseDoctorExcelDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.base.service.word.excelImport.BaseSensitiveExcelDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.base.useragent.UserAgent;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.sync.BaseSyncDataDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.words.BaseImportSensitiveDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.words.BaseSensitiveDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.file_upload.FileUploadService;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.iot.common.UploadVO;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.MixEnvelop;
 | 
	
		
			
				|  |  | import com.yihu.jw.rm.health.house.HealthyHouseMapping;
 | 
	
		
			
				|  |  | import com.yihu.jw.rm.iot.IotRequestMapping;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.mysql.query.BaseJpaService;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.apache.poi.hssf.usermodel.HSSFSheet;
 | 
	
		
			
				|  |  | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 | 
	
		
			
				|  |  | import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 | 
	
		
			
				|  |  | import org.apache.poi.ss.usermodel.Row;
 | 
	
		
			
				|  |  | import org.apache.poi.ss.usermodel.Sheet;
 | 
	
		
			
				|  |  | import org.apache.poi.ss.usermodel.Cell;
 | 
	
		
			
				|  |  | import org.apache.poi.xssf.usermodel.XSSFSheet;
 | 
	
		
			
				|  |  | import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.BeanPropertyRowMapper;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.servlet.ServletException;
 | 
	
		
			
				|  |  | import javax.servlet.ServletOutputStream;
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletResponse;
 | 
	
		
			
				|  |  | import java.io.*;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | import java.util.logging.Logger;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * @author HZY
 | 
	
		
			
				|  |  |  * @vsrsion 1.0
 | 
	
		
			
				|  |  |  * Created at 2020/7/6
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class BaseSensitiveService extends BaseJpaService<BaseSensitiveDO, BaseSensitiveDao> {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseSensitiveDao baseSensitiveDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseImportSensitiveDao baseImportSensitiveDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     FileUploadService fileUploadService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 创建敏感词汇
 | 
	
		
			
				|  |  |      * @param words
 | 
	
		
			
				|  |  |      * @param explanation
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void createSensitiveWords(String words, String explanation,String status,String operatorCode,String name) {
 | 
	
		
			
				|  |  |         BaseSensitiveDO baseSensitiveDO = new BaseSensitiveDO();
 | 
	
		
			
				|  |  |         baseSensitiveDO.setSensitiveWord(words);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setExplanation(explanation);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setStatus(status);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setAddTime(DateUtil.getNowDate());
 | 
	
		
			
				|  |  |         baseSensitiveDO.setOperatorCode(operatorCode);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setOperator(name);
 | 
	
		
			
				|  |  |         baseSensitiveDao.save(baseSensitiveDO);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 更新敏感词汇
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      * @param words
 | 
	
		
			
				|  |  |      * @param explanation
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void updateSensitiveWords(String id, String words, String explanation,String status,String operatorCode,String name) {
 | 
	
		
			
				|  |  |         BaseSensitiveDO baseSensitiveDO = baseSensitiveDao.findOne(id);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setSensitiveWord(words);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setExplanation(explanation);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setRenewTime(DateUtil.getNowDate());
 | 
	
		
			
				|  |  |         baseSensitiveDO.setOperatorCode(operatorCode);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setOperator(name);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setStatus(status);
 | 
	
		
			
				|  |  |         baseSensitiveDao.save(baseSensitiveDO);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 删除敏感词
 | 
	
		
			
				|  |  |      * @param ids
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void delSensitiveWords(String ids) {
 | 
	
		
			
				|  |  |         List<String> list = JSONObject.parseObject(ids, List.class);
 | 
	
		
			
				|  |  |         for (String id : list) {
 | 
	
		
			
				|  |  |             baseSensitiveDao.delete(id);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 修改敏感词状态
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      * @param status
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void updateSensitiveWordsStatus(String id, String status,String operatorCode,String name) {
 | 
	
		
			
				|  |  |         BaseSensitiveDO baseSensitiveDO = baseSensitiveDao.findOne(id);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setStatus(status);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setRenewTime(DateUtil.getNowDate());
 | 
	
		
			
				|  |  |         baseSensitiveDO.setOperatorCode(operatorCode);
 | 
	
		
			
				|  |  |         baseSensitiveDO.setOperator(name);
 | 
	
		
			
				|  |  |         baseSensitiveDao.save(baseSensitiveDO);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 查询所有敏感词
 | 
	
		
			
				|  |  |      * @param status
 | 
	
		
			
				|  |  |      * @param words
 | 
	
		
			
				|  |  |      * @param page
 | 
	
		
			
				|  |  |      * @param pageSize
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public MixEnvelop findSensitiveWords(String status, String words, Integer page, Integer pageSize) {
 | 
	
		
			
				|  |  |         StringBuffer sql = new StringBuffer("select * from base_sensitive s where 1=1 ");
 | 
	
		
			
				|  |  |         StringBuffer countSql = new StringBuffer("select count(*) count from base_sensitive s where 1=1 ");
 | 
	
		
			
				|  |  |         if (StringUtils.isNotEmpty(status)){
 | 
	
		
			
				|  |  |             sql.append(" AND s.status='").append(status).append("'");
 | 
	
		
			
				|  |  |             countSql.append(" AND s.status='").append(status).append("'");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (StringUtils.isNotEmpty(words)){
 | 
	
		
			
				|  |  |             sql.append(" AND (s.sensitive like'%").append(words).append("%'").append(" or ").append("s.explanation like'%").append(words).append("%')");
 | 
	
		
			
				|  |  |             countSql.append(" AND (s.sensitive like'%").append(words).append("%'").append(" or ").append("s.explanation like'%").append(words).append("%')");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         sql.append(" ORDER BY s.renew_time desc LIMIT ").append((page-1)*pageSize).append(",").append(pageSize);
 | 
	
		
			
				|  |  |         Logger.getAnonymousLogger().info("sql="+sql.toString());
 | 
	
		
			
				|  |  |         List<BaseSensitiveDO> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(BaseSensitiveDO.class));
 | 
	
		
			
				|  |  |         List<Map<String, Object>> mapList = jdbcTemplate.queryForList(countSql.toString());
 | 
	
		
			
				|  |  |         long count = (long)mapList.get(0).get("count");
 | 
	
		
			
				|  |  |         return MixEnvelop.getSuccessListWithPage(IotRequestMapping.Common.message_success_find,list,page,pageSize,count);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 导出敏感词日志
 | 
	
		
			
				|  |  |      * @param url
 | 
	
		
			
				|  |  |      * @param operatorCode
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public MixEnvelop exportSensitiveWordsLog(String url,String operatorCode){
 | 
	
		
			
				|  |  |         String sql="SELECT * FROM base_import_sensitive b WHERE b.create_time=(SELECT MAX(b.create_time) FROM base_import_sensitive b) and b.operator_code="+operatorCode;
 | 
	
		
			
				|  |  |         //获取日志
 | 
	
		
			
				|  |  |         List<BaseImportSensitiveDO> list =jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(BaseImportSensitiveDO.class));
 | 
	
		
			
				|  |  |         //拼接字符串
 | 
	
		
			
				|  |  |         StringBuffer text = new StringBuffer();
 | 
	
		
			
				|  |  |         for(BaseImportSensitiveDO one:list){
 | 
	
		
			
				|  |  |             text.append("第");
 | 
	
		
			
				|  |  |             text.append(one.getRow());
 | 
	
		
			
				|  |  |             text.append("行");
 | 
	
		
			
				|  |  |             text.append("   ");
 | 
	
		
			
				|  |  |             text.append(one.getProblem());
 | 
	
		
			
				|  |  |             text.append("\r\n");//换行字符
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         InputStream is = new ByteArrayInputStream(text.toString().getBytes());
 | 
	
		
			
				|  |  |         String name = genAttachmentFileName("导入日志.txt", "import.txt");
 | 
	
		
			
				|  |  |         UploadVO uploadVO=new UploadVO();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             Logger.getAnonymousLogger().info("文件上传参数=name"+name+"---url="+url);
 | 
	
		
			
				|  |  |             uploadVO = fileUploadService.uploadStream(is, name, url);
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |             Logger.getAnonymousLogger().info("文件上传失败");
 | 
	
		
			
				|  |  |             Logger.getAnonymousLogger().info(e.getMessage());
 | 
	
		
			
				|  |  |             return MixEnvelop.getError("日志导出失败");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return MixEnvelop.getSuccess("下载日志成功",uploadVO);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public  String genAttachmentFileName(String cnName, String defaultName) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             cnName = new String(cnName.getBytes("gb2312"), "ISO8859-1");
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             cnName = defaultName;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return cnName;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 导入敏感词
 | 
	
		
			
				|  |  |      * @param correctLs
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Map<String, Object> importSensitive(List<BaseSensitiveExcelDO> correctLs,String operatorCode,String name) {
 | 
	
		
			
				|  |  |         Map<String, Object> rs = new HashMap<>();
 | 
	
		
			
				|  |  |         int total = correctLs.size();
 | 
	
		
			
				|  |  |         rs.put("total",total);
 | 
	
		
			
				|  |  |         Date nowDate = DateUtil.getNowDate();
 | 
	
		
			
				|  |  |         for (BaseSensitiveExcelDO one : correctLs) {
 | 
	
		
			
				|  |  |             BaseSensitiveDO sensitive = baseSensitiveDao.findBySensitiveWord(one.getSensitiveWord());
 | 
	
		
			
				|  |  |             if (sensitive!=null){
 | 
	
		
			
				|  |  |                 BaseImportSensitiveDO importSensitiveDO = new BaseImportSensitiveDO();
 | 
	
		
			
				|  |  |                 importSensitiveDO.setRow(one.getId());
 | 
	
		
			
				|  |  |                 importSensitiveDO.setProblem("敏感词已存在");
 | 
	
		
			
				|  |  |                 importSensitiveDO.setCreateTime(nowDate);
 | 
	
		
			
				|  |  |                 importSensitiveDO.setOperatorCode(operatorCode);
 | 
	
		
			
				|  |  |                 importSensitiveDO.setOperator(name);
 | 
	
		
			
				|  |  |                 baseImportSensitiveDao.save(importSensitiveDO);
 | 
	
		
			
				|  |  |                 total-=1;
 | 
	
		
			
				|  |  |                 continue;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (StringUtils.isEmpty(one.getSensitiveWord())){
 | 
	
		
			
				|  |  |                 BaseImportSensitiveDO importSensitiveDO = new BaseImportSensitiveDO();
 | 
	
		
			
				|  |  |                 importSensitiveDO.setRow(one.getId());
 | 
	
		
			
				|  |  |                 importSensitiveDO.setProblem("敏感词为空");
 | 
	
		
			
				|  |  |                 importSensitiveDO.setCreateTime(nowDate);
 | 
	
		
			
				|  |  |                 importSensitiveDO.setOperatorCode(operatorCode);
 | 
	
		
			
				|  |  |                 importSensitiveDO.setOperator(name);
 | 
	
		
			
				|  |  |                 baseImportSensitiveDao.save(importSensitiveDO);
 | 
	
		
			
				|  |  |                 total-=1;
 | 
	
		
			
				|  |  |                 continue;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             BaseSensitiveDO baseSensitiveDO = new BaseSensitiveDO();
 | 
	
		
			
				|  |  |             baseSensitiveDO.setId(UUID.randomUUID().toString().replaceAll("-",""));
 | 
	
		
			
				|  |  |             baseSensitiveDO.setStatus("1");
 | 
	
		
			
				|  |  |             baseSensitiveDO.setExplanation(one.getExplanation());
 | 
	
		
			
				|  |  |             baseSensitiveDO.setSensitiveWord(one.getSensitiveWord());
 | 
	
		
			
				|  |  |             baseSensitiveDO.setOperatorCode(operatorCode);
 | 
	
		
			
				|  |  |             baseSensitiveDO.setOperator(name);
 | 
	
		
			
				|  |  |             baseSensitiveDO.setAddTime(nowDate);
 | 
	
		
			
				|  |  |             baseSensitiveDao.save(baseSensitiveDO);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         rs.put("success",total);
 | 
	
		
			
				|  |  |         return rs;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 |