package com.yeshi.fanli.service.impl.user; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.InputStream; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.UUID; import javax.annotation.Resource; import javax.transaction.Transactional; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.yeshi.fanli.dao.mybatis.UserRankingsMapper; import com.yeshi.fanli.entity.admin.InviteGetMoney; import com.yeshi.fanli.entity.bus.user.HongBao; import com.yeshi.fanli.entity.bus.user.UserInfo; import com.yeshi.fanli.entity.bus.user.UserRankings; import com.yeshi.fanli.exception.UserRankingsException; import com.yeshi.fanli.service.inter.hongbao.HongBaoService; import com.yeshi.fanli.service.inter.user.UserRankingsService; import com.yeshi.fanli.util.StringUtil; import org.yeshi.utils.JsonUtil; import org.yeshi.utils.tencentcloud.COSManager; @Service public class UserRankingsServiceImpl implements UserRankingsService { @Resource private UserRankingsMapper userRankingsMapper; @Resource private HongBaoService hongBaoService; @Override public int insertSelective(UserRankings record) throws UserRankingsException { // TODO Auto-generated method stub return userRankingsMapper.insertSelective(record); } @Override public int updateByPrimaryKey(UserRankings record) throws UserRankingsException { return userRankingsMapper.updateByPrimaryKey(record); } @Override public int updateByPrimaryKeySelective(UserRankings record) throws UserRankingsException { return userRankingsMapper.updateByPrimaryKeySelective(record); } @Override public int updateBatchSelective(List list) throws UserRankingsException { return userRankingsMapper.updateBatchSelective(list); } @Override @Transactional public int deleteBatchById(Long id) throws UserRankingsException { return userRankingsMapper.deleteByPrimaryKey(id); } @Override public void updateClearTradeState() throws UserRankingsException { userRankingsMapper.updateClearTradeState(); } @Override @Transactional public int deleteBatchByPrimaryKey(List arrayID) throws UserRankingsException { for (Long id : arrayID) { UserRankings userRankings = userRankingsMapper.selectByPrimaryKey(id); String portrait = userRankings.getPortrait(); if (!StringUtil.isNullOrEmpty(portrait)) { COSManager.getInstance().deleteFile(portrait); } userRankingsMapper.deleteByPrimaryKey(id); } return arrayID.size(); } @Override public UserRankings selectByPrimaryKey(Long id) throws UserRankingsException { // TODO Auto-generated method stub return userRankingsMapper.selectByPrimaryKey(id); } @Override public List selectByNickName(String nickName) throws UserRankingsException { // TODO Auto-generated method stub return userRankingsMapper.selectByNickName(nickName); } @Override public List selectByShowId(String id) throws UserRankingsException { // TODO Auto-generated method stub return userRankingsMapper.selectByShowId(id); } @Override public void insertAndUploadFile(UserRankings record, MultipartFile file) throws Exception { // TODO Auto-generated method stub record.setCreatetime(new Date()); userRankingsMapper.insertSelective(record); // 上传图片 if (file != null) { uploadPicture(file, record); } } @Override public void uploadPicture(MultipartFile file, UserRankings userRankings) throws Exception { /* 修改图片时,先删除已存在图片 */ String portrait = userRankings.getPortrait(); if (!StringUtil.isNullOrEmpty(portrait)) { COSManager.getInstance().deleteFile(portrait); } String contentType = file.getContentType(); String type = contentType.substring(contentType.indexOf("/") + 1); // 上传文件相对位置 String fileUrl = "userRankings/" + UUID.randomUUID().toString().replace("-", "") + "." + type; InputStream inputStream = file.getInputStream(); String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl(); /* 更新数据库信息 */ if (!StringUtil.isNullOrEmpty(uploadFilePath)) { userRankings.setPortrait(uploadFilePath); userRankingsMapper.updateByPrimaryKeySelective(userRankings); } } @Override public void removePicture(UserRankings userRankings) throws Exception { /* 修改图片时,先删除已存在图片 */ String portrait = userRankings.getPortrait(); if (!StringUtil.isNullOrEmpty(portrait)) { boolean deleteFile = COSManager.getInstance().deleteFile(portrait); if (deleteFile) { userRankings.setPortrait(null); userRankingsMapper.updateByPrimaryKey(userRankings); } } } @Override public List query(long start, int count, String key, Integer tradeState, Integer shareState, Integer sort) throws UserRankingsException { return userRankingsMapper.query(start, count, key, tradeState, shareState, sort); } @Override public long queryCount(String key, Integer tradeState, Integer shareState) throws UserRankingsException { return userRankingsMapper.queryCount(key, tradeState, shareState); } @Override @Transactional public void setRewardtBatch(List idList, Integer type, Double reward, Double startReward, Double endReward) throws Exception { for (Long id : idList) { UserRankings userRankings = userRankingsMapper.selectByPrimaryKey(id); if (userRankings == null) { continue; } Double result = 0.00; if (reward != null && reward >= 0) { result = reward; } else { if (startReward != null && endReward != null) { // 随机权重 result = startReward + (Math.random() * ((endReward - startReward) + 1)); } else { break; } } if (type == 1 || type.equals(1)) { userRankings.setTradeReward(new BigDecimal(result)); } else { userRankings.setShareReward(new BigDecimal(result)); } userRankingsMapper.updateByPrimaryKeySelective(userRankings); } } @Override @Transactional public void setStateBatch(List idList, Integer type, Integer state) throws Exception { for (Long id : idList) { UserRankings userRankings = userRankingsMapper.selectByPrimaryKey(id); if (userRankings == null) { continue; } if (type == 1 || type.equals(1)) { userRankings.setTradeState(state); if (state != null && state.equals(1)) { userRankings.setTradeTime(new Date()); } } else { userRankings.setShareState(state); if (state != null && state.equals(1)) { userRankings.setShareTime(new Date()); } } userRankingsMapper.updateByPrimaryKey(userRankings); } } @Override @Transactional public void uploadFile(File file) throws Exception { List list = new ArrayList(); File[] files = file.listFiles(); for (File filePic : files) {// 遍历这个数组 String name = filePic.getName(); System.out.println("name" + name); String prefix = name.substring(name.lastIndexOf(".") + 1); // 上传文件相对位置 String fileUrl = "userRankings/" + UUID.randomUUID().toString().replace("-", "") + "." + prefix; System.out.println(fileUrl); FileInputStream is = new FileInputStream(filePic); String uploadFilePath = COSManager.getInstance().uploadFile(is, fileUrl).getUrl(); /* 更新数据库信息 */ if (!StringUtil.isNullOrEmpty(uploadFilePath)) { UserRankings userRankings = new UserRankings(); userRankings.setPortrait(uploadFilePath); userRankings.setCreatetime(new Date()); list.add(userRankings); } is.close(); } if (list.size() > 0) { userRankingsMapper.insertBatch(list); } } @Override @Transactional public void updateShowID(File file) throws Exception { List listID = new ArrayList(); BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件 String s = ""; while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行 listID.add(s); } br.close(); int i = 0; List list = userRankingsMapper.queryAll(); for (UserRankings userRankings : list) { String showId = listID.get(i); i++; userRankings.setShowId(showId); } userRankingsMapper.updateBatchSelective(list); } @Override @Transactional public void batchUpNickName(File file) throws Exception { List listID = new ArrayList(); BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件 String s = ""; while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行 listID.add(s); } br.close(); int i = 0; List list = userRankingsMapper.queryAll(); for (UserRankings userRankings : list) { String nickName = listID.get(i); i++; userRankings.setNickName(nickName); } userRankingsMapper.updateBatchSelective(list); } @Override public List listQueryByIds(List ids) throws UserRankingsException { return userRankingsMapper.listQueryByIds(ids); } @Override public List getRank(long start, int count) { List resultList = new ArrayList(); List rankingsList = new ArrayList(); try { List userRankingsList = query(0, count, null, 1, null, 2); for (UserRankings userRankings : userRankingsList) { InviteGetMoney invite = new InviteGetMoney(); String showId = userRankings.getShowId(); if (StringUtil.isNullOrEmpty(showId)) { showId = "2***5"; } invite.setUid(showId); invite.setName(userRankings.getNickName()); // invite.setIcon(userRankings.getPortrait()); invite.setPortrait(userRankings.getPortrait()); invite.setMoney(userRankings.getTradeReward()); invite.setCreateTime(userRankings.getCreatetime()); rankingsList.add(invite); } Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式 String defaultDate = sdf.format(calendar.getTime()); // 格式化前一天 // 真实用户 List listHongBao = hongBaoService.listShareAndInviteMoney(0, count, defaultDate); if (listHongBao != null && listHongBao.size() > 0) { HongBao hongBao = listHongBao.get(0); BigDecimal maxMoney = hongBao.getShareAndInviteMoney(); UserRankings userRankings = userRankingsList.get(userRankingsList.size() - 1); BigDecimal mminMoney = userRankings.getTradeReward(); // 真实数据大于最低金额 if (maxMoney.compareTo(mminMoney) > 0) { for (HongBao hb : listHongBao) { BigDecimal rewardMoney = hb.getShareAndInviteMoney(); // 奖金高到低排序,不满足结束循环 if (rewardMoney.compareTo(mminMoney) <= 0) { break; } UserInfo userInfo = hb.getUserInfo(); if (userInfo != null) { InviteGetMoney invite = new InviteGetMoney(); invite.setName(userInfo.getNickName()); // invite.setIcon(userRankings.getPortrait()); invite.setPortrait(userInfo.getPortrait()); invite.setMoney(rewardMoney); invite.setCreateTime(new Date()); // 处理id Long id = userInfo.getId(); String uid = id + ""; String index = uid.substring(0, 1); String end = uid.substring(uid.length() - 1, uid.length()); invite.setUid(index + "***" + end); rankingsList.add(invite); } } } } List list = listSort(rankingsList); if (list.size() > count) { resultList = list.subList(0, count-1); } else { resultList = list; } for (int i = 0 ;i listSort(List list) { Collections.sort(list, new Comparator() { public int compare(InviteGetMoney p1, InviteGetMoney p2) { // 按照Person的年龄进行升序排列 if (p1.getMoney().compareTo(p2.getMoney()) < 0) { return 1; } if (p1.getMoney().compareTo(p2.getMoney()) == 0) { return 0; } return -1; } }); return list; } }