UserBillDao.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\BaseModel;
  14. use app\common\model\user\UserBill;
  15. /**
  16. * Class UserBillDao
  17. * @package app\common\dao\user
  18. * @author xaboy
  19. * @day 2020/6/22
  20. */
  21. class UserBillDao extends BaseDao
  22. {
  23. /**
  24. * @return BaseModel
  25. * @author xaboy
  26. * @day 2020-03-30
  27. */
  28. protected function getModel(): string
  29. {
  30. return UserBill::class;
  31. }
  32. /**
  33. * @param array $where
  34. * @param $data
  35. * @return int
  36. * @throws \think\db\exception\DbException
  37. * @author xaboy
  38. * @day 2020/6/22
  39. */
  40. public function updateBill(array $where, $data)
  41. {
  42. return UserBill::getDB()->where($where)->limit(1)->update($data);
  43. }
  44. /**
  45. * @param $time
  46. * @return \think\Collection
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @author xaboy
  51. * @day 2020/6/22
  52. */
  53. public function getTimeoutBrokerageBill($time)
  54. {
  55. return UserBill::getDB()->where('create_time', '<=', $time)->where('category', 'brokerage')
  56. ->whereIn('type', ['order_one', 'order_two'])->with('user')->where('status', 0)->select();
  57. }
  58. /**
  59. * @param $uid
  60. * @return float
  61. * @author xaboy
  62. * @day 2020/6/22
  63. */
  64. public function lockBrokerage($uid)
  65. {
  66. return UserBill::getDB()->where('category', 'brokerage')
  67. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid)->where('status', 0)->sum('number');
  68. }
  69. /**
  70. * @param $uid
  71. * @return float
  72. * @author xaboy
  73. * @day 2020/6/22
  74. */
  75. public function totalBrokerage($uid)
  76. {
  77. return UserBill::getDB()->where('category', 'brokerage')
  78. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid)->sum('number');
  79. }
  80. /**
  81. * @param $uid
  82. * @return float
  83. * @author xaboy
  84. * @day 2020/6/22
  85. */
  86. public function yesterdayBrokerage($uid)
  87. {
  88. return getModelTime(UserBill::getDB()->where('category', 'brokerage')
  89. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid), 'yesterday')->sum('number');
  90. }
  91. /**
  92. * @param array $where
  93. * @return \think\db\BaseQuery
  94. * @author xaboy
  95. * @day 2020/6/22
  96. */
  97. public function search(array $where)
  98. {
  99. return UserBill::getDB()->when(isset($where['now_money']) && in_array($where['now_money'], [0, 1, 2]), function ($query) use ($where) {
  100. if ($where['now_money'] == 0)
  101. $query->where('category', 'now_money')->whereIn('type', ['pay_product', 'recharge', 'sys_inc_money', 'sys_dec_money', 'brokerage', 'presell', 'refund']);
  102. else if ($where['now_money'] == 1)
  103. $query->where('category', 'now_money')->whereIn('type', ['pay_product', 'sys_dec_money', 'presell']);
  104. else if ($where['now_money'] == 2)
  105. $query->where('category', 'now_money')->whereIn('type', ['recharge', 'sys_inc_money', 'brokerage', 'refund']);
  106. })->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  107. $query->where('uid', $where['uid']);
  108. })->when(isset($where['pm']) && $where['pm'] !== '', function ($query) use ($where) {
  109. $query->where('pm', $where['pm']);
  110. })->when(isset($where['category']) && $where['category'] !== '', function ($query) use ($where) {
  111. $query->where('category', $where['category']);
  112. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  113. $query->where('status', $where['status']);
  114. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  115. getModelTime($query, $where['date'], 'create_time');
  116. })->when(isset($where['day']) && $where['day'] !== '', function ($query) use ($where) {
  117. $query->whereDay('create_time',$where['day']);
  118. })->when(isset($where['month']) && $where['month'] !== '', function ($query) use ($where) {
  119. $query->whereMonth('create_time',$where['month']);
  120. });
  121. }
  122. public function userNowMoneyIncTotal($uid)
  123. {
  124. return $this->search(['uid' => $uid, 'now_money' => 2])->sum('number');
  125. }
  126. public function searchJoin(array $where)
  127. {
  128. return UserBill::getDB()->alias('a')->leftJoin('User b', 'a.uid = b.uid')
  129. ->field('a.bill_id,a.pm,a.title,a.number,a.balance,a.mark,a.create_time,a.status,b.nickname,a.uid')
  130. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  131. $query->where('a.type', $where['type']);
  132. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  133. getModelTime($query, $where['date'], 'a.create_time');
  134. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  135. $query->where('a.uid|b.nickname', "%{$where['keyword']}%");
  136. });
  137. }
  138. public function refundBrokerage($order_id, $uid)
  139. {
  140. return UserBill::getDB()->where('link_id', $order_id)->where('uid', $uid)
  141. ->where('category', 'brokerage')->whereIn('type', ['refund_two', 'refund_one'])->sum('number');
  142. }
  143. }