yujian
2020-05-09 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64
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
package com.yeshi.fanli.service.impl.user;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
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.user.ShamUserMapper;
import com.yeshi.fanli.entity.bus.user.ShamUser;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.user.ShamUserService;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONArray;
 
@Service
public class ShamUserServiceImpl implements ShamUserService {
 
    @Resource
    private ShamUserMapper shamUserMapper;
 
    @Resource
    private ConfigService configService;
 
    @Transactional
    public void addShamUser(ShamUser shamUser) {
        shamUserMapper.insertSelective(shamUser);
    }
 
    public List<ShamUser> getAll() {
        return shamUserMapper.listAll();
    }
 
    public List<ShamUser> findShamUserList(int sum) {
        return shamUserMapper.listShamUserByKey(0, sum, null);
    }
 
    @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 int insertBatch(List<ShamUser> list) {
        return shamUserMapper.insertBatch(list);
    }
 
    @Override
    public int updateBatchSelective(List<ShamUser> list) {
        return shamUserMapper.updateBatchSelective(list);
    }
 
    @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(ConfigKeyEnum.shareBonusPicture.getKey());
 
        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);
            // 必须包含 【奖金¥】 这3个符号, 用于前端颜色区分
            shamUser.setMsg("分享了这个商品,累计获得奖金¥" + money);
        }
 
        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));
        }
        
        String name = shamUser.getName();
        if(StringUtil.isNullOrEmpty(name)) {
            // 名字为空时
            String start = randomName();
            String end = randomName();
            shamUser.setName(start + "**" +end);
        } else if (name.length() == 1) {
            // 一个字
            String start = name;
            if (start.trim().length() == 0 || start.equals("*")) {
                start = randomName();
            }
            String end = randomName();
            shamUser.setName(start + "**" +end);
        } else {
            // 两个字及其以上
            String start = name.substring(0, 1);
            String end = name.substring(name.length() - 2, name.length() - 1);
            if (start.trim().length() == 0 || start.equals("*")) {
                start = randomName();
            }
            if (end.trim().length() == 0 || end.equals("*")) {
                end = randomName();
            }
            shamUser.setName(start + "**" +end);
        }
    }
    
    /**
     * 随机名称
     * @return
     */
    public String randomName() {
        String[] array = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                "我","半","士","思","一","成","城","罗","落","花","化","夫","福","扶","浮","生","若","梦","你","繁","华","三","起","七","易","已","意","衣",
                "且","听","风","峰","枫","音","吟","绝","倔","空","离","丽","厘","留","六","流","瘤","陆","鏐","陸","紅","誰","為","美","陳","塵","車","晨",
                "光","零","鱗","藺","離","口","了","酒","舊","傷","賞","殤","橋","巧","喬","愛","啊","嗄","譚","談","貪","銀","汗","航","孔","名","明","明","闵"
                ,"铭","鸣","冥","飞","费","非","废","达","去","曲","渠","小","笑","晓","消","校","西","妖","娆","破","眸","凉","微","兮","つ","越","黑","舊","素",
                "憾","无","っ","つ","烟","東","烁","弃","绕","雪","月","航"};
        return array[(int) (Math.random() * array.length-1)];
    }
 
    @Override
    public List<ShamUser> listRandUser(int count) {
        return shamUserMapper.listRand(count);
    }
}