PushController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.lemon.lifecenter.scheduler.controller;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.transaction.annotation.Propagation;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import com.lemon.lifecenter.scheduler.common.PushUtils;
  19. import com.lemon.lifecenter.scheduler.dto.DeviceInfoDTO;
  20. import com.lemon.lifecenter.scheduler.dto.PushResultDTO;
  21. import com.lemon.lifecenter.scheduler.dto.ScheduleDTO;
  22. import com.lemon.lifecenter.scheduler.service.PushService;
  23. @RestController
  24. public class PushController {
  25. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  26. @Autowired
  27. private PushService service;
  28. @Autowired
  29. private PushUtils pushUtils;
  30. @RequestMapping(value = "/selectSendPushList", method = RequestMethod.POST)
  31. @Transactional(propagation=Propagation.REQUIRED)
  32. public void selectSendPushList() {
  33. int count = service.selectSendPushCount();
  34. if (count == 0) {
  35. } else {
  36. ArrayList<ScheduleDTO> list = (ArrayList<ScheduleDTO>) service.selectSendPushList();
  37. for (int i = 0; i < list.size(); i++) {
  38. int pushIdx = list.get(i).getPushIdx();
  39. String sendType = list.get(i).getSendType();
  40. String sendState = list.get(i).getSendState();
  41. String targetType = list.get(i).getTargetType();
  42. String centerCode = list.get(i).getCenterCode();
  43. String pushTitle = list.get(i).getPushTitle();
  44. String pushContent = list.get(i).getPushContent();
  45. String sendDate = list.get(i).getSendDate();
  46. String sendTime = list.get(i).getSendTime();
  47. String startDate = "";
  48. // sendType : D(즉시), R(발송중), E(매일 발송)
  49. // sendState : W(대기), I(발송중), C(완료)
  50. //A(전체), N(건강정보 미엽락자), M(본인관리환자), P(환자개별선택)
  51. if (sendType.equals("D") && sendState.equals("W") && targetType.equals("P")) {
  52. startDate = sendDate+ " " + sendTime;
  53. list.get(i).setStartDate(startDate);
  54. int idx = service.insertPushLog(list.get(i));
  55. if (idx == 0) {
  56. } else {
  57. service.updatePushSchedule(list.get(i));
  58. List<DeviceInfoDTO> pushTargetList = service.selectTargetPatient(pushIdx);
  59. for (int j = 0; j <pushTargetList.size(); j++) {
  60. String token = pushTargetList.get(j).getDeviceToken();
  61. int patientIdx = pushTargetList.get(j).getPatientIdx();
  62. PushResultDTO pushResultDto = new PushResultDTO();
  63. pushResultDto.setYm(pushUtils.getNowYm());
  64. pushResultDto.setPushIdx(pushIdx);
  65. pushResultDto.setPatientIdx(patientIdx);
  66. pushResultDto.setDeviceToken(token);
  67. pushResultDto.setState("W"); // 결과 대기
  68. service.insertPushResult(pushResultDto);
  69. HashMap<String, String> response = new HashMap<String, String>();
  70. if (!token.equals("")) {
  71. response = pushUtils.sendFcm(token, pushTitle, pushContent);
  72. } else {
  73. response.put("NOT_FOUND", "Requested entity was not found.");
  74. }
  75. for (String key : response.keySet()) {
  76. if (key.equals("success")) {
  77. pushResultDto.setSuccessYn("Y");
  78. } else {
  79. pushResultDto.setSuccessYn("N");
  80. pushResultDto.setFailCode(key);
  81. pushResultDto.setNote(response.get(key));
  82. }
  83. }
  84. pushResultDto.setState("C"); // 발송 완료
  85. service.updatePushResult(pushResultDto);
  86. }
  87. service.updatePushLog(list.get(i));
  88. }
  89. }
  90. }
  91. }
  92. }
  93. @RequestMapping(value = "/createResultTable", method = RequestMethod.POST)
  94. public void createResultTable() {
  95. String ym = pushUtils.getNowYm();
  96. int tableCount = 0;
  97. tableCount = service.resultTableCount(ym);
  98. if (tableCount == 0) {
  99. service.createResultTable(ym);
  100. }
  101. Calendar cal = Calendar.getInstance();
  102. cal.add (cal.MONTH, + 1); //다음달
  103. ym = pushUtils.getNextYm();
  104. tableCount = service.resultTableCount(ym);
  105. if (tableCount == 0) {
  106. service.createResultTable(ym);
  107. }
  108. }
  109. }