textController.java 16 KB

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