package com.jpsoft.excellent.modules; import com.github.pagehelper.Page; import com.jpsoft.excellent.config.OSSConfig; import com.jpsoft.excellent.modules.base.entity.*; import com.jpsoft.excellent.modules.base.service.*; import com.jpsoft.excellent.modules.common.dto.MessageResult; import com.jpsoft.excellent.modules.common.dto.Sort; import com.jpsoft.excellent.modules.common.utils.DES3; import com.jpsoft.excellent.modules.common.utils.OSSUtil; import com.jpsoft.excellent.modules.common.utils.POIUtils; import com.jpsoft.excellent.modules.common.utils.SMSUtil; import com.jpsoft.excellent.modules.sys.entity.DataDictionary; import com.jpsoft.excellent.modules.sys.entity.User; import com.jpsoft.excellent.modules.sys.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.*; @RestController @RequestMapping("/text") @Api(description = "测试") public class textController { private Logger logger = LoggerFactory.getLogger(getClass()); @Value("${jwt.secret}") private String jwtSecret; @Autowired private OSSConfig ossConfig; @Autowired private UserService userService; @Autowired private PersonService personService; @Autowired private IncidentService incidentService; @Autowired private IncidentStepService incidentStepService; @Autowired private AreaService areaService; @Autowired private WorkStationService workStationService; @Autowired private FeedbackOpinionService feedbackOpinionService; @ApiOperation(value="短信发送") @RequestMapping(value = "sendSMS",method = RequestMethod.POST) public MessageResult sendSMS(@RequestAttribute String subject){ MessageResult msgResult = new MessageResult(); String MessageContent = "【双优督办】干部监督评议有一条投诉,请查收。"; String UserNumber = "19972671252,15827719088"; try { MessageResult message = SMSUtil.sendSMS(MessageContent, UserNumber, null); // message = SMSUtil.sendSMS(MessageContent, UserNumber, null); Map map = (Map) message.getData(); if("success".equals(map.get("returnstatus"))) { msgResult.setCode(200); msgResult.setResult(true); msgResult.setMessage(map.get("code").toString()); } else { msgResult.setCode(203); msgResult.setResult(true); msgResult.setMessage(map.get("code").toString()); msgResult.setData(message.getData()); } } catch(Exception ex){ msgResult.setCode(400); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="人员转用户") @RequestMapping(value = "personToUser",method = RequestMethod.POST) public MessageResult personToUser(@RequestAttribute String subject){ MessageResult msgResult = new MessageResult(); try { Integer count = 0; List persons = personService.list(); for(Person person : persons){ DES3 des3 = new DES3(); User user = new User(); user.setId(UUID.randomUUID().toString()); user.setOrgId(person.getOrgId()); user.setUserName(person.getPhone()); user.setPhone(person.getPhone()); user.setPassword(des3.encrypt(jwtSecret,"123456")); user.setRealName(person.getName()); user.setCreateBy(subject); user.setCreateTime(new Date()); user.setDelFlag(false); userService.insert(user); count++; } msgResult.setResult(true); msgResult.setMessage("导入数据"+count+"条。"); } catch(Exception ex){ msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="督查步进表信息写入督查事件表") @RequestMapping(value = "stepToIncident",method = RequestMethod.POST) public MessageResult stepToIncident(@RequestAttribute String subject){ MessageResult msgResult = new MessageResult(); try { Integer count = 0; List incidentSteps = incidentStepService.list(); for(IncidentStep incidentStep : incidentSteps){ Incident incident = incidentService.get(incidentStep.getIncidentId()); incident.setOrgId(incidentStep.getOrgId()); incident.setWarnDate1(incidentStep.getWarnDate1()); incident.setWarnDate2(incidentStep.getWarnDate2()); incident.setWarnDate3(incidentStep.getWarnDate3()); incident.setAllotedDate(incidentStep.getAllotedDate()); incidentService.update(incident); count++; } msgResult.setResult(true); msgResult.setMessage("导入数据"+count+"条。"); } catch(Exception ex){ msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="导入区域") @PostMapping("importArea") public MessageResult importArea(MultipartFile uploadFile, @RequestAttribute String subject){ MessageResult msgResult = new MessageResult<>(); try { POIUtils poiUtils = new POIUtils(uploadFile.getInputStream()); Sheet sheet1 = poiUtils.getSheetAt(0); int affectCount = 0; int failCount = 0; int validateColIndex = 3; for(int rowIndex=1; rowIndex<=sheet1.getLastRowNum(); rowIndex++){ try { String strLv1 = poiUtils.getCellValue(0,rowIndex,0).toString(); String strLv2 = poiUtils.getCellValue(0,rowIndex,1).toString(); String strLv3 = poiUtils.getCellValue(0,rowIndex,2).toString(); Area area1 = areaService.getByName(strLv1); if(area1 == null){ area1 = new Area(); area1.setId(UUID.randomUUID().toString()); area1.setDelFlag(false); area1.setCreateBy(subject); area1.setCreateTime(new Date()); area1.setParentId(null); area1.setName(strLv1); areaService.insert(area1); affectCount++; } Area area2 = areaService.getByName(strLv2); if(area2 == null){ area2 = new Area(); area2.setId(UUID.randomUUID().toString()); area2.setDelFlag(false); area2.setCreateBy(subject); area2.setCreateTime(new Date()); area2.setParentId(area1.getId()); area2.setName(strLv2); areaService.insert(area2); affectCount++; } Area area3 = areaService.getByName(strLv3); if(area3 == null){ area3 = new Area(); area3.setId(UUID.randomUUID().toString()); area3.setDelFlag(false); area3.setCreateBy(subject); area3.setCreateTime(new Date()); area3.setParentId(area2.getId()); area3.setName(strLv3); areaService.insert(area3); affectCount++; } } catch(Exception innerEx){ failCount++; } } if (failCount>0){ //有导入失败的记录 msgResult.setResult(false); msgResult.setMessage("数据成功导入" + affectCount + "条,有" + failCount + "条数据未导入成功,错误原因请查看报表。"); //todo 只保留错误数据的sheet Workbook wb = poiUtils.exportErrorXls(0,validateColIndex,1 + affectCount + failCount); //todo 将wb保存到oss ByteArrayOutputStream output = new ByteArrayOutputStream(); wb.write(output); byte[] buffer = output.toByteArray(); ByteArrayInputStream input = new ByteArrayInputStream(buffer); //格式化holidayInfo SimpleDateFormat sim = new SimpleDateFormat("yyyyMMddHHmmss"); String fileName = "error" + sim.format(new Date()) + ".xls"; String downloadUrl = OSSUtil.upload(ossConfig,"excellent",fileName,input); //todo 返回导入失败报表下载链接 msgResult.setData(downloadUrl); } else{ msgResult.setResult(true); msgResult.setMessage("数据成功导入" + affectCount + "条"); } } catch(Exception ex){ logger.error(ex.getMessage(),ex); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="更新站点区域") @RequestMapping(value = "updateStation",method = RequestMethod.POST) public MessageResult updateStation(@RequestAttribute String subject){ MessageResult msgResult = new MessageResult(); try { Integer count = 0; List workStations = workStationService.list(); for(WorkStation workStation : workStations){ if(workStation.getStationName().indexOf("镇") > 0) { String stationName = workStation.getStationName().substring(0, workStation.getStationName().indexOf("镇")+1); Area area = areaService.getByName(stationName); if(area != null) { workStation.setAreaId(area.getId()); workStationService.update(workStation); } else{ area = areaService.getByName(workStation.getAreaId()); if(area != null) { workStation.setAreaId(area.getId()); workStationService.update(workStation); } } } else if(workStation.getStationName().indexOf("乡") > 0) { String stationName = workStation.getStationName().substring(0, workStation.getStationName().indexOf("乡")+1); Area area = areaService.getByName(stationName); if(area != null) { workStation.setAreaId(area.getId()); workStationService.update(workStation); } else{ area = areaService.getByName(workStation.getAreaId()); if(area != null) { workStation.setAreaId(area.getId()); workStationService.update(workStation); } } } else{ Area area = areaService.getByName(workStation.getAreaId()); if(area != null) { workStation.setAreaId(area.getId()); workStationService.update(workStation); } } count++; } msgResult.setResult(true); msgResult.setMessage("更新数据"+count+"条。"); } catch(Exception ex){ msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="发送邮件") @RequestMapping(value = "sendEmail",method = RequestMethod.POST) public MessageResult sendEmail(String randomCode){ MessageResult msgResult = new MessageResult(); try { // 1. 创建一封邮件 Properties props = new Properties(); // 用于连接邮件服务器的参数配置(发送邮件时才需要用到) Session session= Session.getInstance(props); // 根据参数配置,创建会话对象(为了发送邮件准备的) MimeMessage message = new MimeMessage(session); // 创建邮件对象 /* * 也可以根据已有的eml邮件文件创建 MimeMessage 对象 * MimeMessage message = new MimeMessage(session, new FileInputStream("myEmail.eml")); */ message.setFrom(new InternetAddress("email@send.com", "USER_AA", "UTF-8"));//From: 发件人(其中 InternetAddress 的三个参数分别为: 邮箱, 显示的昵称(只用于显示, 没有特别的要求), 昵称的字符集编码) message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("cc@receive.com", "USER_CC", "UTF-8"));//To: 收件人 message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress("dd@receive.com", "USER_DD", "UTF-8"));//To: 增加收件人(可选) message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("ee@receive.com", "USER_EE", "UTF-8"));//Cc: 抄送(可选) message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress("ff@receive.com", "USER_FF", "UTF-8"));//Bcc: 密送(可选) message.setSubject("邮件主题", "UTF-8");//Subject: 邮件主题 message.setContent("这是邮件正文", "text/html;charset=UTF-8");//Content: 邮件正文(可以使用html标签) message.setSentDate(new Date());//设置显示的发件时间 message.saveChanges(); // 8. 将该邮件保存到本地 OutputStream out = new FileOutputStream("myEmail.eml"); message.writeTo(out); out.flush(); out.close(); msgResult.setResult(true); } catch(Exception ex){ msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="更新序号") @RequestMapping(value = "updataSortNum",method = RequestMethod.POST) @ApiImplicitParams({ @ApiImplicitParam(name="year", value="年份", required=true, paramType="query"), }) public MessageResult updataSortNum(String year){ MessageResult msgResult = new MessageResult(); try { Map searchParams = new HashMap<>(); if (StringUtils.isNotEmpty(year)) { searchParams.put("year", year + "%"); } List sortList = new ArrayList<>(); sortList.add(new Sort("sort_","asc")); Page page = feedbackOpinionService.pageSearch(searchParams,1,1000000,false,sortList); for(int i=0; i