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);
|
}
|
}
|