PointActiveBiz.java
11.8 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.cjs.cms.biz.user.point;
import java.lang.reflect.InvocationTargetException;
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 org.springframework.transaction.annotation.Transactional;
import com.cjs.cms.biz.pub.QuartzBiz;
import com.cjs.cms.biz.user.WeiXinBiz;
import com.cjs.cms.dao.pub.MySqlSysDateDao;
import com.cjs.cms.dao.pub.QuartzTaskConfigDao;
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.PointRuleDao;
import com.cjs.cms.dao.user.point.PointUserDao;
import com.cjs.cms.model.pub.QuartzTaskConfigInfo;
import com.cjs.cms.model.user.UserInfo;
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.PointRuleInfo;
import com.cjs.cms.model.user.point.PointUserInfo;
import com.cjs.cms.util.lang.JsonUtil;
import com.cjs.cms.util.lang.SpringContextUtil;
import com.cjs.cms.util.lang.StringUtil;
import com.cjs.cms.util.web.ActionUtil;
/**
* 配额任务
* @author tongxiaochuan
*
*/
@Service
public class PointActiveBiz {
@Autowired
private PointActiveDao pointActiveDao;
@Autowired
private PointRuleDao pointRuleDao;
@Autowired
private PointUserDao pointUserDao;
@Autowired
private PointRecordDao pointRecordDao;
@Autowired
private ClientDao clientDao;
@Autowired
private ClientInfoDao clientInfoDao;
@Autowired
private WeixinDao weixinDao;
@Autowired
private WeiXinBiz weiXinBiz;
@Autowired
private MySqlSysDateDao mySqlSysDateDao;
@Autowired
private QuartzTaskConfigDao quartzTaskConfigDao;
@Autowired
private PointCommonBiz pointCommonBiz;
Logger log = LogManager.getLogger();
/**任务设置*/
public Map<String, Object> config(PointActiveInfo pointActiveInfo) {
Map<String, Object> outmap = new HashMap<String, Object>();
if (pointActiveInfo.getEndDate().before(pointActiveInfo.getStartDate())) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "活动开始时间必须早于结束时间");
return outmap;
}
if ("0".equals(pointActiveInfo.getStatus())) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "已作废的活动不可修改");
return outmap;
}
UserInfo userInfo = ActionUtil.getUser();
pointActiveInfo.setCreateBy(userInfo.getTruename());
if (pointActiveInfo.getEndDate().after(new Date())) {
pointActiveInfo.setStatus("1");
} else if (pointActiveInfo.getEndDate().before(new Date())) {
pointActiveInfo.setStatus("2");
}
if (StringUtil.isBlank(pointActiveInfo.getGiftAmount())) {
pointActiveInfo.setGiftAmount(0.00);
}
//新增
if (StringUtil.isBlank(pointActiveInfo.getId())) {
pointActiveInfo.setGiftRemain(pointActiveInfo.getGiftAmount());
pointActiveDao.insert(pointActiveInfo);
if (StringUtil.isBlank(pointActiveInfo.getId())) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "活动信息添加失败");
return outmap;
}
} else {
//修改
if (pointActiveInfo.getGiftAmount() == 0) {
pointActiveInfo.setGiftRemain(0.00);
} else {
PointActiveInfo oldInfo = pointActiveDao.queryActiveById(pointActiveInfo.getId());
double mixAmount = pointActiveInfo.getGiftAmount() - oldInfo.getGiftAmount();
if (oldInfo.getGiftRemain() + mixAmount < 0) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "奖品总量设置错误,当前奖品剩余量为:" + oldInfo.getGiftRemain());
return outmap;
} else {
pointActiveInfo.setGiftRemain(oldInfo.getGiftRemain() + mixAmount);
}
}
pointActiveDao.update(pointActiveInfo);
pointRuleDao.deleteByActiveId(pointActiveInfo.getId());
}
for (PointRuleInfo rule : pointActiveInfo.getRules()) {
rule.setActiveId(pointActiveInfo.getId());
rule.setCreateBy(userInfo.getTruename());
pointRuleDao.insert(rule);
if (StringUtil.isBlank(rule.getId())) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "活动规则信息添加失败");
return outmap;
}
}
this.setQuartz(pointActiveInfo);
outmap.put("resultCode", "1");
outmap.put("resultMsg", "操作完毕");
return outmap;
}
/**设置定时任务*/
public void setQuartz(PointActiveInfo pointActiveInfo) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("taskType", "1");
params.put("activeId", pointActiveInfo.getId());
params.put("remark", "active_status_start");
QuartzTaskConfigInfo task = quartzTaskConfigDao.queryTaskByCondition(params);
if (task == null) {
task = new QuartzTaskConfigInfo();
task.setClassName("com.cjs.cms.biz.point.PointQuartzBiz");
task.setActiveId(pointActiveInfo.getId());
task.setTaskType("1");
task.setStartDate(pointActiveInfo.getStartDate());
task.setSchedType("2");
task.setIntervalMs(1000);
task.setTimes(1);
task.setStatus("0");
task.setRemark("active_status_start");
//设置活动开始时间
quartzTaskConfigDao.insert(task);
task.setStartDate(pointActiveInfo.getEndDate());
task.setRemark("active_status_end");
//设置活动结束时间
quartzTaskConfigDao.insert(task);
} else {
task.setStartDate(pointActiveInfo.getStartDate());
quartzTaskConfigDao.updateStartDate(task);
params.put("remark", "active_status_end");
task = quartzTaskConfigDao.queryTaskByCondition(params);
task.setStartDate(pointActiveInfo.getEndDate());
quartzTaskConfigDao.updateStartDate(task);
}
QuartzBiz.addJob(task);
}
public PointActiveInfo searchById(int id) {
return pointActiveDao.queryActiveById(id);
}
/**任务作废*/
public Map<String, Object> cancel(int id) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("id", id);
params.put("status", "0");
pointActiveDao.updateStatus(params);
Map<String, Object> outmap = new HashMap<String, Object>();
outmap.put("resultCode", "1");
outmap.put("resultMsg", "操作完成");
return outmap;
}
/**配额发放*/
@SuppressWarnings("unchecked")
public String executer(int id) {
String resultMsg;
PointActiveInfo active = pointActiveDao.queryActiveById(id);
String[] str = active.getExecuter().split(",");
try {
Class execlass = Class.forName(str[0]);
Object bean = SpringContextUtil.getBean(execlass);
resultMsg = (String) bean.getClass().getMethod(str[2], new Class[] { int.class })
.invoke(bean, new Object[] { id });
} catch (Exception e) {
String msg = "";
if (e instanceof InvocationTargetException) {
Throwable targetEx = ((InvocationTargetException) e).getTargetException();
if (targetEx != null) {
msg = targetEx.getMessage();
}
} else {
msg = e.getMessage();
}
log.info(msg);
resultMsg = JsonUtil.toFormJson(msg, false);
}
return resultMsg;
}
/**补偿发放
* @throws Exception */
@Transactional
public String compensationPayment(Map<String, Object> params) throws Exception {
log.info(params);
PointActiveInfo pointActiveInfo = pointActiveDao
.queryActiveById(Integer.parseInt(params.get("activeId").toString()));
if (pointActiveInfo.getGiftAmount() > 0 && pointActiveInfo.getGiftRemain() < Double
.parseDouble(params.get("occurCount").toString())) {
return JsonUtil.toFormJson("活动配额剩余数量不足", false);
}
//扣减剩余量
if (pointActiveInfo.getGiftAmount() > 0) {
pointActiveInfo
.setGiftRemain(pointActiveInfo.getGiftRemain()
- Double.parseDouble(params.get("occurCount").toString()));
pointActiveDao.update(pointActiveInfo);
}
//发放配额记录
PointRecordInfo pointRecordInfo = new PointRecordInfo();
pointRecordInfo.setActiveId(Integer.parseInt(params.get("activeId").toString()));
pointRecordInfo.setOccurCount(Double.parseDouble(params.get("occurCount").toString()));
pointRecordInfo.setUserId(params.get("userId").toString());
UserInfo userInfo = ActionUtil.getUser();
PointUserInfo pointUserInfo = pointUserDao.queryByUserId(pointRecordInfo.getUserId());
double postCount;
if (pointUserInfo != null) {
postCount = pointUserInfo.getPointAmount() + pointRecordInfo.getOccurCount();
pointUserInfo.setPointAmount(postCount);
pointUserDao.update(pointUserInfo);
} else {
postCount = pointRecordInfo.getOccurCount();
pointUserInfo = new PointUserInfo();
pointUserInfo.setUserId(pointRecordInfo.getUserId());
pointUserInfo.setPointAmount(postCount);
pointUserDao.insert(pointUserInfo);
}
pointRecordInfo.setBusinessType("1");
pointRecordInfo.setRemainCount(pointRecordInfo.getOccurCount());
pointRecordInfo.setPostCount(postCount);
Calendar calendar = Calendar.getInstance();
calendar.setTime(mySqlSysDateDao.getSysDate());
calendar.add(Calendar.DAY_OF_YEAR, Integer.parseInt(params.get("expiredDate").toString()));
Date date = calendar.getTime();
pointRecordInfo.setExpiredDate(date);
pointRecordInfo.setRemark(params.get("remark").toString());
pointRecordInfo.setCreateBy(userInfo.getTruename());
pointRecordDao.insert(pointRecordInfo);
//发送微信提示
PointActiveInfo activeInfo = pointActiveDao.queryActiveById(pointRecordInfo.getActiveId());
WxUserInfo wxUserInfo = weixinDao.queryByUserId(pointRecordInfo.getUserId());
if (wxUserInfo != null) {
Map<String, String> rationPointInfo = new HashMap<String, String>();
rationPointInfo.put("openId", wxUserInfo.getOpenId());
rationPointInfo.put("active", activeInfo.getName());
rationPointInfo.put("occurCount", pointRecordInfo.getOccurCount().toString());
rationPointInfo.put("postCount", pointRecordInfo.getPostCount().toString());
weiXinBiz.postTemplate(rationPointInfo,
"/template/rationPointInfo?openId={openId}&active={active}&occurCount={occurCount}&postCount={postCount}");
}
return JsonUtil.toFormJson("操作完毕", true);
}
/**根据用户号查询用户名*/
public String queryNameById(String clientId) {
return clientDao.queryNameById(clientId);
}
}