UserBillDao.php 7.5 KB

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