喻健
2018-11-08 a94b7685716096afb867716c96065a60db7b9564
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
package com.yeshi.fanli.service.impl.user;
 
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
import javax.annotation.Resource;
 
import net.sf.json.JSONArray;
 
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.user.ShamUserMapper;
import com.yeshi.fanli.dao.user.ShamUserDao;
import com.yeshi.fanli.entity.bus.user.HongBaoMessage;
import com.yeshi.fanli.entity.bus.user.ShamUser;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.user.ShamUserService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.Utils;
 
@Service
public class ShamUserServiceImpl implements ShamUserService {
    
    @Resource
    private ShamUserDao dao;
    
    @Resource
    private ShamUserMapper shamUserMapper;
    
    @Resource
    private ConfigService configService;
    
    
    @Transactional
    public void addShamUser(ShamUser shamUser) {
        dao.save(shamUser);
    }
 
    public List<ShamUser> getAll() {
        return dao.list("from ShamUser");
    }
 
    
    @SuppressWarnings("unchecked")
    public List<ShamUser> findShamUserList(int sum){
        return (List<ShamUser>) dao.excute(new HibernateCallback<List<ShamUser>>() {
 
            public List<ShamUser> doInHibernate(Session session)
                    throws HibernateException {
                SQLQuery query = session.createSQLQuery("SELECT * FROM yeshi_ec_demouser ORDER BY id desc").addEntity(ShamUser.class);
                query.setFirstResult(0);
                query.setMaxResults(sum);
                List<ShamUser> list = query.list();
                return list;
            }
        });
    }
    
    @SuppressWarnings("unchecked")
    public List<HongBaoMessage> getManyData() {
        
        return (List<HongBaoMessage>) dao.excute(new HibernateCallback<List<HongBaoMessage>>() {
 
            public List<HongBaoMessage> doInHibernate(Session session)
                    throws HibernateException {
                SQLQuery query = session.createSQLQuery("SELECT * FROM yeshi_ec_sham_user ORDER BY RAND()").addEntity(ShamUser.class);
                query.setFirstResult(0);
                query.setMaxResults(Constant.PAGE_SIZE);
                List<ShamUser> list = query.list();
                List<HongBaoMessage> hbmList=new ArrayList<HongBaoMessage>();
                if(list.size()>0){
                    HongBaoMessage hbm = null;
                    DecimalFormat df = new DecimalFormat("######0.00");  
                    for (ShamUser shamUser : list) {
                        hbm=new HongBaoMessage();
                        hbm.setName(shamUser.getName());
                        hbm.setPicUrl(shamUser.getPicUrl());
                        double random = (Math.random()+0.01)*20;
                        String format = df.format(random);
                        hbm.setContent("悄悄领取了"+format+"元红包");
                        hbmList.add(hbm);
                    }
                }
                return hbmList;
            }
        });
    }
    @Transactional
    public void delete(final long id) {
        dao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException {
                    session.createQuery(" delete ShamUser su where su.id = ? ").setParameter(0, id).executeUpdate();
                return null;
            }
        });
    }
 
    @SuppressWarnings("unchecked")
    public List<HongBaoMessage> findShamThreeSaleInfo() {
 
        return (List<HongBaoMessage>) dao.excute(new HibernateCallback<List<HongBaoMessage>>() {
 
            public List<HongBaoMessage> doInHibernate(Session session)
                    throws HibernateException {
                SQLQuery query = session.createSQLQuery("SELECT * FROM yeshi_ec_sham_user ORDER BY RAND()").addEntity(ShamUser.class);
                query.setFirstResult(0);
                query.setMaxResults(5);
                List<ShamUser> list = query.list();
                List<HongBaoMessage> hbmList=new ArrayList<HongBaoMessage>();
                if(list.size()>0){
                    HongBaoMessage hbm = null;
                    DecimalFormat df = new DecimalFormat("######0.00");  
                    for (ShamUser shamUser : list) {
                        String name = shamUser.getName();
                        if(name != null){
                            if(name.length() > 2){
                                name = Utils.getStarString(name,1,name.length()-1);
                            }else{
                                name = name.substring(0, 1)+"**"+name.substring(name.length()-1);
                            }
                        }
                        shamUser.setName(name);
                        hbm=new HongBaoMessage();
                        hbm.setName(shamUser.getName());
                        hbm.setPicUrl(shamUser.getPicUrl());
                        double random = (Math.random()+100)*10;
                        String format = df.format(random);
                        hbm.setContent(format+"");
                        hbmList.add(hbm);
                    }
                }
                Collections.sort(hbmList, new Comparator<HongBaoMessage>() {
                    public int compare(HongBaoMessage o1, HongBaoMessage o2) {
                        double o1m = Double.parseDouble(o1.getContent());
                        double o2m = Double.parseDouble(o2.getContent());
                        double m=o1m-o2m;
                        return m<0?1:-1;
                    }
                });
                return hbmList;
            }
        });
    
    }
 
    @Override
    public int deleteByPrimaryKey(Long id) {
        return shamUserMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(ShamUser record) {
        return shamUserMapper.insert(record);
    }
 
    @Override
    public int insertSelective(ShamUser record) {
        return shamUserMapper.insertSelective(record);
    }
 
    @Override
    public ShamUser selectByPrimaryKey(Long id) {
        return shamUserMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(ShamUser record) {
        return shamUserMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(ShamUser record) {
        return shamUserMapper.updateByPrimaryKey(record);
    }
    
    
    @Override
    public List<ShamUser> listDistinctPicUrl(long start, int count) {
        return shamUserMapper.listByDistinctPicUrl(start, count);
    }
    
    
    @Override
    public List<ShamUser> listRandCouponUser(int count, int smallTime, int largeTime) {
        List<Integer> listTime = new ArrayList<Integer>();
 
        while (listTime.size() < count) {
            int time = smallTime + (int) (Math.random() * (largeTime - smallTime + 1));
            if (!listTime.contains(Integer.valueOf(time))) {
                listTime.add(Integer.valueOf(time));
            }
        }
 
        Collections.sort(listTime);
 
        List<ShamUser> listRand = shamUserMapper.listRand(count);
 
        for (int i = 1; i <= count; i++) {
            ShamUser shamUser = (ShamUser) listRand.get(i - 1);
 
            transformId(shamUser);
 
            shamUser.setMsg(listTime.get(listTime.size() - i) + "秒前领券购买了这个商品");
        }
 
        return listRand;
    }
 
    @Override
    public List<ShamUser> listRandShareUser(int count, BigDecimal shareMoney, int small, int large) {
        
        List<Integer> listMultiple = new ArrayList<Integer>();
 
        while (listMultiple.size() < count) {
            int Multiple = small + (int) (Math.random() * (large - small + 1));
            listMultiple.add(Integer.valueOf(Multiple));
        }
 
        List<String> imgList = new ArrayList<String>();
        String value = configService.get("shareBonusPicture");
 
        if (!StringUtil.isNullOrEmpty(value)) {
            JSONArray array = JSONArray.fromObject(value);
            if (array != null) {
                for (int n = 0; n < array.size(); n++) {
                    imgList.add(array.optString(n));
                }
            }
 
        }
 
        List<ShamUser> listRand = shamUserMapper.listRand(count);
 
        for (int i = 0; i < count; i++) {
            ShamUser shamUser = (ShamUser) listRand.get(i);
 
            transformId(shamUser);
 
            if (imgList.size() > 0) {
                int randBonusPic = (int) (Math.random() * imgList.size());
                shamUser.setShareBonusPic((String) imgList.get(randBonusPic));
            }
 
            BigDecimal money = shareMoney.multiply(new BigDecimal(((Integer) listMultiple.get(i)).intValue())).setScale(2, 1);
            shamUser.setMsg("分享了这个商品,累计获得<font color=\"red\">奖金" + money + "</font>");
        }
 
        return listRand;
    }
 
    public void transformId(ShamUser shamUser) {
        String id = shamUser.getId() + "";
        
        if (id.length() == 1) {
            shamUser.setShowId("2****" + id);
        } else {
            shamUser.setShowId(id.substring(0, 1) + "****" + id.substring(id.length() - 2, id.length() - 1));
        }
    }
 
}