Commit d910a637 authored by licc's avatar licc

优化sql

parent 9aab3052
...@@ -14,4 +14,6 @@ public interface UserDataMapper extends BaseMapper<UserData> { ...@@ -14,4 +14,6 @@ public interface UserDataMapper extends BaseMapper<UserData> {
int edit(UserData userData); int edit(UserData userData);
UserData getByUserId(@Param("userId") Long userId); UserData getByUserId(@Param("userId") Long userId);
Long getMaxId();
} }
...@@ -14,4 +14,11 @@ public interface UserRechargeMapper extends BaseMapper<UserRechargeMapper> { ...@@ -14,4 +14,11 @@ public interface UserRechargeMapper extends BaseMapper<UserRechargeMapper> {
*/ */
Double getTotalRecharge(@Param("userId") Long userId); Double getTotalRecharge(@Param("userId") Long userId);
/**
* 获取充币地址
* @param userId 用户id
* @return 充币地址
*/
String getReceiveAddr(@Param("userId") Long userId);
} }
...@@ -71,7 +71,15 @@ public interface UsersMapper extends BaseMapper<Users> { ...@@ -71,7 +71,15 @@ public interface UsersMapper extends BaseMapper<Users> {
*/ */
Integer getTotal(@Param("id") Long id); Integer getTotal(@Param("id") Long id);
List<UserDto> getAllUserData(); /**
* 获取用户 基本数据
*
* @param startNumber 开始编号
* @param endNo 结束编号
* @return 列表
*/
List<UserDto> getAllUserData(@Param("startNo") Integer startNumber, @Param("endNo") Integer endNo);
/*************** chenqi****************/ /*************** chenqi****************/
......
...@@ -15,12 +15,13 @@ ...@@ -15,12 +15,13 @@
<result column="total_people" property="totalPeople"/> <result column="total_people" property="totalPeople"/>
<result column="buy_total" property="buyTotal"/> <result column="buy_total" property="buyTotal"/>
<result column="sale_total" property="saleTotal"/> <result column="sale_total" property="saleTotal"/>
<result column="receive_addr" property="receiveAddr"/>
<result column="created_at" property="createdAt"/> <result column="created_at" property="createdAt"/>
<result column="updated_at" property="updatedAt"/> <result column="updated_at" property="updatedAt"/>
</resultMap> </resultMap>
<sql id="table"> <sql id="table">
user_date user_data
</sql> </sql>
<sql id="cols_all"> <sql id="cols_all">
...@@ -30,12 +31,12 @@ ...@@ -30,12 +31,12 @@
<sql id="cols_exclude_id"> <sql id="cols_exclude_id">
user_id,real_name,id_number,phone,total_recharge,total_withdrawal,rank, bottom,total_people,buy_total, user_id,real_name,id_number,phone,total_recharge,total_withdrawal,rank, bottom,total_people,buy_total,
sale_total,created_at,updated_at sale_total,receive_addr,created_at,updated_at
</sql> </sql>
<sql id="vals"> <sql id="vals">
#{userId},#{realName},#{idNumber},#{phone},#{totalRecharge},#{totalWithdrawal},#{rank},#{bottom}, #{userId},#{realName},#{idNumber},#{phone},#{totalRecharge},#{totalWithdrawal},#{rank},#{bottom},
#{totalPeople},#{buyTotal},#{saleTotal},now(),now() #{totalPeople},#{buyTotal},#{saleTotal},#{receiveAddr},now(),now()
</sql> </sql>
<sql id="updateCondition"> <sql id="updateCondition">
...@@ -50,22 +51,24 @@ ...@@ -50,22 +51,24 @@
<if test="totalPeople != null">total_people = #{totalPeople},</if> <if test="totalPeople != null">total_people = #{totalPeople},</if>
<if test="buyTotal != null">buy_total = #{buyTotal},</if> <if test="buyTotal != null">buy_total = #{buyTotal},</if>
<if test="saleTotal != null">sale_total =#{saleTotal},</if> <if test="saleTotal != null">sale_total =#{saleTotal},</if>
<if test="receiveAddr != null">receive_addr =#{receiveAddr},</if>
updated_at =now() updated_at =now()
</sql> </sql>
<sql id="criteria"> <sql id="criteria">
<if test="id != null">id = #{id}</if> <if test="id != null">id = #{id}</if>
<if test="userId != null">and user_id = #{userId},</if> <if test="userId != null">and user_id = #{userId}</if>
<if test="realName != null">and real_name =#{realName},</if> <if test="realName != null">and real_name =#{realName}</if>
<if test="idNumber != null">and id_number =#{idNumber},</if> <if test="idNumber != null">and id_number =#{idNumber}</if>
<if test="phone != null">and phone =#{phone},</if> <if test="phone != null">and phone =#{phone}</if>
<if test="totalRecharge != null">and total_recharge = #{totalRecharge},</if> <if test="totalRecharge != null">and total_recharge = #{totalRecharge}</if>
<if test="totalWithdrawal != null">and total_withdrawal =#{totalWithdrawal},</if> <if test="totalWithdrawal != null">and total_withdrawal =#{totalWithdrawal}</if>
<if test="rank != null">and rank =#{rank},</if> <if test="rank != null">and rank =#{rank}</if>
<if test="bottom != null">and bottom =#{bottom},</if> <if test="bottom != null">and bottom =#{bottom}</if>
<if test="totalPeople != null">and total_people = #{totalPeople},</if> <if test="totalPeople != null">and total_people = #{totalPeople}</if>
<if test="buyTotal != null">and buy_total = #{buyTotal},</if> <if test="buyTotal != null">and buy_total = #{buyTotal}</if>
<if test="saleTotal != null">and sale_total =#{saleTotal},</if> <if test="saleTotal != null">and sale_total =#{saleTotal}</if>
<if test="receiveAddr != null">and receive_addr =#{receiveAddr}</if>
<if test="createdAt != null">and created_at &gt;= #{createdAt}</if> <if test="createdAt != null">and created_at &gt;= #{createdAt}</if>
<if test="updatedAt != null">and #{updatedAt} &gt;= updated_at</if> <if test="updatedAt != null">and #{updatedAt} &gt;= updated_at</if>
</sql> </sql>
...@@ -101,4 +104,8 @@ ...@@ -101,4 +104,8 @@
</where> </where>
</select> </select>
<select id="getMaxId" resultType="java.lang.Long">
select user_id from user_data order by user_id desc limit 1
</select>
</mapper> </mapper>
...@@ -99,4 +99,8 @@ ...@@ -99,4 +99,8 @@
select sum(amount) from user_recharge where status=1 and user_id=#{userId} select sum(amount) from user_recharge where status=1 and user_id=#{userId}
</select> </select>
<select id="getReceiveAddr" resultType="java.lang.String">
select receive_addr from user_recharge where status=1 and user_id=#{userId} limit 1
</select>
</mapper> </mapper>
...@@ -172,12 +172,13 @@ ...@@ -172,12 +172,13 @@
<select id="getLevelAndTotal" resultType="cn.wisenergy.model.dto.UsersInfoDto"> <select id="getLevelAndTotal" resultType="cn.wisenergy.model.dto.UsersInfoDto">
SELECT SELECT u1.rank,u2.count
MAX(rank) 'bottom', FROM
count(*)-1 'totalPeople' users u1,(select count(*) 'count',max(LENGTH(path)) 'maxrank'
FROM users from users
WHERE where path like CONCAT((SELECT path FROM users WHERE id=#{userId}), '%')) u2
FIND_IN_SET(#{userId},path) WHERE path like CONCAT((SELECT path FROM users WHERE id=#{userId}), '%')
and LENGTH(path) = u2.maxrank LIMIT 1
</select> </select>
<select id="getTotal" resultType="java.lang.Integer"> <select id="getTotal" resultType="java.lang.Integer">
...@@ -239,6 +240,7 @@ GROUP BY user_id ...@@ -239,6 +240,7 @@ GROUP BY user_id
select id as userId,rank,phone select id as userId,rank,phone
from from
<include refid="table"/> <include refid="table"/>
limit #{startNo},#{endNo}
</select> </select>
</mapper> </mapper>
...@@ -51,6 +51,9 @@ public class UserData { ...@@ -51,6 +51,9 @@ public class UserData {
@ApiModelProperty(name = "saleTotal", value = "otc卖出总额") @ApiModelProperty(name = "saleTotal", value = "otc卖出总额")
private Double saleTotal; private Double saleTotal;
@ApiModelProperty(name = "receiveAddr", value = "充币地址")
private String receiveAddr;
private Date createdAt; private Date createdAt;
private Date updatedAt; private Date updatedAt;
......
...@@ -6,6 +6,7 @@ import lombok.Data; ...@@ -6,6 +6,7 @@ import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
/** /**
* @author 86187
* @Authotr:陈奇 * @Authotr:陈奇
* @QQ1799796883 * @QQ1799796883
*/ */
......
package cn.wisenergy.model.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author 86187
*/
@Data
@ApiModel(value = "UserQueryVo")
public class UserQueryVo {
@ApiModelProperty(value = "要统计的数量", name = "number")
private Integer number;
}
package cn.wisenergy.service.app; package cn.wisenergy.service.app;
import cn.wisenergy.common.utils.R; import cn.wisenergy.common.utils.R;
import cn.wisenergy.model.vo.UserQueryVo;
/** /**
* @author 86187 * @author 86187
...@@ -9,8 +10,9 @@ public interface UserDataService { ...@@ -9,8 +10,9 @@ public interface UserDataService {
/** /**
* 批量添加用户统计数据 * 批量添加用户统计数据
* *
* @param queryVo 查询参数
* @return true or false * @return true or false
*/ */
R<Boolean> addBatch(); R<Boolean> addBatch(UserQueryVo queryVo);
} }
...@@ -4,8 +4,10 @@ import cn.wisenergy.common.utils.R; ...@@ -4,8 +4,10 @@ import cn.wisenergy.common.utils.R;
import cn.wisenergy.mapper.*; import cn.wisenergy.mapper.*;
import cn.wisenergy.model.app.Actives; import cn.wisenergy.model.app.Actives;
import cn.wisenergy.model.app.UserData; import cn.wisenergy.model.app.UserData;
import cn.wisenergy.model.app.Users;
import cn.wisenergy.model.dto.UserDto; import cn.wisenergy.model.dto.UserDto;
import cn.wisenergy.model.dto.UsersInfoDto; import cn.wisenergy.model.dto.UsersInfoDto;
import cn.wisenergy.model.vo.UserQueryVo;
import cn.wisenergy.service.Manager.UserDataManger; import cn.wisenergy.service.Manager.UserDataManger;
import cn.wisenergy.service.app.UserDataService; import cn.wisenergy.service.app.UserDataService;
import org.apache.shiro.util.CollectionUtils; import org.apache.shiro.util.CollectionUtils;
...@@ -38,20 +40,32 @@ public class UserDataServiceImpl implements UserDataService { ...@@ -38,20 +40,32 @@ public class UserDataServiceImpl implements UserDataService {
@Resource @Resource
private UserDataManger userDataManger; private UserDataManger userDataManger;
@Resource
private UserDataMapper userDataMapper;
@Override @Override
public R<Boolean> addBatch() { public R<Boolean> addBatch(UserQueryVo queryVo) {
if (null == queryVo.getNumber() || queryVo.getNumber() == 0) {
return R.error("参数不能为空或0!");
}
Long usersId = userDataMapper.getMaxId();
if (null == usersId) {
usersId = 0L;
}
Integer startNo = Math.toIntExact(usersId);
Integer endNo = queryVo.getNumber();
//获取所有用户数据 //获取所有用户数据
List<UserDto> userDtos = usersMapper.getAllUserData(); List<UserDto> userDtos = usersMapper.getAllUserData(startNo, endNo);
if (CollectionUtils.isEmpty(userDtos)) { if (CollectionUtils.isEmpty(userDtos)) {
return R.ok(1, false); return R.ok(1, false);
} }
UserData userData = new UserData();
List<UserData> list = new ArrayList<>(); List<UserData> list = new ArrayList<>();
for (UserDto userDto : userDtos) { for (UserDto userDto : userDtos) {
Long userId = userDto.getUserId(); Long userId = userDto.getUserId();
UserData userData = new UserData();
//1、用户基本信息 //1、用户基本信息
userData.setUserId(userDto.getUserId()); userData.setUserId(userDto.getUserId());
...@@ -76,27 +90,25 @@ public class UserDataServiceImpl implements UserDataService { ...@@ -76,27 +90,25 @@ public class UserDataServiceImpl implements UserDataService {
//4、充值总额 //4、充值总额
Double totalRecharge = userRechargeMapper.getTotalRecharge(userId); Double totalRecharge = userRechargeMapper.getTotalRecharge(userId);
if (null != totalRecharge) {
userData.setTotalRecharge(totalRecharge); userData.setTotalRecharge(totalRecharge);
}
//5、提现总额 //5、提现总额
Double totalWithdrawal = withdrawsMapper.getTotalWithdrawal(userId); Double totalWithdrawal = withdrawsMapper.getTotalWithdrawal(userId);
if (null != totalWithdrawal) {
userData.setTotalWithdrawal(totalWithdrawal); userData.setTotalWithdrawal(totalWithdrawal);
}
//6、otc卖出总额 //6、otc卖出总额
Double buyTotal = orderDetailsMapper.getBuyTotal(userId); Double buyTotal = orderDetailsMapper.getBuyTotal(userId);
if (null != buyTotal) {
userData.setBuyTotal(buyTotal); userData.setBuyTotal(buyTotal);
}
//7、otc买入总额 //7、otc买入总额
Double saleTotal = orderDetailsMapper.getSaleTotal(userId); Double saleTotal = orderDetailsMapper.getSaleTotal(userId);
if (null != saleTotal) {
userData.setSaleTotal(saleTotal); userData.setSaleTotal(saleTotal);
}
//8、充币地址
String receiveAddr = userRechargeMapper.getReceiveAddr(userId);
userData.setReceiveAddr(receiveAddr);
list.add(userData); list.add(userData);
} }
......
package cn.wisenergy.web.admin.controller.app; package cn.wisenergy.web.admin.controller.app;
import cn.wisenergy.common.utils.R; import cn.wisenergy.common.utils.R;
import cn.wisenergy.model.vo.UserQueryVo;
import cn.wisenergy.service.app.UserDataService; import cn.wisenergy.service.app.UserDataService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Reference;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -24,8 +25,9 @@ public class UserDataController { ...@@ -24,8 +25,9 @@ public class UserDataController {
private UserDataService userDataService; private UserDataService userDataService;
@ApiOperation(value = "获取token接口", notes = "获取token接口", httpMethod = "POST") @ApiOperation(value = "获取token接口", notes = "获取token接口", httpMethod = "POST")
@ApiImplicitParam(name = "queryVo", value = "每次统计条数", dataType = "UserQueryVo")
@PostMapping(value = "/user/saveBatchUserData") @PostMapping(value = "/user/saveBatchUserData")
public R<Boolean> saveBatchUserData() { public R<Boolean> saveBatchUserData(@RequestBody UserQueryVo queryVo) {
return userDataService.addBatch(); return userDataService.addBatch(queryVo);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment