UserBillDao.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-05-07
  7. *
  8. *
  9. */
  10. namespace app\common\dao\user;
  11. use app\common\dao\BaseDao;
  12. use app\common\model\BaseModel;
  13. use app\common\model\user\UserBill;
  14. /**
  15. * Class UserBillDao
  16. * @package app\common\dao\user
  17. * @author xaboy
  18. * @day 2020/6/22
  19. */
  20. class UserBillDao extends BaseDao
  21. {
  22. /**
  23. * @return BaseModel
  24. * @author xaboy
  25. * @day 2020-03-30
  26. */
  27. protected function getModel(): string
  28. {
  29. return UserBill::class;
  30. }
  31. /**
  32. * @param array $where
  33. * @param $data
  34. * @return int
  35. * @throws \think\db\exception\DbException
  36. * @author xaboy
  37. * @day 2020/6/22
  38. */
  39. public function updateBill(array $where, $data)
  40. {
  41. return UserBill::getDB()->where($where)->limit(1)->update($data);
  42. }
  43. /**
  44. * @param $time
  45. * @return \think\Collection
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @author xaboy
  50. * @day 2020/6/22
  51. */
  52. public function getTimeoutBrokerageBill($time)
  53. {
  54. return UserBill::getDB()->where('create_time', '<=', $time)->where('category', 'brokerage')
  55. ->whereIn('type', ['order_one', 'order_two'])->with('user')->where('status', 0)->select();
  56. }
  57. /**
  58. * @param $uid
  59. * @return float
  60. * @author xaboy
  61. * @day 2020/6/22
  62. */
  63. public function lockBrokerage($uid)
  64. {
  65. return UserBill::getDB()->where('category', 'brokerage')
  66. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid)->where('status', 0)->sum('number');
  67. }
  68. /**
  69. * @param $uid
  70. * @return float
  71. * @author xaboy
  72. * @day 2020/6/22
  73. */
  74. public function totalBrokerage($uid)
  75. {
  76. return UserBill::getDB()->where('category', 'brokerage')
  77. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid)->sum('number');
  78. }
  79. /**
  80. * @param $uid
  81. * @return float
  82. * @author xaboy
  83. * @day 2020/6/22
  84. */
  85. public function yesterdayBrokerage($uid)
  86. {
  87. return getModelTime(UserBill::getDB()->where('category', 'brokerage')
  88. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid), 'yesterday')->sum('number');
  89. }
  90. /**
  91. * @param array $where
  92. * @return \think\db\BaseQuery
  93. * @author xaboy
  94. * @day 2020/6/22
  95. */
  96. public function search(array $where)
  97. {
  98. return UserBill::getDB()->when(isset($where['now_money']) && in_array($where['now_money'], [0, 1, 2]), function ($query) use ($where) {
  99. if ($where['now_money'] == 0)
  100. $query->where('category', 'now_money')->whereIn('type', ['pay_product', 'recharge']);
  101. else if ($where['now_money'] == 1)
  102. $query->where('category', 'now_money')->whereIn('type', 'pay_product');
  103. else if ($where['now_money'] == 2)
  104. $query->where('category', 'now_money')->whereIn('type', 'recharge');
  105. })->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  106. $query->where('uid', $where['uid']);
  107. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  108. $query->where('mer_id', $where['mer_id']);
  109. })->when(isset($where['pm']) && $where['pm'] !== '', function ($query) use ($where) {
  110. $query->where('pm', $where['pm']);
  111. })->when(isset($where['category']) && $where['category'] !== '', function ($query) use ($where) {
  112. $query->where('category', $where['category']);
  113. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  114. $query->where('status', $where['status']);
  115. })->when(isset($where['is_red_brokerage']) && $where['is_red_brokerage'] !== '', function ($query) use ($where) {
  116. $query->where('is_red_brokerage', $where['is_red_brokerage']);
  117. })->when(isset($where['commission_type']) && !empty($where['commission_type']), function ($query) use ($where) {
  118. $query->whereIn('commission_type', $where['commission_type']);
  119. })->when(isset($where['not_commission_type']) && !empty($where['not_commission_type']), function ($query) use ($where) {
  120. $query->whereNotIn('commission_type', $where['not_commission_type']);
  121. });
  122. }
  123. public function searchJoin(array $where)
  124. {
  125. return UserBill::getDB()
  126. ->alias('a')
  127. // ->join('User b', 'a.uid = b.uid')
  128. ->Join('merchant_intention c', 'a.mer_id = c.mer_id')
  129. ->Join('user_city d', 'c.city_id = d.city_id')
  130. ->where("a.status",1)
  131. // ->where('a.source', 1)
  132. ->where("a.type_shop",0)
  133. // ->where("a.mer_id",'>',0)
  134. ->field('a.bill_id,a.pm,a.title,a.number,a.balance,a.mark,a.create_time,a.status,a.uid,a.order_sn,a.source,a.mer_id,a.commission_type')
  135. ->field('c.mer_name')
  136. ->field('d.city_name')
  137. ->group("a.order_sn")
  138. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  139. $query->where('a.type', $where['type']);
  140. })
  141. ->when(isset($where['user_time']) && $where['user_time'] !== '', function ($query) use ($where) {
  142. getModelTime($query, $where['user_time'], 'a.create_time');
  143. })
  144. // ->when(isset($where['city_id']) && $where['city_id'], function ($query) use ($where) {
  145. // $query->where('c.city_id', $where['city_id']);
  146. // })
  147. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  148. $query->whereLike('c.mer_name', "%{$where['keyword']}%");
  149. });
  150. }
  151. public function searchJoins(array $where)
  152. {
  153. return UserBill::getDB()
  154. ->alias('a')
  155. ->join('User b', 'a.uid = b.uid')
  156. ->field('a.bill_id,a.pm,a.title,a.number,a.balance,a.mark,a.create_time,a.status,b.nickname,b.phone,a.uid,a.order_sn,a.source')
  157. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  158. $query->where('a.type', $where['type']);
  159. })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  160. getModelTime($query, $where['date'], 'a.create_time');
  161. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  162. $query->whereLike('a.uid|b.nickname', "%{$where['keyword']}%");
  163. });
  164. }
  165. public function refundBrokerage($order_id, $uid)
  166. {
  167. return UserBill::getDB()->where('link_id', $order_id)->where('uid', $uid)
  168. ->where('category', 'brokerage')->whereIn('type', ['refund_two', 'refund_one'])->sum('number');
  169. }
  170. public function type()
  171. {
  172. return UserBill::getDB()->group('type')->column('title,type');
  173. }
  174. }