ActiveConfigBiz.java
9.75 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
package com.cjs.cms.biz.user.point;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.dao.user.point.ActiveConfigDao;
import com.cjs.cms.dao.user.point.ActiveGiftDao;
import com.cjs.cms.dao.user.point.ActiveRuleDao;
import com.cjs.cms.model.user.UserInfo;
import com.cjs.cms.model.user.point.ActiveConfigInfo;
import com.cjs.cms.model.user.point.ActiveGiftInfo;
import com.cjs.cms.model.user.point.ActiveRuleInfo;
import com.cjs.cms.util.lang.StringUtil;
import com.cjs.cms.util.web.ActionUtil;
/**
* 活动设置
* @author tongxiaochuan
*
*/
@Service
public class ActiveConfigBiz {
Logger log = LogManager.getLogger();
@Autowired
private ActiveConfigDao activeConfigDao;
@Autowired
private ActiveGiftDao activeGiftDao;
@Autowired
private ActiveRuleDao activeRuleDao;
/**活动新增或修改*/
@Transactional
public Map<String, Object> config(ActiveConfigInfo activeConfigInfo) {
Map<String, Object> outmap = new HashMap<String, Object>();
if (activeConfigInfo.getStartDate().after(activeConfigInfo.getEndDate())) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "活动开始时间必须早于结束时间");
return outmap;
}
if ("0".equals(activeConfigInfo.getStatus())) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "已作废的活动不可修改");
return outmap;
}
UserInfo userInfo = ActionUtil.getUser();
activeConfigInfo.setCreateBy(userInfo.getTruename());
if (activeConfigInfo.getEndDate().after(new Date())) {
activeConfigInfo.setStatus("2");
} else if (activeConfigInfo.getStartDate().after(new Date())) {
activeConfigInfo.setStatus("1");
} else if (activeConfigInfo.getEndDate().before(new Date())) {
activeConfigInfo.setStatus("3");
}
if (StringUtil.isBlank(activeConfigInfo.getId())) {
//新增
activeConfigDao.insert(activeConfigInfo);
if (StringUtil.isBlank(activeConfigInfo.getId())) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "活动信息添加失败");
return outmap;
}
for (ActiveGiftInfo gift : activeConfigInfo.getGifts()) {
gift.setActiveId(activeConfigInfo.getId());
gift.setCreateBy(userInfo.getTruename());
gift.setGiftRemainAmount(gift.getGiftAmount());
activeGiftDao.insert(gift);
if (StringUtil.isBlank(gift.getId())) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "活动奖品信息添加失败");
return outmap;
}
}
} else {
//修改
activeConfigDao.update(activeConfigInfo);
List<ActiveGiftInfo> gifts = activeGiftDao.queryByActiveId(activeConfigInfo.getId());
Set<ActiveGiftInfo> add = new HashSet<ActiveGiftInfo>();
Set<ActiveGiftInfo> update = new HashSet<ActiveGiftInfo>();
Set<Integer> updateIndex = new HashSet<Integer>();
Set<Integer> del = new HashSet<Integer>();
if (activeConfigInfo.getGifts().size() == 0) {
for (ActiveGiftInfo activeGiftInfo : gifts) {
del.add(activeGiftInfo.getId());
}
}
if (gifts.size() == 0) {
for (ActiveGiftInfo activeGiftInfo : activeConfigInfo.getGifts()) {
activeGiftInfo.setActiveId(activeConfigInfo.getId());
activeGiftInfo.setCreateBy(userInfo.getTruename());
activeGiftInfo.setGiftRemainAmount(activeGiftInfo.getGiftAmount());
add.add(activeGiftInfo);
}
}
if (activeConfigInfo.getGifts().size() != 0 && gifts.size() != 0) {
//修改的奖品
for (ActiveGiftInfo oldGift : gifts) {
for (ActiveGiftInfo newGift : activeConfigInfo.getGifts()) {
if (oldGift.getGiftCode().equals(newGift.getGiftCode())) {
int mixAmount = oldGift.getGiftAmount() - newGift.getGiftAmount();
if (oldGift.getGiftRemainAmount() - mixAmount < 0) {
outmap.put("resultCode", "0");
outmap.put("resultMsg",
"奖品:" + newGift.getGiftCode() + "的剩余数量为:"
+ oldGift.getGiftRemainAmount()
+ ",奖励总量的减少数量不可超过此数量。");
return outmap;
} else {
newGift
.setGiftRemainAmount(oldGift.getGiftRemainAmount() - mixAmount);
}
newGift.setCreateBy(userInfo.getTruename());
newGift.setActiveId(activeConfigInfo.getId());
newGift.setId(oldGift.getId());
update.add(newGift);
updateIndex.add(oldGift.getId());
}
}
}
//新增的奖品
for (ActiveGiftInfo activeGiftInfo : update) {
activeConfigInfo.getGifts().remove(activeGiftInfo);
}
for (ActiveGiftInfo activeGiftInfo : activeConfigInfo.getGifts()) {
activeGiftInfo.setGiftRemainAmount(activeGiftInfo.getGiftAmount());
activeGiftInfo.setCreateBy(userInfo.getTruename());
activeGiftInfo.setActiveId(activeConfigInfo.getId());
add.add(activeGiftInfo);
}
//删除的奖品
List<String> giftsId = new ArrayList<String>();
for (ActiveGiftInfo activeGiftInfo : gifts) {
giftsId.add(activeGiftInfo.getId().toString());
}
for (Integer index : updateIndex) {
giftsId.remove(index.toString());
}
for (String str : giftsId) {
del.add(Integer.parseInt(str));
}
}
for (Integer index : del) {
activeGiftDao.delete(index);
}
for (ActiveGiftInfo activeGiftInfo : update) {
activeGiftDao.update(activeGiftInfo);
}
for (ActiveGiftInfo activeGiftInfo : add) {
activeGiftDao.insert(activeGiftInfo);
}
}
outmap.put("resultCode", "1");
outmap.put("resultMsg", "操作完毕");
return outmap;
}
public ActiveConfigInfo searchConfig(int activeId) {
ActiveConfigInfo config = activeConfigDao.queryByIdForUpdate(activeId);
log.info(config);
return activeConfigDao.queryByIdForUpdate(activeId);
}
/**根据活动id查询规则*/
public List<ActiveRuleInfo> searchRule(int activeId) {
return activeRuleDao.queryByConfigId(activeId);
}
/**根据活动id查询奖品编码*/
public List<Map<String, Object>> searchGiftCode(int activeId) {
List<String> list = activeGiftDao.queryGiftCodeByActiveId(activeId);
List<Map<String, Object>> returnlist = new ArrayList<Map<String, Object>>();
for (String str : list) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", str);
map.put("value", str);
returnlist.add(map);
}
return returnlist;
}
/**设定活动规则*/
@Transactional
public Map<String, Object> addRule(ActiveConfigInfo config) {
Map<String, Object> outmap = new HashMap<String, Object>();
log.info(config);
if ("0".equals(config.getStatus())) {
outmap.put("resultCode", "0");
outmap.put("resultMsg", "已作废的活动不可修改");
return outmap;
}
UserInfo userInfo = ActionUtil.getUser();
int activeId = config.getId();
activeRuleDao.deleteByActiveId(config.getId());
Map<String, Object> queryGift = new HashMap<String, Object>();
ActiveGiftInfo gift;
for (ActiveRuleInfo rule : config.getRules()) {
queryGift.put("activeId", activeId);
queryGift.put("giftCode", rule.getActiveGiftInfo().getGiftCode());
int giftId = activeGiftDao.queryGiftId(queryGift);
rule.setActiveId(activeId);
rule.setGiftId(giftId);
rule.setCreateBy(userInfo.getTruename());
activeRuleDao.insert(rule);
gift = activeGiftDao.queryById(giftId);
gift.setGiftType(rule.getActiveGiftInfo().getGiftType());
activeGiftDao.update(gift);
}
outmap.put("resultCode", "1");
outmap.put("resultMsg", "操作完毕");
return outmap;
}
/**根据藏品编号查询藏品名*/
public String queryGiftName(String giftCode) {
return activeGiftDao.queryGiftName(giftCode);
}
/**取消活动*/
public void cancelActive(int activeId) {
activeConfigDao.cancel(activeId);
}
}