dao层

// 调用存储过程
void callProcedureGrantEarnings(@Param("params") Map<String,Object> params);

  Xml

<select id="callProcedureGrantEarnings" statementType="CALLABLE">
{call earnings_proceduce(#{params.result,mode=OUT,jdbcType=VARCHAR})}
</select>

得到的结果就在map中的result中。

存储过程

CREATE DEFINER=`root`@`::` PROCEDURE `earnings_proceduce`(out  result varchar())
label:BEGIN
#收益记录的分配 # 基本参数的定义
# 总金额
DECLARE _total_money BIGINT DEFAULT ;
# 发放配置占比
DECLARE _deduct BIGINT;
# 待发放金额
DECLARE _stay_out BIGINT DEFAULT ;
# 用户最多保存数量
DECLARE _num BIGINT DEFAULT ;
# 查询通宝币总额
DECLARE _tb_num BIGINT DEFAULT ;
# 实际发放金额
DECLARE _amount BIGINT DEFAULT ;
# 定时发放时间分钟
DECLARE _time_mi BIGINT DEFAULT ;
# 收入统计的id
DECLARE _newid BIGINT; # 判断是否遍历全部记录的标记
DECLARE done int default ;
# 标识事务错误
DECLARE err INT DEFAULT ; DECLARE i_id BIGINT;
DECLARE i_num BIGINT; # 使用游标将数据存储到数据库中,并进行实际发放金额的统计
DECLARE cur CURSOR FOR
select c.id,sum(d.numbers) from
( select a.id from sys_user a LEFT JOIN earnings_record b ON a.id = b.user_id and b.status = '' GROUP BY a.id having count(a.id) <= (
select deduct from earnings_manage where type =
)) c join zxt_detail d on c.id = d.creator group by c.id; # 出现错误,设置为1,只要发生异常就回滚
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET err=;
# 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = ;
set result=''; # 查询总金额
select IFNULL(sum(order_money),) into _total_money from other_order where ISNULL(issue_id);
# 如果为0 就退出存储过程
if _total_money = THEN
set result='查询总金额为0,不进行发放';
LEAVE label;
end if; # 查询基本配置
select deduct into _deduct from earnings_manage where type = ;
# 计算待发放金额
set _stay_out=ROUND(_total_money * _deduct /);
# 如果为0 就退出存储过程
if _stay_out = THEN
set result='待发放金额金额为0,不进行发放';
LEAVE label;
end if; # 查询通宝总额
select IFNULL(sum(numbers),) into _tb_num from zxt_detail;
# 如果为0 就退出存储过程
if _tb_num = THEN
set result='通宝总金额为0,不进行发放';
LEAVE label;
end if; # 定时发放的时间
select deduct * into _time_mi from earnings_manage where type = ; # 开启事务
start TRANSACTION;
# 打开游标
open cur;
# 开始循环
read_loop: LOOP
# 提取游标的数据
FETCH cur INTO i_id,i_num;
# 声明结束的时候
IF done = THEN
LEAVE read_loop;
END IF; # 事务的处理
# 获取新的id
set _newid = REPLACE(unix_timestamp(current_timestamp()),'.','');
set i_num = FLOOR( _stay_out * i_num / _tb_num);
# 添加个人收益
IF i_num != THEN
set _amount = _amount+i_num;
INSERT INTO `earnings_record` (`creator`, `user_id`, `status`, `amount`, `create_date`) VALUES ('',i_id,'',i_num, NOW());
end if;
end LOOP read_loop; # 添加总收益
INSERT INTO `earnings_issue` (`id`, `stay_out`, `amount`, `other_id`, `updater`, `update_date`, `creator`, `create_date`, `deduct`, `earnings_sum`, `time_out`) VALUES (_newid, _stay_out, _amount, NULL, '', NOW(), '', NOW(),_total_money - _stay_out, _total_money, _time_mi); # 给订单表绑定任务
update other_order set issue_id = _newid where ISNULL(issue_id); # 如果事务发生错误,就进行回滚
IF err= THEN
# 如果发生回滚就表示发生发生错误
set result='发生了回滚,不进行发放';
ROLLBACK;
ELSE
commit;
end if;
#关闭游标
CLOSE cur; END

  

最新文章

  1. ssh 登录慢?
  2. CI-持续集成(1)-软件工业“流水线”概述
  3. android 自定义 view 和 ViewGroup
  4. C++ STL 学习 :for_each与仿函数(functor)
  5. object-c(oc)内存管理机制详解
  6. Gamit解算脚本
  7. SharePoint 2013 创建一个搜索中心和搜索设置
  8. 深入浅出数据结构C语言班(11)——简要介绍算法时间复杂度
  9. Vue 随机分配的打扫卫生H5 :打扫让我快乐
  10. SpringBoot系列: 与Spring Rest服务交互数据
  11. nginx实践(二)之静态资源web服务(浏览器缓存场景)
  12. Socket.IO学习之基础入门
  13. TexStudio + TexLive 修改字体大小
  14. [转载]Oracle PL/SQL之LOOP循环控制语句
  15. BZOJ 3585 mex
  16. Redis---基础数据结构
  17. 使用kendynet构建异步redis访问服务
  18. Cocos2d-x 3.0 Lua编程 之 响应Android手机的按键
  19. PostgreSQL脱敏示例
  20. python学习笔记(十五)之集合

热门文章

  1. HNOI2017 day1 T3 礼物
  2. 多线程-threading模块
  3. springboot读写分离--temp
  4. 【旧文章搬运】ZwQuerySystemInformation枚举进线程信息
  5. 连接mysql报错-Can&#39;t connect to MySQL server on
  6. 51nod 1247 可能的路径(gcd)
  7. 笔记-JavaWeb学习之旅
  8. css - 单词的自动换行问题
  9. Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推
  10. Two Flowers CodeChef - TWOFL