yujian
2019-05-05 ecfbc24553fb24c788f1735b41a47b4970ce70e1
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
package com.yeshi.fanli.service.impl.push;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.push.PushCouponMapper;
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
import com.yeshi.fanli.entity.push.PushCoupon;
import com.yeshi.fanli.entity.push.PushCouponRecord;
import com.yeshi.fanli.exception.PushException;
import com.yeshi.fanli.exception.push.PushCouponException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.push.PushCouponRecordService;
import com.yeshi.fanli.service.inter.push.PushCouponService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
@Service
public class PushCouponServiceImpl implements PushCouponService {
 
    @Resource
    private PushService pushService;
 
    @Resource
    private ConfigService configService;
 
    @Resource
    private PushCouponMapper pushCouponMapper;
    
    @Resource
    private PushCouponRecordService pushCouponRecordService;
    
    @Resource
    private UserSystemCouponService userSystemCouponService;
    
    @Override
    public int deleteByPrimaryKey(Long id) {
        return pushCouponMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(PushCoupon record) {
        return pushCouponMapper.insert(record);
    }
 
    @Override
    public int insertSelective(PushCoupon record) {
        return pushCouponMapper.insertSelective(record);
    }
 
    @Override
    public PushCoupon selectByPrimaryKey(Long id) {
        return pushCouponMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(PushCoupon record) {
        return pushCouponMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(PushCoupon record) {
        return pushCouponMapper.updateByPrimaryKey(record);
    }
 
 
    @Override
    public List<PushCoupon> listQuery(long start, int count, String key, Integer state) {
        return pushCouponMapper.listQuery(start, count, key, state);
    }
 
    @Override
    public long countQuery(String key, Integer state) {
        return pushCouponMapper.countQuery(key, state);
    }
 
    @Override
    @Transactional
    public void save(PushCoupon record) throws PushCouponException,Exception {
        if (record == null) {
            throw new PushCouponException(1, "参数不正确");
        }
 
        String uids = record.getUids();
        if (StringUtil.isNullOrEmpty(uids)) {
            throw new PushCouponException(1, "发放用户不能为空");
        }
        
        Integer amount = record.getAmount();
        if (amount == null) {
            throw new PushCouponException(1, "券数量不能为空");
        } else if (amount < 1 || amount > 5) {
            throw new PushCouponException(1, "券数量范围是 1~5");
        }
        
 
        Long id = record.getId();
        if (id == null) {
            // 新增
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            // 插入数据
            pushCouponMapper.insert(record);
            
            String[] uidArray = uids.split(",");
            
            if (uidArray != null) {
                for (int i = 0; i < uidArray.length; i++) {
 
                    Long uid = Long.parseLong(uidArray[i]);
                    // 插入记录
                    PushCouponRecord pushCouponRecord = new PushCouponRecord();
                    pushCouponRecord.setPushCoupon(record);
                    pushCouponRecord.setUid(uid);
                    pushCouponRecord.setCreateTime(new Date());
                
                    // 插入领取记录
                    pushCouponRecordService.insertSelective(pushCouponRecord);
                    // 直接发送券
                    userSystemCouponService.randomRewardCoupon(amount, uid, UserSystemCoupon.SOURCE_SYSTEM_PUSH);
                }
            }
            
            
        } else {
//            
//            // 修改
//            PushCoupon current = selectByPrimaryKey(id);
//            if (current == null) {
//                throw new PushCouponException(1, "该记录已不存在");
//            }
//            
//            if(current.isPushed()) {
//                throw new PushCouponException(1, "已推送的信息不能修改");
//            }
//            current.setAmount(amount);
//            current.setUids(record.getUids());
//            current.setRemark(record.getRemark());
//            current.setUpdateTime(new Date());
//            // 更新数据
//            updateByPrimaryKey(current);
        }
    }
 
    @Override
    @Transactional
    public void executePush(Long id) throws Exception, PushCouponException, PushException {
 
        PushCoupon pushCoupon = selectByPrimaryKey(id);
        if (pushCoupon == null) {
            throw new PushCouponException(1, "推送信息已不存在");
        }
        
        if(pushCoupon.isPushed()) {
            throw new PushCouponException(1, "该信息不能重复推送");
        }
        String versions = pushCoupon.getVersions();
        if (versions == null || versions.trim().length() == 0 ) {
            throw new PushCouponException(1, "推送版本不能为空");
        }
        
 
        String title = pushCoupon.getTitle();
        String content = pushCoupon.getContent();
        if (title == null || title.trim().length() == 0 || content == null || content.trim().length() == 0) {
            throw new PushCouponException(1, "标题或内容不能为空");
        }
        
        
        List<String> listuid = null;
        String uids = pushCoupon.getUids();
        if (uids != null && uids.trim().length() > 0) {
            listuid = Arrays.asList(uids.split(","));
            if (listuid == null || listuid.size() == 0) {
                throw new PushCouponException(1, "用户id格式不正确");
            }
        }
        
        List<String> listIOS = new ArrayList<String>();
        JSONObject json = JSONObject.fromObject(versions);
        String versionsIOS = json.getString("IOS");
        if (versionsIOS != null && versionsIOS.trim().length() > 0) {
            if (versionsIOS.contains("全推")) {
                listIOS = null;
            } else {
                listIOS = Arrays.asList(versionsIOS.split(","));
            }
        }
        
        List<String> listAndroid = new ArrayList<String>();
        String versionsAndroid  = json.getString("Android");
        if (versionsAndroid != null && versionsAndroid.trim().length() > 0) {
            if (versionsAndroid.contains("全推")) {
                listAndroid = null;
            } else {
                listAndroid = Arrays.asList(versionsAndroid.split(","));
            }
        }
        
        
        if (listuid == null) {
            // 全推
            pushService.pushWelfareCenter(null, title, content, listIOS, listAndroid);
            
        } else {
            // 指定用户推送
            for (String str_uid: listuid) {
                if (str_uid != null && str_uid.trim().length() > 0) {
                    pushService.pushWelfareCenter(Long.parseLong(str_uid), title, content, listIOS, listAndroid);
                }
            }
        }
        
        // 已推送
        pushCoupon.setPushed(true);
        // 推送时间
        pushCoupon.setPushTime(new Date());
        // 更新时间
        pushCoupon.setUpdateTime(new Date());
        
        updateByPrimaryKeySelective(pushCoupon);
    }
    
 
    @Override
    @Transactional
    public void deleteBatchByPrimaryKey(List<Long> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        
        for (Long id: list) {
            long receivedCount = pushCouponRecordService.countByPushId(id);
            // 领取数量
            if (receivedCount > 0) {
                continue;
            }
            
            pushCouponMapper.deleteByPrimaryKey(id);
        }
    }
 
    @Override
    public List<PushCoupon> listQueryEffective() {
        return pushCouponMapper.listQueryEffective();
    }
    
}