123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package com.lemon.lifecenter.scheduler.controller;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import com.lemon.lifecenter.scheduler.common.PushUtils;
- import com.lemon.lifecenter.scheduler.dto.DeviceInfoDTO;
- import com.lemon.lifecenter.scheduler.dto.PushResultDTO;
- import com.lemon.lifecenter.scheduler.dto.ScheduleDTO;
- import com.lemon.lifecenter.scheduler.service.PushService;
- @RestController
- public class PushController {
-
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
-
- @Autowired
- private PushService service;
-
- @Autowired
- private PushUtils pushUtils;
-
- @RequestMapping(value = "/selectSendPushList", method = RequestMethod.POST)
- @Transactional(propagation=Propagation.REQUIRED)
- public void selectSendPushList() {
- int count = service.selectSendPushCount();
- if (count == 0) {
- } else {
- ArrayList<ScheduleDTO> list = (ArrayList<ScheduleDTO>) service.selectSendPushList();
- for (int i = 0; i < list.size(); i++) {
- int pushIdx = list.get(i).getPushIdx();
- String sendType = list.get(i).getSendType();
- String sendState = list.get(i).getSendState();
- String targetType = list.get(i).getTargetType();
- String centerCode = list.get(i).getCenterCode();
- String pushTitle = list.get(i).getPushTitle();
- String pushContent = list.get(i).getPushContent();
- String sendDate = list.get(i).getSendDate();
- String sendTime = list.get(i).getSendTime();
- String startDate = "";
-
- // sendType : D(즉시), R(발송중), E(매일 발송)
- // sendState : W(대기), I(발송중), C(완료)
- //A(전체), N(건강정보 미엽락자), M(본인관리환자), P(환자개별선택)
- if (sendType.equals("D") && sendState.equals("W") && targetType.equals("P")) {
- startDate = sendDate+ " " + sendTime;
- list.get(i).setStartDate(startDate);
- int idx = service.insertPushLog(list.get(i));
- if (idx == 0) {
- } else {
- service.updatePushSchedule(list.get(i));
- List<DeviceInfoDTO> pushTargetList = service.selectTargetPatient(pushIdx);
- for (int j = 0; j <pushTargetList.size(); j++) {
- String token = pushTargetList.get(j).getDeviceToken();
- int patientIdx = pushTargetList.get(j).getPatientIdx();
-
- PushResultDTO pushResultDto = new PushResultDTO();
- pushResultDto.setYm(pushUtils.getNowYm());
- pushResultDto.setPushIdx(pushIdx);
- pushResultDto.setPatientIdx(patientIdx);
- pushResultDto.setDeviceToken(token);
- pushResultDto.setState("W"); // 결과 대기
- service.insertPushResult(pushResultDto);
-
- HashMap<String, String> response = new HashMap<String, String>();
- if (!token.equals("")) {
- response = pushUtils.sendFcm(token, pushTitle, pushContent);
- } else {
- response.put("NOT_FOUND", "Requested entity was not found.");
- }
-
- for (String key : response.keySet()) {
- if (key.equals("success")) {
- pushResultDto.setSuccessYn("Y");
- } else {
- pushResultDto.setSuccessYn("N");
- pushResultDto.setFailCode(key);
- pushResultDto.setNote(response.get(key));
- }
- }
-
- pushResultDto.setState("C"); // 발송 완료
- service.updatePushResult(pushResultDto);
- }
-
- service.updatePushLog(list.get(i));
- }
- }
- }
-
- }
- }
-
- @RequestMapping(value = "/createResultTable", method = RequestMethod.POST)
- public void createResultTable() {
- String ym = pushUtils.getNowYm();
-
- int tableCount = 0;
- tableCount = service.resultTableCount(ym);
-
- if (tableCount == 0) {
- service.createResultTable(ym);
- }
- Calendar cal = Calendar.getInstance();
- cal.add (cal.MONTH, + 1); //다음달
-
- ym = pushUtils.getNextYm();
-
- tableCount = service.resultTableCount(ym);
-
- if (tableCount == 0) {
- service.createResultTable(ym);
- }
- }
- }
|