UserRechargeDao.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\user;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\user\UserRecharge;
  14. use app\common\repositories\store\order\StoreOrderRepository;
  15. use think\db\BaseQuery;
  16. /**
  17. * Class UserRechargeDao
  18. * @package app\common\dao\user
  19. * @author xaboy
  20. * @day 2020/6/2
  21. */
  22. class UserRechargeDao extends BaseDao
  23. {
  24. /**
  25. * @return string
  26. * @author xaboy
  27. * @day 2020/6/2
  28. */
  29. protected function getModel(): string
  30. {
  31. return UserRecharge::class;
  32. }
  33. /**
  34. * 创建用户充值订单ID
  35. *
  36. * 本函数用于生成用户充值订单的唯一标识。订单ID由固定前缀、当前时间戳和自增序列组成,
  37. * 保证了订单ID的唯一性。其中,自增序列基于当天已创建的充值订单数量,确保了每天的订单号不重复。
  38. *
  39. * @param int $uid 用户ID。用于区分不同用户的订单。
  40. * @return string 返回生成的订单ID。
  41. */
  42. public function createOrderId($uid)
  43. {
  44. // 统计当天已创建的用户充值订单数量
  45. $count = (int)UserRecharge::getDB()->where('uid', $uid)->where('create_time', '>=', date("Y-m-d"))->where('create_time', '<', date("Y-m-d", strtotime('+1 day')))->count();
  46. // 生成订单ID,格式为:类型前缀 + 当前时间戳 + 用户ID + 当天订单计数
  47. return StoreOrderRepository::TYPE_SN_USER_RECHARGE . date('YmdHis', time()) . ($uid . $count);
  48. }
  49. /**
  50. * 计算用户的充值总额
  51. *
  52. * 本函数用于查询指定用户的所有已支付充值订单的总价,并返回这个总价。
  53. * 充值订单的数据通过UserRecharge模型来访问和操作。
  54. *
  55. * @param int $uid 用户ID
  56. * 本参数指定需要查询充值总额的用户,通过用户ID在数据库中定位到相应的充值记录。
  57. * @return float 用户的充值总额
  58. * 函数返回一个浮点数,代表指定用户的所有已支付充值订单的总价。
  59. */
  60. public function userRechargePrice($uid)
  61. {
  62. // 使用UserRecharge模型的getDB方法获取数据库实例
  63. // 然后通过where语句筛选出uid为$uid且paid为1(表示已支付)的充值记录
  64. // 最后使用sum方法计算这些记录的price列的总和,即用户的充值总额
  65. return UserRecharge::getDB()->where('uid', $uid)->where('paid', 1)->sum('price');
  66. }
  67. /**
  68. * 根据条件查询用户充值记录,并进行数据关联和字段筛选。
  69. * 本函数用于构建查询用户充值记录的SQL语句,通过加入条件来筛选特定的充值记录。
  70. *
  71. * @param array $where 查询条件数组,包含keyword(关键字)、paid(支付状态)、date(日期)等条件。
  72. * @return \think\db\Query 返回构建好的查询对象,可用于进一步操作或获取数据。
  73. */
  74. public function searchJoinQuery(array $where)
  75. {
  76. // 使用UserRecharge模型获取数据库对象,并设置表别名为a
  77. return UserRecharge::getDB()->alias('a')
  78. // 加入用户表b,根据uid进行关联
  79. ->join('User b', 'a.uid = b.uid')
  80. // 指定查询的字段,包括充值相关字段和用户相关字段
  81. ->field('a.paid,a.order_id,a.recharge_id,b.nickname,b.avatar,a.price,a.give_price,a.recharge_type,a.pay_time,a.recharge_type')
  82. // 当keyword条件存在且不为空时,添加模糊搜索条件,搜索昵称或订单号
  83. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  84. $query->whereLike('b.nickname|a.order_id', "%{$where['keyword']}%");
  85. })
  86. // 当paid条件存在且不为空时,按支付状态进行筛选
  87. ->when(isset($where['paid']) && $where['paid'] !== '', function ($query) use ($where) {
  88. $query->where('a.paid', $where['paid']);
  89. })
  90. ->when(isset($where['recharge_type']) && $where['recharge_type'] !== '', function ($query) use ($where) {
  91. if ($where['recharge_type'] == 1) {
  92. $query->whereIn('a.recharge_type', ['h5','weixin','routine']);
  93. } else {
  94. $query->whereIn('a.recharge_type', 'alipay');
  95. }
  96. })
  97. ->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  98. $query->where('b.uid', $where['uid']);
  99. })
  100. ->when(isset($where['real_name']) && $where['real_name'] !== '', function ($query) use ($where) {
  101. $query->whereLike('b.real_name', "%{$where['real_name']}%");
  102. })
  103. ->when(isset($where['nickname']) && $where['nickname'] !== '', function ($query) use ($where) {
  104. $query->whereLike('b.nickname', "%{$where['nickname']}%");
  105. })
  106. ->when(isset($where['phone']) && $where['phone'] !== '', function ($query) use ($where) {
  107. $query->whereLike('b.phone', "%{$where['phone']}%");
  108. })
  109. // 当date条件存在且不为空时,根据指定的日期范围筛选充值记录
  110. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  111. getModelTime($query, $where['date'], 'a.create_time');
  112. });
  113. }
  114. /**
  115. * 计算所有已支付充值的总金额
  116. *
  117. * 本函数通过查询用户充值表中支付状态为已支付的记录,计算这些记录的充值金额总和。
  118. * 这样做的目的是为了获取系统中已实际完成充值交易的用户资金总流入额。
  119. *
  120. * @return float 返回已支付充值记录的金额总和
  121. */
  122. public function totalPayPrice()
  123. {
  124. // 使用UserRecharge模型的getDB方法获取数据库实例,并通过where条件筛选出支付状态为1(已支付)的记录,然后计算这些记录的price列的总和
  125. return UserRecharge::getDB()->where('paid', 1)->sum('price');
  126. }
  127. /**
  128. * 计算已退款的总金额
  129. *
  130. * 本函数用于查询并返回所有已支付订单的退款总金额。
  131. * 它通过查询UserRecharge表中paid状态为1(表示已支付)的订单的refund_price列的总和来实现。
  132. *
  133. * @return float 已退款的总金额
  134. */
  135. public function totalRefundPrice()
  136. {
  137. // 使用getDB方法获取数据库实例,并通过where子句筛选出已支付的订单,然后计算refund_price列的总和
  138. return UserRecharge::getDB()->where('paid', 1)->sum('refund_price');
  139. }
  140. /**
  141. * 计算所有常规充值的总价。
  142. *
  143. * 本函数通过查询用户充值记录,筛选出充值类型为常规且已支付的订单,
  144. * 然后计算这些订单的充值金额总和。
  145. *
  146. * @return float 返回所有常规充值的总价。
  147. */
  148. public function totalRoutinePrice()
  149. {
  150. // 使用UserRecharge模型的getDB方法获取数据库实例,并链式调用where方法筛选出充值类型为常规且已支付的记录,最后调用sum方法计算价格总和
  151. return UserRecharge::getDB()->where('paid', 1)->where('recharge_type', 'routine')->sum('price');
  152. }
  153. /**
  154. * 计算微信支付的总金额
  155. *
  156. * 本函数通过查询用户充值记录,筛选出支付状态为已支付且充值方式为H5或微信的记录,
  157. * 然后计算这些记录的充值金额总和。这个函数主要用于统计通过微信支付的总收入。
  158. *
  159. * @return float 返回微信支付的总金额
  160. */
  161. public function totalWxPrice()
  162. {
  163. // 使用UserRecharge模型的getDB方法获取数据库实例,并链式调用where和whereIn方法设定查询条件,最后调用sum方法计算价格总和
  164. return UserRecharge::getDB()->where('paid', 1)->whereIn('recharge_type', ['h5', 'wechat'])->sum('price');
  165. }
  166. }