UserBillDao.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\common\dao\user;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\user\UserBill;
  6. /**
  7. * Class UserBillDao
  8. * @package app\common\dao\user
  9. * @author zfy
  10. * @day 2020/6/22
  11. */
  12. class UserBillDao extends BaseDao
  13. {
  14. /**
  15. * @return BaseModel
  16. * @author zfy
  17. * @day 2020-03-30
  18. */
  19. protected function getModel(): string
  20. {
  21. return UserBill::class;
  22. }
  23. /**
  24. * @param array $where
  25. * @param $data
  26. * @return int
  27. * @throws \think\db\exception\DbException
  28. * @author zfy
  29. * @day 2020/6/22
  30. */
  31. public function updateBill(array $where, $data)
  32. {
  33. return UserBill::getDB()->where($where)->limit(1)->update($data);
  34. }
  35. /**
  36. * @param $time
  37. * @return \think\Collection
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @author zfy
  42. * @day 2020/6/22
  43. */
  44. public function getTimeoutBrokerageBill($time)
  45. {
  46. return UserBill::getDB()->where('create_time', '<=', $time)->where('category', 'brokerage')
  47. ->whereIn('type', ['order_one', 'order_two'])->with('user')->where('status', 0)->select();
  48. }
  49. public function getTimeoutIntegralBill($time)
  50. {
  51. return UserBill::getDB()->where('create_time', '<=', $time)->where('category', 'integral')
  52. ->where('type', 'lock')->with('user')->where('status', 0)->select();
  53. }
  54. public function getTimeoutMerchantMoneyBill($time)
  55. {
  56. return UserBill::getDB()->where('create_time', '<=', $time)->where('category', 'mer_lock_money')
  57. ->where('status', 0)->select();
  58. }
  59. public function refundMerchantMoney($order_id, $type, $mer_id)
  60. {
  61. return UserBill::getDB()->where('link_id', $order_id)->where('mer_id', $mer_id)
  62. ->where('category', 'mer_refund_money')->where('type', $type)->sum('number');
  63. }
  64. public function merchantLickMoney($merId = null)
  65. {
  66. $lst = UserBill::getDB()->where('category', 'mer_lock_money')->when($merId, function ($query, $val) {
  67. $query->where('mer_id', $val);
  68. })->where('status', 0)->select()->toArray();
  69. $lockMoney = 0;
  70. if (count($lst)) {
  71. $lockMoney = -1 * UserBill::getDB()->whereIn('link_id', array_column($lst, 'link_id'))
  72. ->where('category', 'mer_refund_money')->sum('number');
  73. }
  74. foreach ($lst as $bill) {
  75. $lockMoney = bcadd($lockMoney, $bill['number'], 2);
  76. }
  77. return $lockMoney;
  78. }
  79. /**
  80. * @param $uid
  81. * @return float
  82. * @author zfy
  83. * @day 2020/6/22
  84. */
  85. public function lockBrokerage($uid)
  86. {
  87. $lst = UserBill::getDB()->where('category', 'brokerage')
  88. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid)->where('status', 0)->field('link_id,number')->select()->toArray();
  89. $refundPrice = 0;
  90. if (count($lst)) {
  91. $refundPrice = -1 * UserBill::getDB()->whereIn('link_id', array_column($lst, 'link_id'))->where('uid', $uid)
  92. ->where('category', 'brokerage')->whereIn('type', ['refund_two', 'refund_one'])->sum('number');
  93. }
  94. foreach ($lst as $bill) {
  95. $refundPrice = bcadd($refundPrice, $bill['number'], 2);
  96. }
  97. return $refundPrice;
  98. }
  99. public function lockIntegral($uid = null, $order_id = null)
  100. {
  101. $lst = UserBill::getDB()->where('category', 'integral')
  102. ->where('type', 'lock')->when($order_id, function ($query, $order_id) {
  103. $query->where('link_id', $order_id);
  104. })->when($uid, function ($query, $uid) {
  105. $query->where('uid', $uid);
  106. })->where('status', 0)->field('link_id,number')->select()->toArray();
  107. $lockIntegral = 0;
  108. if (count($lst)) {
  109. $lockIntegral = -1 * UserBill::getDB()->whereIn('link_id', array_column($lst, 'link_id'))->where('uid', $uid)
  110. ->where('category', 'integral')->where('type', 'refund_lock')->sum('number');
  111. }
  112. foreach ($lst as $bill) {
  113. $lockIntegral = bcadd($lockIntegral, $bill['number'], 0);
  114. }
  115. return $lockIntegral;
  116. }
  117. public function deductionIntegral($uid)
  118. {
  119. return UserBill::getDB()->where('uid', $uid)
  120. ->where('category', 'integral')->where('type', 'deduction')->sum('number');
  121. }
  122. public function totalGainIntegral($uid)
  123. {
  124. return UserBill::getDB()->where('uid', $uid)
  125. ->where('category', 'integral')->where('pm', 1)->whereNotIn('type', ['refund', 'cancel'])->sum('number');
  126. }
  127. /**
  128. * @param $uid
  129. * @return float
  130. * @author zfy
  131. * @day 2020/6/22
  132. */
  133. public function totalBrokerage($uid)
  134. {
  135. return bcsub(UserBill::getDB()->where('category', 'brokerage')
  136. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid)->sum('number'),
  137. UserBill::getDB()->where('uid', $uid)
  138. ->where('category', 'brokerage')->whereIn('type', ['refund_two', 'refund_one'])->sum('number'), 2);
  139. }
  140. /**
  141. * @param $uid
  142. * @return float
  143. * @author zfy
  144. * @day 2020/6/22
  145. */
  146. public function yesterdayBrokerage($uid)
  147. {
  148. return getModelTime(UserBill::getDB()->where('category', 'brokerage')
  149. ->whereIn('type', ['order_one', 'order_two'])->where('uid', $uid), 'yesterday')->sum('number');
  150. }
  151. /**
  152. * @param array $where
  153. * @return \think\db\BaseQuery
  154. * @author zfy
  155. * @day 2020/6/22
  156. */
  157. public function search(array $where)
  158. {
  159. return UserBill::getDB()
  160. ->when(isset($where['now_money']) && in_array($where['now_money'], [0, 1, 2]), function ($query) use ($where) {
  161. if ($where['now_money'] == 0)
  162. $query->where('category', 'now_money')->whereIn('type', ['pay_product', 'recharge', 'sys_inc_money', 'sys_dec_money', 'brokerage', 'presell', 'refund']);
  163. else if ($where['now_money'] == 1)
  164. $query->where('category', 'now_money')->whereIn('type', ['pay_product', 'sys_dec_money', 'presell']);
  165. else if ($where['now_money'] == 2)
  166. $query->where('category', 'now_money')->whereIn('type', ['recharge', 'sys_inc_money', 'brokerage', 'refund']);
  167. })
  168. ->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  169. $query->where('uid', $where['uid'])->where('mer_id', 0);
  170. })
  171. ->when(isset($where['pm']) && $where['pm'] !== '', function ($query) use ($where) {
  172. $query->where('pm', $where['pm']);
  173. })
  174. ->when(isset($where['category']) && $where['category'] !== '', function ($query) use ($where) {
  175. $query->where('category', $where['category']);
  176. })
  177. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  178. $query->where('status', $where['status']);
  179. })
  180. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  181. getModelTime($query, $where['date'], 'create_time');
  182. })
  183. ->when(isset($where['day']) && $where['day'] !== '', function ($query) use ($where) {
  184. $query->whereDay('create_time', $where['day']);
  185. })
  186. ->when(isset($where['month']) && $where['month'] !== '', function ($query) use ($where) {
  187. $query->whereMonth('create_time', $where['month']);
  188. })
  189. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  190. $query->where('type', $where['type']);
  191. })
  192. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  193. $query->where('mer_id', $where['mer_id']);
  194. })
  195. ->when(isset($where['link_id']) && $where['link_id'] !== '', function ($query) use ($where) {
  196. $query->where('link_id', $where['link_id']);
  197. });
  198. }
  199. public function userNowMoneyIncTotal($uid)
  200. {
  201. return $this->search(['uid' => $uid, 'now_money' => 2])->sum('number');
  202. }
  203. public function searchJoin(array $where)
  204. {
  205. return UserBill::getDB()->alias('a')->leftJoin('User b', 'a.uid = b.uid')
  206. ->field('a.bill_id,a.pm,a.title,a.number,a.balance,a.mark,a.create_time,a.status,b.nickname,a.uid,a.category')
  207. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  208. $query->where('a.mer_id',$where['mer_id']);
  209. })
  210. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  211. $query->where('a.type', $where['type']);
  212. })
  213. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  214. getModelTime($query, $where['date'], 'a.create_time');
  215. })
  216. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  217. $query->whereLike('a.uid|b.nickname|a.title', "%{$where['keyword']}%");
  218. })
  219. ->when(isset($where['category']) && $where['category'] !== '', function ($query) use ($where) {
  220. $query->where('a.category', $where['category']);
  221. })
  222. ;
  223. }
  224. public function refundBrokerage($order_id, $uid)
  225. {
  226. return UserBill::getDB()->where('link_id', $order_id)->where('uid', $uid)
  227. ->where('category', 'brokerage')->whereIn('type', ['refund_two', 'refund_one'])->sum('number');
  228. }
  229. public function refundIntegral($order_id, $uid)
  230. {
  231. return UserBill::getDB()->where('link_id', $order_id)->where('uid', $uid)
  232. ->where('category', 'integral')->where('type', 'refund_lock')->sum('number');
  233. }
  234. public function validIntegral($uid, $start, $end)
  235. {
  236. $lst = UserBill::getDB()->where('category', 'integral')
  237. ->where('type', 'lock')->whereBetween('create_time', [$start, $end])->where('uid', $uid)->where('status', 1)->field('link_id,number')->select()->toArray();
  238. $integral = 0;
  239. if (count($lst)) {
  240. $integral = -1 * UserBill::getDB()->whereIn('link_id', array_column($lst, 'link_id'))->where('uid', $uid)
  241. ->where('category', 'integral')->where('type', 'refund_lock')->sum('number');
  242. }
  243. foreach ($lst as $bill) {
  244. $integral = bcadd($integral, $bill['number'], 0);
  245. }
  246. $integral2 = UserBill::getDB()->where('uid', $uid)->whereBetween('create_time', [$start, $end])
  247. ->where('category', 'integral')->where('pm', 1)->whereNotIn('type', ['lock', 'refund'])->sum('number');
  248. $integral3 = UserBill::getDB()->where('uid', $uid)->whereBetween('create_time', [$start, $end])
  249. ->where('category', 'integral')->where('type', 'sys_dec')->sum('number');
  250. return (int)max(bcsub(bcadd($integral, $integral2, 0), $integral3, 0), 0);
  251. }
  252. }