textController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package com.jpsoft.excellent.modules;
  2. import com.jpsoft.excellent.config.OSSConfig;
  3. import com.jpsoft.excellent.modules.base.entity.*;
  4. import com.jpsoft.excellent.modules.base.service.*;
  5. import com.jpsoft.excellent.modules.common.dto.MessageResult;
  6. import com.jpsoft.excellent.modules.common.utils.DES3;
  7. import com.jpsoft.excellent.modules.common.utils.OSSUtil;
  8. import com.jpsoft.excellent.modules.common.utils.POIUtils;
  9. import com.jpsoft.excellent.modules.common.utils.SMSUtil;
  10. import com.jpsoft.excellent.modules.sys.entity.DataDictionary;
  11. import com.jpsoft.excellent.modules.sys.entity.User;
  12. import com.jpsoft.excellent.modules.sys.service.UserService;
  13. import io.swagger.annotations.Api;
  14. import io.swagger.annotations.ApiImplicitParam;
  15. import io.swagger.annotations.ApiImplicitParams;
  16. import io.swagger.annotations.ApiOperation;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.apache.poi.ss.usermodel.Sheet;
  19. import org.apache.poi.ss.usermodel.Workbook;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.web.bind.annotation.*;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import javax.mail.Session;
  27. import javax.mail.internet.InternetAddress;
  28. import javax.mail.internet.MimeMessage;
  29. import java.io.ByteArrayInputStream;
  30. import java.io.ByteArrayOutputStream;
  31. import java.io.FileOutputStream;
  32. import java.io.OutputStream;
  33. import java.text.SimpleDateFormat;
  34. import java.util.*;
  35. @RestController
  36. @RequestMapping("/text")
  37. @Api(description = "测试")
  38. public class textController {
  39. private Logger logger = LoggerFactory.getLogger(getClass());
  40. @Value("${jwt.secret}")
  41. private String jwtSecret;
  42. @Autowired
  43. private OSSConfig ossConfig;
  44. @Autowired
  45. private UserService userService;
  46. @Autowired
  47. private PersonService personService;
  48. @Autowired
  49. private IncidentService incidentService;
  50. @Autowired
  51. private IncidentStepService incidentStepService;
  52. @Autowired
  53. private AreaService areaService;
  54. @Autowired
  55. private WorkStationService workStationService;
  56. @ApiOperation(value="短信发送")
  57. @RequestMapping(value = "sendSMS",method = RequestMethod.POST)
  58. public MessageResult sendSMS(@RequestAttribute String subject){
  59. MessageResult msgResult = new MessageResult();
  60. String MessageContent = "【双优督办】干部监督评议有一条投诉,请查收。";
  61. String UserNumber = "19972671252,15827719088";
  62. try {
  63. MessageResult message = SMSUtil.sendSMS(MessageContent, UserNumber, null);
  64. // message = SMSUtil.sendSMS(MessageContent, UserNumber, null);
  65. Map map = (Map) message.getData();
  66. if("success".equals(map.get("returnstatus"))) {
  67. msgResult.setCode(200);
  68. msgResult.setResult(true);
  69. msgResult.setMessage(map.get("code").toString());
  70. }
  71. else {
  72. msgResult.setCode(203);
  73. msgResult.setResult(true);
  74. msgResult.setMessage(map.get("code").toString());
  75. msgResult.setData(message.getData());
  76. }
  77. }
  78. catch(Exception ex){
  79. msgResult.setCode(400);
  80. msgResult.setResult(false);
  81. msgResult.setMessage(ex.getMessage());
  82. }
  83. return msgResult;
  84. }
  85. @ApiOperation(value="人员转用户")
  86. @RequestMapping(value = "personToUser",method = RequestMethod.POST)
  87. public MessageResult personToUser(@RequestAttribute String subject){
  88. MessageResult msgResult = new MessageResult();
  89. try {
  90. Integer count = 0;
  91. List<Person> persons = personService.list();
  92. for(Person person : persons){
  93. DES3 des3 = new DES3();
  94. User user = new User();
  95. user.setId(UUID.randomUUID().toString());
  96. user.setOrgId(person.getOrgId());
  97. user.setUserName(person.getPhone());
  98. user.setPhone(person.getPhone());
  99. user.setPassword(des3.encrypt(jwtSecret,"123456"));
  100. user.setRealName(person.getName());
  101. user.setCreateBy(subject);
  102. user.setCreateTime(new Date());
  103. user.setDelFlag(false);
  104. userService.insert(user);
  105. count++;
  106. }
  107. msgResult.setResult(true);
  108. msgResult.setMessage("导入数据"+count+"条。");
  109. }
  110. catch(Exception ex){
  111. msgResult.setResult(false);
  112. msgResult.setMessage(ex.getMessage());
  113. }
  114. return msgResult;
  115. }
  116. @ApiOperation(value="督查步进表信息写入督查事件表")
  117. @RequestMapping(value = "stepToIncident",method = RequestMethod.POST)
  118. public MessageResult stepToIncident(@RequestAttribute String subject){
  119. MessageResult msgResult = new MessageResult();
  120. try {
  121. Integer count = 0;
  122. List<IncidentStep> incidentSteps = incidentStepService.list();
  123. for(IncidentStep incidentStep : incidentSteps){
  124. Incident incident = incidentService.get(incidentStep.getIncidentId());
  125. incident.setOrgId(incidentStep.getOrgId());
  126. incident.setWarnDate1(incidentStep.getWarnDate1());
  127. incident.setWarnDate2(incidentStep.getWarnDate2());
  128. incident.setWarnDate3(incidentStep.getWarnDate3());
  129. incident.setAllotedDate(incidentStep.getAllotedDate());
  130. incidentService.update(incident);
  131. count++;
  132. }
  133. msgResult.setResult(true);
  134. msgResult.setMessage("导入数据"+count+"条。");
  135. }
  136. catch(Exception ex){
  137. msgResult.setResult(false);
  138. msgResult.setMessage(ex.getMessage());
  139. }
  140. return msgResult;
  141. }
  142. @ApiOperation(value="导入区域")
  143. @PostMapping("importArea")
  144. public MessageResult<String> importArea(MultipartFile uploadFile, @RequestAttribute String subject){
  145. MessageResult<String> msgResult = new MessageResult<>();
  146. try {
  147. POIUtils poiUtils = new POIUtils(uploadFile.getInputStream());
  148. Sheet sheet1 = poiUtils.getSheetAt(0);
  149. int affectCount = 0;
  150. int failCount = 0;
  151. int validateColIndex = 3;
  152. for(int rowIndex=1; rowIndex<=sheet1.getLastRowNum(); rowIndex++){
  153. try {
  154. String strLv1 = poiUtils.getCellValue(0,rowIndex,0).toString();
  155. String strLv2 = poiUtils.getCellValue(0,rowIndex,1).toString();
  156. String strLv3 = poiUtils.getCellValue(0,rowIndex,2).toString();
  157. Area area1 = areaService.getByName(strLv1);
  158. if(area1 == null){
  159. area1 = new Area();
  160. area1.setId(UUID.randomUUID().toString());
  161. area1.setDelFlag(false);
  162. area1.setCreateBy(subject);
  163. area1.setCreateTime(new Date());
  164. area1.setParentId(null);
  165. area1.setName(strLv1);
  166. areaService.insert(area1);
  167. affectCount++;
  168. }
  169. Area area2 = areaService.getByName(strLv2);
  170. if(area2 == null){
  171. area2 = new Area();
  172. area2.setId(UUID.randomUUID().toString());
  173. area2.setDelFlag(false);
  174. area2.setCreateBy(subject);
  175. area2.setCreateTime(new Date());
  176. area2.setParentId(area1.getId());
  177. area2.setName(strLv2);
  178. areaService.insert(area2);
  179. affectCount++;
  180. }
  181. Area area3 = areaService.getByName(strLv3);
  182. if(area3 == null){
  183. area3 = new Area();
  184. area3.setId(UUID.randomUUID().toString());
  185. area3.setDelFlag(false);
  186. area3.setCreateBy(subject);
  187. area3.setCreateTime(new Date());
  188. area3.setParentId(area2.getId());
  189. area3.setName(strLv3);
  190. areaService.insert(area3);
  191. affectCount++;
  192. }
  193. }
  194. catch(Exception innerEx){
  195. failCount++;
  196. }
  197. }
  198. if (failCount>0){
  199. //有导入失败的记录
  200. msgResult.setResult(false);
  201. msgResult.setMessage("数据成功导入" + affectCount + "条,有" + failCount + "条数据未导入成功,错误原因请查看报表。");
  202. //todo 只保留错误数据的sheet
  203. Workbook wb = poiUtils.exportErrorXls(0,validateColIndex,1 + affectCount + failCount);
  204. //todo 将wb保存到oss
  205. ByteArrayOutputStream output = new ByteArrayOutputStream();
  206. wb.write(output);
  207. byte[] buffer = output.toByteArray();
  208. ByteArrayInputStream input = new ByteArrayInputStream(buffer);
  209. //格式化holidayInfo
  210. SimpleDateFormat sim = new SimpleDateFormat("yyyyMMddHHmmss");
  211. String fileName = "error" + sim.format(new Date()) + ".xls";
  212. String downloadUrl = OSSUtil.upload(ossConfig,"excellent",fileName,input);
  213. //todo 返回导入失败报表下载链接
  214. msgResult.setData(downloadUrl);
  215. }
  216. else{
  217. msgResult.setResult(true);
  218. msgResult.setMessage("数据成功导入" + affectCount + "条");
  219. }
  220. }
  221. catch(Exception ex){
  222. logger.error(ex.getMessage(),ex);
  223. msgResult.setResult(false);
  224. msgResult.setMessage(ex.getMessage());
  225. }
  226. return msgResult;
  227. }
  228. @ApiOperation(value="更新站点区域")
  229. @RequestMapping(value = "updateStation",method = RequestMethod.POST)
  230. public MessageResult updateStation(@RequestAttribute String subject){
  231. MessageResult msgResult = new MessageResult();
  232. try {
  233. Integer count = 0;
  234. List<WorkStation> workStations = workStationService.list();
  235. for(WorkStation workStation : workStations){
  236. if(workStation.getStationName().indexOf("镇") > 0) {
  237. String stationName = workStation.getStationName().substring(0, workStation.getStationName().indexOf("镇")+1);
  238. Area area = areaService.getByName(stationName);
  239. if(area != null) {
  240. workStation.setAreaId(area.getId());
  241. workStationService.update(workStation);
  242. }
  243. else{
  244. area = areaService.getByName(workStation.getAreaId());
  245. if(area != null) {
  246. workStation.setAreaId(area.getId());
  247. workStationService.update(workStation);
  248. }
  249. }
  250. }
  251. else if(workStation.getStationName().indexOf("乡") > 0) {
  252. String stationName = workStation.getStationName().substring(0, workStation.getStationName().indexOf("乡")+1);
  253. Area area = areaService.getByName(stationName);
  254. if(area != null) {
  255. workStation.setAreaId(area.getId());
  256. workStationService.update(workStation);
  257. }
  258. else{
  259. area = areaService.getByName(workStation.getAreaId());
  260. if(area != null) {
  261. workStation.setAreaId(area.getId());
  262. workStationService.update(workStation);
  263. }
  264. }
  265. }
  266. else{
  267. Area area = areaService.getByName(workStation.getAreaId());
  268. if(area != null) {
  269. workStation.setAreaId(area.getId());
  270. workStationService.update(workStation);
  271. }
  272. }
  273. count++;
  274. }
  275. msgResult.setResult(true);
  276. msgResult.setMessage("更新数据"+count+"条。");
  277. }
  278. catch(Exception ex){
  279. msgResult.setResult(false);
  280. msgResult.setMessage(ex.getMessage());
  281. }
  282. return msgResult;
  283. }
  284. @ApiOperation(value="发送邮件")
  285. @RequestMapping(value = "sendEmail",method = RequestMethod.POST)
  286. public MessageResult sendEmail(String randomCode){
  287. MessageResult msgResult = new MessageResult();
  288. try {
  289. // 1. 创建一封邮件
  290. Properties props = new Properties(); // 用于连接邮件服务器的参数配置(发送邮件时才需要用到)
  291. Session session= Session.getInstance(props); // 根据参数配置,创建会话对象(为了发送邮件准备的)
  292. MimeMessage message = new MimeMessage(session); // 创建邮件对象
  293. /*
  294. * 也可以根据已有的eml邮件文件创建 MimeMessage 对象
  295. * MimeMessage message = new MimeMessage(session, new FileInputStream("myEmail.eml"));
  296. */
  297. message.setFrom(new InternetAddress("email@send.com", "USER_AA", "UTF-8"));//From: 发件人(其中 InternetAddress 的三个参数分别为: 邮箱, 显示的昵称(只用于显示, 没有特别的要求), 昵称的字符集编码)
  298. message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("cc@receive.com", "USER_CC", "UTF-8"));//To: 收件人
  299. message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress("dd@receive.com", "USER_DD", "UTF-8"));//To: 增加收件人(可选)
  300. message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("ee@receive.com", "USER_EE", "UTF-8"));//Cc: 抄送(可选)
  301. message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress("ff@receive.com", "USER_FF", "UTF-8"));//Bcc: 密送(可选)
  302. message.setSubject("邮件主题", "UTF-8");//Subject: 邮件主题
  303. message.setContent("这是邮件正文", "text/html;charset=UTF-8");//Content: 邮件正文(可以使用html标签)
  304. message.setSentDate(new Date());//设置显示的发件时间
  305. message.saveChanges();
  306. // 8. 将该邮件保存到本地
  307. OutputStream out = new FileOutputStream("myEmail.eml");
  308. message.writeTo(out);
  309. out.flush();
  310. out.close();
  311. msgResult.setResult(true);
  312. }
  313. catch(Exception ex){
  314. msgResult.setResult(false);
  315. msgResult.setMessage(ex.getMessage());
  316. }
  317. return msgResult;
  318. }
  319. }