Selaa lähdekoodia

Push기능 추가

dwkim 2 vuotta sitten
vanhempi
commit
a89efcc713

+ 15 - 1
pom.xml

@@ -199,7 +199,21 @@
199 199
 			<version>3.8.0</version>
200 200
 		</dependency>
201 201
 
202
-  	</dependencies>
202
+		<dependency>
203
+			<groupId>com.google.firebase</groupId>
204
+			<artifactId>firebase-admin</artifactId>
205
+			<version>8.1.0</version>
206
+			<scope>runtime</scope>
207
+		</dependency>
208
+		<dependency>
209
+			<groupId>org.json</groupId>
210
+			<artifactId>json</artifactId>
211
+			<version>20180813</version>
212
+		</dependency>
213
+
214
+
215
+
216
+	</dependencies>
203 217
 	<profiles>
204 218
 		<profile>
205 219
 			<id>local</id>

+ 9 - 0
src/main/java/com/oqpo/api/constant/ApiConstants.java

@@ -20,4 +20,13 @@ public interface ApiConstants {
20 20
     String STR_OPTION_DELIMETER = "!";
21 21
     String STR_OPTION_END = "@";
22 22
     int STR_TICKET_START_LEN = STR_TICKET_START.length();
23
+
24
+    Boolean PUSH_CONTENT_AVAILABLE = true;
25
+    String PUSH_BAGE = "1";
26
+    String PUSH_PRIORITY = "high";
27
+    String PUSH_APNS_PUSH_TYPE = "alert";
28
+    String PUSH_APNS_PRIORITY = "10";
29
+    String PUSH_ACTION = ".view.MainActivity";
30
+    Integer PUSH_RETRY  = 3;
31
+    Integer URLCONN_TIME_OUT = 3000;
23 32
 }

+ 19 - 0
src/main/java/com/oqpo/api/entity/PushData.java

@@ -0,0 +1,19 @@
1
+package com.oqpo.api.entity;
2
+
3
+import com.oqpo.api.entity.oper.MemberEntity;
4
+import lombok.Getter;
5
+import lombok.Setter;
6
+import lombok.ToString;
7
+
8
+import java.io.Serializable;
9
+import java.util.List;
10
+
11
+@Getter
12
+@Setter
13
+@ToString
14
+public class PushData implements Serializable {
15
+    private static final long serialVersionUID = -3590936033715917450L;
16
+
17
+    private String bodyData;
18
+    private List<MemberEntity> targetList;
19
+}

+ 20 - 0
src/main/java/com/oqpo/api/entity/PushEntity.java

@@ -0,0 +1,20 @@
1
+package com.oqpo.api.entity;
2
+
3
+import lombok.Getter;
4
+import lombok.Setter;
5
+import lombok.ToString;
6
+
7
+import java.io.Serializable;
8
+import java.util.List;
9
+
10
+@Getter
11
+@Setter
12
+@ToString
13
+public class PushEntity implements Serializable {
14
+    private static final long serialVersionUID = -3590936033715917450L;
15
+
16
+    private String jobType;
17
+    private String relUnqNo;
18
+    private String appReg;
19
+    private String userNm;
20
+}

+ 35 - 0
src/main/java/com/oqpo/api/enums/PushMessage.java

@@ -0,0 +1,35 @@
1
+package com.oqpo.api.enums;
2
+
3
+import lombok.Getter;
4
+import lombok.Setter;
5
+
6
+public enum PushMessage {
7
+
8
+    PUSH_REQ("PO", "매장 발주 요청건이 들어왔습니다."), //
9
+    PUSH_RTN("RTN", "매장 반품 요청건이 들어왔습니다."), //
10
+    ;
11
+
12
+    PushMessage(String cd, String nm) {
13
+        this.cd = cd;
14
+        this.name = nm;
15
+    }
16
+
17
+    @Getter
18
+    @Setter
19
+    private String cd;
20
+
21
+    @Getter
22
+    @Setter
23
+    private String name;
24
+
25
+    public static String getName(String ccd) {
26
+        PushMessage[] values = PushMessage.values();
27
+        for (PushMessage icd : values) {
28
+            if (icd.cd.equals(ccd)) {
29
+                return icd.name;
30
+            }
31
+        }
32
+        return ccd;
33
+    }
34
+
35
+}

+ 4 - 0
src/main/java/com/oqpo/api/mapper/oper/UserMngMapper.java

@@ -1,12 +1,14 @@
1 1
 package com.oqpo.api.mapper.oper;
2 2
 
3 3
 
4
+import com.oqpo.api.entity.oper.MemberEntity;
4 5
 import com.oqpo.api.entity.oper.UserAuthInfoEntity;
5 6
 import com.oqpo.api.entity.oper.UserMngEntity;
6 7
 import com.oqpo.api.web.dto.request.GridRequest;
7 8
 import org.apache.ibatis.annotations.Mapper;
8 9
 import org.apache.ibatis.annotations.Param;
9 10
 
11
+import java.lang.reflect.Member;
10 12
 import java.util.List;
11 13
 
12 14
 @Mapper
@@ -67,4 +69,6 @@ public interface UserMngMapper {
67 69
     int deleteUserAuthWishList(@Param("brandId") String brandId) throws Exception;
68 70
 
69 71
     int selectWishListCnt(@Param("brandId") String brandId) throws Exception;
72
+
73
+    List<MemberEntity> selectPushTargetList(@Param("brandId") String brandId) ;
70 74
 }

+ 166 - 0
src/main/java/com/oqpo/api/service/PushService.java

@@ -0,0 +1,166 @@
1
+package com.oqpo.api.service;
2
+
3
+import com.oqpo.api.constant.ApiConstants;
4
+import com.oqpo.api.entity.PushData;
5
+import com.oqpo.api.entity.oper.MemberEntity;
6
+import com.oqpo.api.enums.PushMessage;
7
+import com.oqpo.api.mapper.oper.UserMngMapper;
8
+import com.oqpo.api.web.interceptor.HeaderRequestInterceptor;
9
+import lombok.extern.slf4j.Slf4j;
10
+import org.codehaus.jackson.map.ObjectMapper;
11
+import org.springframework.beans.factory.annotation.Autowired;
12
+import org.springframework.beans.factory.annotation.Value;
13
+import org.json.JSONArray;
14
+import org.json.JSONException;
15
+import org.json.JSONObject;
16
+import org.springframework.http.HttpEntity;
17
+import org.springframework.http.client.ClientHttpRequestInterceptor;
18
+import org.springframework.stereotype.Service;
19
+
20
+import java.io.BufferedReader;
21
+import java.io.InputStreamReader;
22
+import java.io.OutputStream;
23
+import java.net.HttpURLConnection;
24
+import java.net.URL;
25
+import java.util.ArrayList;
26
+import java.util.HashMap;
27
+import java.util.List;
28
+import java.util.Map;
29
+
30
+@Service
31
+@Slf4j
32
+public class PushService  extends CommonService {
33
+
34
+    @Autowired
35
+    private UserMngMapper userMngMapper;
36
+
37
+
38
+    @Value("${push.firebase_server_key}")
39
+    private String firebase_server_key;
40
+
41
+    @Value("${push.firebase_api_url}")
42
+    private String firebase_api_url;
43
+
44
+    // jobType po 주문, rtn 반품  , reuUnqNo 관련번호
45
+    public PushData makePushData(String brandId, String jobType, String relUnqNo) throws JSONException, InterruptedException {
46
+
47
+        PushData  pushData = new PushData();
48
+
49
+        List<MemberEntity> targetList = userMngMapper.selectPushTargetList(brandId);
50
+
51
+        if (targetList.size() > 0) {
52
+
53
+            JSONObject bodySA = new JSONObject();
54
+            //List<JSONArray> arrayListSA = new ArrayList<JSONArray>();
55
+            JSONArray arraySA = new JSONArray();
56
+
57
+            for (int i = 0 ; i < targetList.size() ;i++) {
58
+                MemberEntity tempEntity = targetList.get(i);
59
+                arraySA.put(tempEntity.getAppReg());
60
+            }
61
+
62
+            bodySA.put("registration_ids", arraySA);
63
+            bodySA.put("content_available", ApiConstants.PUSH_CONTENT_AVAILABLE);
64
+            bodySA.put("priority", ApiConstants.PUSH_PRIORITY);
65
+            bodySA.put("apns-push-type", ApiConstants.PUSH_APNS_PUSH_TYPE);
66
+            bodySA.put("apns-priority", ApiConstants.PUSH_APNS_PRIORITY);
67
+
68
+            JSONObject dataSA = new JSONObject();
69
+
70
+            try {
71
+                if ("PO".equals(jobType)) {
72
+                    dataSA.put("title", "매장주문");
73
+                } else {
74
+                    dataSA.put("title", "매장반품");
75
+                }
76
+                if ("PO".equals(jobType)) {
77
+                    dataSA.put("body", PushMessage.PUSH_REQ.getName());
78
+                } else {
79
+                    dataSA.put("body", PushMessage.PUSH_RTN.getName());
80
+                }
81
+
82
+                dataSA.put("click_action", ApiConstants.PUSH_ACTION);
83
+                //dataSA.put("data", entity.getPushLandingCd());
84
+                //dataSA.put("relNo", entity.getLinkUrl());
85
+                bodySA.put("data", dataSA);
86
+            } catch (Exception e) {
87
+            }
88
+
89
+            pushData.setBodyData(bodySA.toString());
90
+            //pushData.setPushNo(entity.getPushNo());
91
+            //pushData.setPushSize(targetList.size());
92
+
93
+        }
94
+
95
+        return pushData;
96
+    }
97
+
98
+
99
+    public ArrayList<ClientHttpRequestInterceptor> pushSet(HttpEntity<String> entity) {
100
+
101
+        ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
102
+
103
+        log.info("firebase_server_key ============>" + firebase_server_key);
104
+        interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + firebase_server_key));
105
+        interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json;charset=UTF-8"));
106
+
107
+        return interceptors;
108
+    }
109
+
110
+
111
+    public void pushSend(String brandId, String jobType, String relUnqNo) {
112
+        log.info("=========================push run");
113
+        try {
114
+            PushData  pushData = makePushData(brandId, jobType, relUnqNo);
115
+            String bodyData = pushData.getBodyData();
116
+            for (int i = 0; i < ApiConstants.PUSH_RETRY; i++) {
117
+                URL url = new URL(firebase_api_url);
118
+                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
119
+                conn.setDoOutput(true);
120
+                conn.setRequestMethod("POST");
121
+                conn.setRequestProperty("Content-Type", "application/json");
122
+                conn.setRequestProperty("Authorization", "key=" + firebase_server_key);
123
+                conn.setConnectTimeout(ApiConstants.URLCONN_TIME_OUT);
124
+                conn.setReadTimeout(ApiConstants.URLCONN_TIME_OUT);
125
+                conn.setDoOutput(true);
126
+
127
+                OutputStream os = conn.getOutputStream();
128
+                os.write(bodyData.getBytes("UTF-8"));
129
+                os.flush();
130
+                os.close();
131
+                int responseCode = conn.getResponseCode();
132
+                if (responseCode == HttpURLConnection.HTTP_OK) {
133
+                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
134
+                    String inputLine;
135
+                    StringBuffer response = new StringBuffer();
136
+
137
+                    while ((inputLine = in.readLine()) != null) {
138
+                        response.append(inputLine);
139
+                    }
140
+                    in.close();
141
+                    // 결과 처리
142
+                    Map<String,Object> responsMap = new ObjectMapper().readValue(response.toString(), HashMap.class);
143
+
144
+                    int retSuccess = (int) responsMap.get("success");
145
+                    log.info("========================connect retSuccess" + Integer.toString(retSuccess));
146
+                    int retError = (int) responsMap.get("failure");
147
+                    log.info("========================connect erroor" + Integer.toString(retError));
148
+                    break;
149
+                } else {
150
+                    Thread.sleep(1000);
151
+                    log.info("========================connect erroor" + Integer.toString(i));
152
+                }
153
+
154
+            }
155
+
156
+        } catch(Exception e) {
157
+            e.printStackTrace();
158
+        }
159
+
160
+
161
+    }
162
+
163
+
164
+
165
+
166
+}

+ 10 - 0
src/main/java/com/oqpo/api/service/pomng/PchReqService.java

@@ -19,6 +19,7 @@ import com.oqpo.api.mapper.pomng.PchReqMapper;
19 19
 import com.oqpo.api.mapper.stinfo.BrandMapper;
20 20
 import com.oqpo.api.mapper.stockmng.WhsMngMapper;
21 21
 import com.oqpo.api.service.CommonService;
22
+import com.oqpo.api.service.PushService;
22 23
 import com.oqpo.api.util.DateUtil;
23 24
 import com.oqpo.api.web.dto.request.GridRequest;
24 25
 import com.oqpo.api.web.dto.request.pomng.pchReq.PchReqInitInfoRequest;
@@ -54,6 +55,9 @@ public class PchReqService extends CommonService {
54 55
     @Autowired
55 56
     private WhsMngMapper whsMngMapper;
56 57
 
58
+    @Autowired
59
+    private PushService pushService;
60
+
57 61
     /* 구매요청  초기 정보 */
58 62
     public PchReqInitInfoResponse selectPchReqInitInfo(String userId, PchReqInitInfoRequest pchReqInitInfoRequest) throws Exception {
59 63
         System.out.println("====================");
@@ -354,6 +358,12 @@ public class PchReqService extends CommonService {
354 358
                 }
355 359
             }
356 360
 
361
+            // push서비스 호출
362
+            log.info("====================================>" + savePchReqlRequest.getPchReqStCd());
363
+            if ("PR20".equals(savePchReqlRequest.getPchReqStCd())) {
364
+                log.info("=======================================pushSend");
365
+                pushService.pushSend(savePchReqlRequest.getBrandId(),"PO", savePchReqlRequest.getPchReqUnqNo() );
366
+            }
357 367
 
358 368
 
359 369
         } catch (GlobalException e) {

+ 11 - 0
src/main/java/com/oqpo/api/service/rtnmng/RtnReqService.java

@@ -19,6 +19,7 @@ import com.oqpo.api.mapper.rtnmng.RtnReqMapper;
19 19
 import com.oqpo.api.mapper.stinfo.BrandMapper;
20 20
 import com.oqpo.api.mapper.stockmng.WhsMngMapper;
21 21
 import com.oqpo.api.service.CommonService;
22
+import com.oqpo.api.service.PushService;
22 23
 import com.oqpo.api.util.DateUtil;
23 24
 import com.oqpo.api.web.dto.request.GridRequest;
24 25
 import com.oqpo.api.web.dto.request.pomng.pchReq.PchReqInitInfoRequest;
@@ -51,6 +52,9 @@ public class RtnReqService extends CommonService {
51 52
     @Autowired
52 53
     private WhsMngMapper whsMngMapper;
53 54
 
55
+    @Autowired
56
+    private PushService pushService;
57
+
54 58
     /* 반품요청  초기 정보 */
55 59
 
56 60
     public RtnReqInitInfoResponse selectRtnReqInitInfo(String userId, PchReqInitInfoRequest pchReqInitInfoRequest) throws Exception {
@@ -200,6 +204,13 @@ public class RtnReqService extends CommonService {
200 204
                 rtnReqMapper.updateAllRtnReqDtlStCd(userId, entity.getRtnReqUnqNo(), "RRD00");
201 205
             }
202 206
 
207
+            // push서비스 호출
208
+            log.info("====================================>" + saveRtnReqlRequest.getRtnReqStCd());
209
+            if ("RR20".equals(saveRtnReqlRequest.getRtnReqStCd())) {
210
+                log.info("=======================================pushSend");
211
+                pushService.pushSend(saveRtnReqlRequest.getBrandId(),"RTN", entity.getRtnReqUnqNo());
212
+            }
213
+
203 214
         } catch (GlobalException e) {
204 215
             e.printStackTrace();
205 216
             throw new GlobalException(e.getSystemMessageCode());

+ 4 - 0
src/main/resources-env/local/application.yml

@@ -90,3 +90,7 @@ kicc:
90 90
   cert_dir: C:/kicc/easypay/cert
91 91
   log_dir: C:/kicc/easypay/logs
92 92
   test_mall_tid: T0010761
93
+
94
+push:
95
+  firebase_server_key: AAAAIZ_eZOc:APA91bH38GR1FbgsRD-5tBLF__QehSHqd9kvGOr0lghneRyQFMdqme7uXv1-_U_3jXG4TgaVBc6sV6mSX3JT5my7-uhKrNPnkDBb_7Qms5dnymyWQp9t7z_qxmlzzYMG9VabiDHGGTWj
96
+  firebase_api_url: https://fcm.googleapis.com/fcm/send

+ 9 - 0
src/main/resources/mybatis/sqlmaps/oper/UserMng.xml

@@ -393,4 +393,13 @@
393 393
         and   auth_no = 'SHOP_WISH'
394 394
     </insert>
395 395
 
396
+    <select id="selectPushTargetList" resultType="com.oqpo.api.entity.oper.MemberEntity">
397
+        select  user_nm, app_reg
398
+        from np_user_info
399
+        where brand_id = #{brandId}
400
+        and   auth_tp_cd = '40'
401
+        and   app_reg is not null
402
+        and   app_reg <![CDATA[ <> ]]> ''
403
+    </select>
404
+
396 405
 </mapper>