PointCommonBiz.java
9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.cjs.cms.biz.user.point;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cjs.cms.biz.ServiceException;
import com.cjs.cms.biz.user.WeiXinBiz;
import com.cjs.cms.dao.pub.MySqlSysDateDao;
import com.cjs.cms.dao.user.WeixinDao;
import com.cjs.cms.dao.user.account.ClientDao;
import com.cjs.cms.dao.user.account.ClientInfoDao;
import com.cjs.cms.dao.user.point.PointActiveDao;
import com.cjs.cms.dao.user.point.PointRecordDao;
import com.cjs.cms.dao.user.point.PointUserDao;
import com.cjs.cms.model.user.WxUserInfo;
import com.cjs.cms.model.user.point.PointActiveInfo;
import com.cjs.cms.model.user.point.PointRecordInfo;
import com.cjs.cms.model.user.point.PointUserInfo;
import com.cjs.cms.util.lang.DateEnum;
import com.cjs.cms.util.lang.DateUtil;
import com.cjs.cms.util.lang.StringUtil;
@Service
public class PointCommonBiz {
Logger log = LogManager.getLogger();
@Autowired
private MySqlSysDateDao mySqlSysDateDao;
@Autowired
private PointRecordDao pointRecordDao;
@Autowired
private PointActiveDao pointActiveDao;
@Autowired
private PointUserDao pointUserDao;
@Autowired
private WeixinDao weixinDao;
@Autowired
private WeiXinBiz weiXinBiz;
@Autowired
private ClientInfoDao clientInfoDao;
@Autowired
private ClientDao clientDao;
/**
* 根据有效期天数查询有效期截止时间
* @param expiredDate 有效期天数
* @return
*/
public Date getExpiredDate(int expiredDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(mySqlSysDateDao.getSysDate());
calendar.add(Calendar.DAY_OF_YEAR, expiredDate);
return calendar.getTime();
}
/**
* 积分发放
* @param map
* @param pointActiveInfo 活动信息
* @param expiredDate 有效期日期
* @param initDate 采样数据日期,每日发放奖励的活动需要
* @throws Exception
*/
public void sendPoint(Map<String, Object> map, PointActiveInfo pointActiveInfo,
Date expiredDate, String initDate, double giftAmount) throws Exception {
String userId = map.get("CLIENT_ID").toString();
//验证当日指定用户是否已发放配额
if (this.checkRecord(map, pointActiveInfo, initDate)) {
return;
}
if (pointActiveInfo.getGiftAmount() > 0 && pointActiveInfo.getGiftRemain() < giftAmount) {
throw new ServiceException("剩余数量不足");
}
if (pointActiveInfo.getGiftAmount() != 0) {
pointActiveInfo.setGiftRemain(pointActiveInfo.getGiftRemain() - giftAmount);
pointActiveDao.update(pointActiveInfo);
}
PointRecordInfo record = new PointRecordInfo();
record.setActiveId(pointActiveInfo.getId());
record.setUserId(userId);
record.setOccurCount(giftAmount);
record.setRemainCount(giftAmount);
PointUserInfo pointUserInfo = pointUserDao.queryByUserId(userId);
double postAmount;
if (pointUserInfo != null) {
postAmount = pointUserInfo.getPointAmount() + giftAmount;
pointUserInfo.setPointAmount(postAmount);
pointUserDao.update(pointUserInfo);
} else {
postAmount = giftAmount;
pointUserInfo = new PointUserInfo();
pointUserInfo.setPointAmount(postAmount);
pointUserInfo.setUserId(userId);
pointUserDao.insert(pointUserInfo);
}
record.setPostCount(postAmount);
record.setExpiredDate(expiredDate);
record.setBusinessType("0");
record.setRemark("完成" + pointActiveInfo.getName() + "活动");
record.setGiftBy(map.get("remark").toString());
record.setCreateBy("管理员");
pointRecordDao.insert(record);
log.info("用户:" + userId + "," + initDate + "奖励配额已发放");
//发送微信提示
WxUserInfo wxUserInfo = weixinDao.queryByUserId(userId);
if (wxUserInfo != null) {
Map<String, String> rationPointInfo = new HashMap<String, String>();
rationPointInfo.put("openId", wxUserInfo.getOpenId());
rationPointInfo.put("active", pointActiveInfo.getName());
rationPointInfo.put("occurCount", record.getOccurCount().toString());
rationPointInfo.put("postCount", record.getPostCount().toString());
weiXinBiz.postTemplate(rationPointInfo,
"/template/rationPointInfo?openId={openId}&active={active}&occurCount={occurCount}&postCount={postCount}");
}
}
/**
* 检查是否已发放 true:已发放 false:未发放
* @param map
* @param pointActiveInfo
* @param initDate
* @return
*/
public boolean checkRecord(Map<String, Object> map, PointActiveInfo pointActiveInfo,
String initDate) {
String userId = map.get("CLIENT_ID").toString();
//验证当日指定用户是否已发放配额
Map<String, Object> params = new HashMap<String, Object>();
params.put("userId", userId);
if (!"".equals(initDate)) {
params.put("startDate", DateUtil.getNow(DateEnum.DATE) + " 9:00:00");
params.put("endDate", DateUtil.getNow(DateEnum.DATE) + " 23:59:59");
}
params.put("activeId", pointActiveInfo.getId());
params.put("businessType", "0");
if (!StringUtil.isBlank(map.get("flag"))) {
params.put("flag", map.get("flag"));
}
PointRecordInfo validRecord = pointRecordDao.queryRecordByCondition(params);
if (validRecord != null) {
return true;
} else {
return false;
}
}
/**活动_验证直推用户。
* 若直推用户等级高于该用户等级,则返回直推用户会员号、直推用户等级,否则而返回失败
* @param params {clientId : 当前会员号, userLevel : 当前会员会员等级}
* @return {developer : 直推用户 , developerLevel : 直推用户会员等级}
*/
public Map<String, Object> checkDeveloper(Map<String, Object> params) {
Map<String, Object> outmap = new HashMap<String, Object>();
String developer = clientInfoDao
.queryDeveloperByClientId(params.get("clientId").toString());
if ("".equals(developer) || "5001".equals(developer) || StringUtil.isBlank(developer)) {
outmap.put("resultCode", "0");
log.info("用户:" + params.get("clientId") + "没有推荐人");
return outmap;
}
String develoeprLevel = clientDao.queryUserLevel(developer);
//若推荐人等级不高于该会员等级,则返回失败
if (Integer.parseInt(develoeprLevel) <= Integer.parseInt(params.get("userLevel").toString())
&& "0".equals(params.get("baseUser").toString())) {
outmap.put("resultCode", "0");
log.info("等级为:" + params.get("userLevel") + "的会员:" + params.get("clientId") + "的推荐人等级为:"
+ develoeprLevel);
} else {
outmap.put("resultCode", "1");
outmap.put("developer", developer);
outmap.put("developerLevel", develoeprLevel);
}
return outmap;
}
/**活动_计算配额*/
public void setPoint(Map<String, Object> params,
PointActiveInfo pointActiveInfo) throws Exception {
String giftBy = "check,会员为:" + params.get("CLIENT_ID") + ",买入交易额:" + params.get("TOTAL");
params.put("remark", giftBy);
//flag为重复发放验证方式标识,不为空则验证remark字段
params.put("flag", "check");
int num = Integer.parseInt(params.get("NUM").toString());
Date expiredDate = this.getExpiredDate(pointActiveInfo.getExpiredDate());
this.sendPoint(params, pointActiveInfo, expiredDate, params.get("INIT_DATE").toString(),
pointActiveInfo.getRules().get(0).getGiftAmount() * num);
Map<String, Object> inmap = new HashMap<String, Object>();
Map<String, Object> retmap = new HashMap<String, Object>();
for (int i = 0; i <= 4; i++) {
int devPoint;
if (retmap.size() == 0) {
inmap.put("clientId", params.get("CLIENT_ID"));
inmap.put("userLevel", 0);
inmap.put("baseUser", 1);
} else {
inmap.put("clientId", retmap.get("developer"));
inmap.put("userLevel", retmap.get("developerLevel"));
inmap.put("baseUser", 0);
}
retmap = this.checkDeveloper(inmap);
if ("0".equals(retmap.get("resultCode"))) {
break;
}
if (inmap.get("clientId").equals(params.get("CLIENT_ID"))) {
devPoint = 5;
} else {
devPoint = 1;
}
giftBy = "交易用户为:" + params.get("CLIENT_ID") + ",直推用户为:" + inmap.get("clientId")
+ ",推荐人为:" + retmap.get("developer");
retmap.put("flag",
"交易用户为:" + params.get("CLIENT_ID") + ",直推用户为:" + inmap.get("clientId"));
retmap.put("remark", giftBy);
retmap.put("CLIENT_ID", retmap.get("developer"));
this.sendPoint(retmap, pointActiveInfo, expiredDate, params.get("INIT_DATE").toString(),
devPoint * num);
}
}
}