Order.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php
  2. namespace App\Modules\Trade\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. class Order extends Model
  6. {
  7. protected $table = 'orders';
  8. protected $fillable = ['distribution_channel_id', 'uid', 'created_at', 'product_id', 'price', 'status', 'pay_type', 'trade_no', 'pay_merchant_id', 'servicer', 'transaction_id', 'pay_end_at', 'create_ip', 'send_order_id', 'send_order_name', 'activity_id'];
  9. static function getByTradeNo($trade_no)
  10. {
  11. return self::where(['trade_no' => $trade_no])->first();
  12. }
  13. //查询
  14. static function search($params = [], $is_all = false)
  15. {
  16. // \Log::info('search:'.json_encode($params));
  17. $search_obj = self::orderBy('id', 'desc')->select(['orders.*', 'activity.name'])->leftjoin('activity', 'activity.id', '=', 'orders.activity_id');
  18. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) $search_obj->where('orders.distribution_channel_id', $params['distribution_channel_id']);
  19. if (isset($params['begin_time']) && $params['begin_time']) $search_obj->where('orders.created_at', '>=', $params['begin_time']);
  20. if (isset($params['end_time']) && $params['end_time']) $search_obj->where('orders.created_at', '<=', $params['end_time'] . ' 23:59:59');
  21. if (isset($params['uid']) && $params['uid']) $search_obj->where('uid', $params['uid']);
  22. if (isset($params['inner_send_order_id']) && $params['inner_send_order_id']) $search_obj->where('inner_send_order_id', $params['inner_send_order_id']);
  23. if (isset($params['send_order_id']) && $params['send_order_id']) $search_obj->where('send_order_id', $params['send_order_id']);
  24. if (isset($params['activity_id']) && $params['activity_id']) $search_obj->where('activity_id', $params['activity_id']);
  25. if (isset($params['trade_no']) && $params['trade_no']) $search_obj->where('trade_no', $params['trade_no']);
  26. if (isset($params['transaction_id']) && $params['transaction_id']) $search_obj->where('transaction_id', $params['transaction_id']);
  27. if (isset($params['status']) && $params['status']) $search_obj->where('status', $params['status']);
  28. if (isset($params['activity_id']) && count($params['activity_id']) > 0) $search_obj->whereIn('activity_id', $params['activity_id']);
  29. if (isset($params['from_type']) && $params['from_type']) $search_obj->where('from_type', $params['from_type']);
  30. /*\Log::info('filter_:'.json_encode($params));
  31. \Log::info(json_encode($search_obj));
  32. \Log::info('my_sql:'.($search_obj->toSql()));*/
  33. // \Log::info('my_sql:'.($search_obj->toSql()));
  34. if (isset($params['created_at']) && $params['created_at']) {
  35. \Log::info('created_at--:' . date('Y-m-d 00:00:00', strtotime($params['created_at'])));
  36. $search_obj->whereBetween('orders.created_at', [date('Y-m-d 00:00:00', strtotime($params['created_at'])), date('Y-m-d 23:59:59', strtotime($params['created_at']))]);
  37. }
  38. if ($is_all) {
  39. return $search_obj->get();
  40. } else {
  41. return $search_obj->paginate();
  42. }
  43. }
  44. //查询对象
  45. static function searchObj($params = [])
  46. {
  47. $search_obj = self::orderBy('id', 'desc')->select(['orders.*', 'activity.name'])->leftjoin('activity', 'activity.id', '=', 'orders.activity_id');
  48. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) $search_obj->where('distribution_channel_id', $params['distribution_channel_id']);
  49. if (isset($params['begin_time']) && $params['begin_time']) $search_obj->where('orders.created_at', '>=', $params['begin_time']);
  50. if (isset($params['end_time']) && $params['end_time']) $search_obj->where('orders.created_at', '<=', $params['end_time'] . ' 23:59:59');
  51. if (isset($params['uid']) && $params['uid']) $search_obj->where('uid', $params['uid']);
  52. if (isset($params['send_order_id']) && $params['send_order_id']) $search_obj->where('send_order_id', $params['send_order_id']);
  53. if (isset($params['activity_id']) && $params['activity_id']) $search_obj->where('activity_id', $params['activity_id']);
  54. if (isset($params['trade_no']) && $params['trade_no']) $search_obj->where('trade_no', $params['trade_no']);
  55. if (isset($params['transaction_id']) && $params['transaction_id']) $search_obj->where('transaction_id', $params['transaction_id']);
  56. if (isset($params['status']) && $params['status']) $search_obj->where('status', $params['status']);
  57. if (isset($params['activity_id']) && count($params['activity_id']) > 0) $search_obj->whereIn('activity_id', $params['activity_id']);
  58. return $search_obj;
  59. }
  60. //总额统计
  61. static function getAmount($params = [])
  62. {
  63. $search_obj = self::where('status', 'PAID');
  64. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) $search_obj->where('distribution_channel_id', $params['distribution_channel_id']);
  65. if (isset($params['begin_time']) && $params['begin_time']) $search_obj->where('created_at', '>=', $params['begin_time']);
  66. if (isset($params['end_time']) && $params['end_time']) $search_obj->where('created_at', '<=', $params['end_time']);
  67. if (isset($params['uid']) && $params['uid']) $search_obj->where('uid', $params['uid']);
  68. if (isset($params['send_order_id']) && $params['send_order_id']) $search_obj->where('send_order_id', $params['send_order_id']);
  69. if (isset($params['activity_id']) && $params['activity_id']) $search_obj->where('activity_id', $params['activity_id']);
  70. if (isset($params['distribution_channel_id_array']) && $params['distribution_channel_id_array']) $search_obj->whereIn('distribution_channel_id', $params['distribution_channel_id_array']);
  71. if (isset($params['not_in_pay_merchant_id']) && count($params['not_in_pay_merchant_id']) > 0) {
  72. $search_obj->whereNotIn('pay_merchant_id', $params['not_in_pay_merchant_id']);
  73. } else {
  74. }
  75. return (float)$search_obj->sum('price');
  76. }
  77. static function getAmountV2($params = [])
  78. {
  79. $search_obj = self::join('pay_merchants', 'pay_merchants.id', '=', 'orders.pay_merchant_id')->where('status', 'PAID');
  80. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) $search_obj->where('orders.distribution_channel_id', $params['distribution_channel_id']);
  81. if (isset($params['begin_time']) && $params['begin_time']) $search_obj->where('orders.created_at', '>=', $params['begin_time']);
  82. if (isset($params['end_time']) && $params['end_time']) $search_obj->where('orders.created_at', '<=', $params['end_time']);
  83. if (isset($params['uid']) && $params['uid']) $search_obj->where('orders.uid', $params['uid']);
  84. if (isset($params['send_order_id']) && $params['send_order_id']) $search_obj->where('orders.send_order_id', $params['send_order_id']);
  85. if (isset($params['activity_id']) && $params['activity_id']) $search_obj->where('orders.activity_id', $params['activity_id']);
  86. if (isset($params['distribution_channel_id_array']) && $params['distribution_channel_id_array']) $search_obj->whereIn('orders.distribution_channel_id', $params['distribution_channel_id_array']);
  87. if (isset($params['not_in_pay_merchant_id']) && count($params['not_in_pay_merchant_id']) > 0) {
  88. $search_obj->whereNotIn('orders.pay_merchant_id', $params['not_in_pay_merchant_id']);
  89. }
  90. if (isset($params['is_self_channel']) && $params['is_self_channel']) $search_obj->where('pay_merchants.is_self_channel', $params['is_self_channel']);
  91. return (float)$search_obj->sum('orders.price');
  92. }
  93. //获取渠道当日数据
  94. static function getChannelToday($distribution_channel_id)
  95. {
  96. $begin_time = date('Y-m-d');
  97. $end_time = date("Y-m-d", strtotime($begin_time) + 86400);
  98. return self::select([DB::Raw("sum(price) as success_amount,count(1) as paid_number")])
  99. ->where('distribution_channel_id', $distribution_channel_id)
  100. ->where('created_at', '>=', $begin_time)
  101. ->where('created_at', '<', $end_time)
  102. ->where('status', 'PAID')
  103. ->first();
  104. }
  105. //获取渠道当日数据
  106. static function getChannelsToday($distribution_channel_ids)
  107. {
  108. $begin_time = date('Y-m-d');
  109. $end_time = date("Y-m-d", strtotime($begin_time) + 86400);
  110. return self::select([DB::Raw("sum(price) as success_amount,count(1) as paid_number")])
  111. ->whereIn('distribution_channel_id', $distribution_channel_ids)
  112. ->where('created_at', '>=', $begin_time)
  113. ->where('created_at', '<', $end_time)
  114. ->where('status', 'PAID')
  115. ->first();
  116. }
  117. //获取渠道当日数据
  118. static function getPaymerchantToday($pay_merchant_id)
  119. {
  120. $begin_time = date('Y-m-d');
  121. $end_time = date("Y-m-d", strtotime($begin_time) + 86400);
  122. return self::select([DB::Raw("sum(price) as success_amount,count(1) as paid_number")])
  123. ->whereIn('pay_merchant_id', $pay_merchant_id)
  124. ->where('created_at', '>=', $begin_time)
  125. ->where('created_at', '<', $end_time)
  126. ->where('status', 'PAID')
  127. ->first();
  128. }
  129. //获取渠道当日实时统计数据
  130. static function getChannelTodayData($distribution_channel_id)
  131. {
  132. $begin_time = date('Y-m-d');
  133. $end_time = date('Y-m-d', strtotime($begin_time) + 86400);
  134. $paid_info = self::select([DB::Raw("order_type,sum(price) as success_amount,count(1) as paid_num")])
  135. ->where('distribution_channel_id', $distribution_channel_id)
  136. ->where('created_at', '>=', $begin_time)
  137. ->where('created_at', '<', $end_time)
  138. ->where('status', 'PAID')
  139. ->groupBy('order_type')
  140. ->get()
  141. ->toArray();
  142. $unpaid_info = self::select([DB::Raw("order_type,sum(price),count(1) as unpaid_num")])
  143. ->where('distribution_channel_id', $distribution_channel_id)
  144. ->where('created_at', '>=', $begin_time)
  145. ->where('created_at', '<', $end_time)
  146. ->where('status', 'UNPAID')
  147. ->groupBy('order_type')
  148. ->get()
  149. ->toArray();
  150. $ret = [
  151. 'amount' => 0,
  152. 'paid_num' => 0,
  153. 'total_order_num' => 0,
  154. 'recharge_unpaid_number' => 0,
  155. 'recharge_paid_number' => 0,
  156. 'recharge_success_amount' => 0,
  157. 'year_unpaid_number' => 0,
  158. 'year_paid_number' => 0,
  159. 'year_success_amount' => 0
  160. ];
  161. foreach ($paid_info as $item) {
  162. $ret['amount'] += $item['success_amount'];
  163. $ret['paid_num'] += $item['paid_num'];
  164. $ret['total_order_num'] += $item['paid_num'];
  165. if ($item['order_type'] == 'RECHARGE') {
  166. $ret['recharge_paid_number'] += $item['paid_num'];
  167. $ret['recharge_success_amount'] += $item['success_amount'];
  168. }
  169. if ($item['order_type'] == 'YEAR') {
  170. $ret['year_paid_number'] += $item['paid_num'];
  171. $ret['year_success_amount'] += $item['success_amount'];
  172. }
  173. }
  174. foreach ($unpaid_info as $uitem) {
  175. $ret['total_order_num'] += $uitem['unpaid_num'];
  176. if ($uitem['order_type'] == 'RECHARGE') $ret['recharge_unpaid_number'] += $uitem['unpaid_num'];
  177. if ($uitem['order_type'] == 'YEAR') $ret['year_unpaid_number'] += $uitem['unpaid_num'];
  178. }
  179. return $ret;
  180. }
  181. //获取渠道日数据
  182. static function getChannelDateOrderInfo($distribution_channel_id, $date)
  183. {
  184. $begin_time = $date;
  185. $end_time = date("Y-m-d", strtotime($begin_time) + 86400);
  186. return self::select([DB::Raw("sum(price) as success_amount,count(1) as paid_number")])
  187. ->where('distribution_channel_id', $distribution_channel_id)
  188. ->where('created_at', '>=', $begin_time)
  189. ->where('created_at', '<', $end_time)
  190. ->where('status', 'PAID')
  191. ->first();
  192. }
  193. //获取渠道对应支付通道总额
  194. static function getPayMerchantSourceAmount($params)
  195. {
  196. $search_obj = self::select([DB::Raw("pay_merchant_source,sum(price) as success_amount")], 'pay_merchant_source')->where('status', 'PAID');
  197. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) $search_obj->where('distribution_channel_id', $params['distribution_channel_id']);
  198. if (isset($params['begin_time']) && $params['begin_time']) $search_obj->where('created_at', '>=', $params['begin_time']);
  199. if (isset($params['end_time']) && $params['end_time']) $search_obj->where('created_at', '<=', $params['end_time']);
  200. if (isset($params['not_in_pay_merchant_id']) && count($params['not_in_pay_merchant_id']) > 0) {
  201. $search_obj->whereNotIn('pay_merchant_id', $params['not_in_pay_merchant_id']);
  202. } else {
  203. }
  204. return $search_obj->groupBy('pay_merchant_source')->pluck('success_amount', 'pay_merchant_source')->toArray();
  205. }
  206. //获取付费用户数
  207. static function getPayUserNum($send_order_id)
  208. {
  209. return self::where('send_order_id', $send_order_id)->where('status', 'PAID')->distinct('uid')->count('uid');
  210. }
  211. //获取普通付费用户数
  212. static function getNormalPayUserNum($send_order_id)
  213. {
  214. return self::where('send_order_id', $send_order_id)->where('order_type', 'RECHARGE')->where('status', 'PAID')->distinct('uid')->count('uid');
  215. }
  216. //获取VIP付费用户数
  217. static function getVipPayUserNum($send_order_id)
  218. {
  219. return self::where('send_order_id', $send_order_id)->where('order_type', 'YEAR')->where('status', 'PAID')->distinct('uid')->count('uid');
  220. }
  221. //获取派单下的订单总数
  222. static function getOrderCount($send_order_id)
  223. {
  224. return self::where('send_order_id', $send_order_id)->where('status', 'PAID')->count();
  225. }
  226. //获取活动的订单总数
  227. static function getActivityOrderNum($params = [])
  228. {
  229. $search_obj = self::orderBy('created_at', 'desc');
  230. if (isset($params['status']) && $params['status']) $search_obj->where('status', $params['status']);
  231. if (isset($params['activity_id']) && $params['activity_id']) $search_obj->where('activity_id', $params['activity_id']);
  232. if (isset($params['begin_time']) && $params['begin_time']) $search_obj->where('created_at', '>=', $params['begin_time']);
  233. if (isset($params['end_time']) && $params['end_time']) $search_obj->where('created_at', '<=', $params['end_time']);
  234. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) $search_obj->where('distribution_channel_id', $params['distribution_channel_id']);
  235. return $search_obj->count();
  236. }
  237. //获取派单下的普通用户订单总数
  238. static function getNormalOrderCount($send_order_id)
  239. {
  240. return self::where('send_order_id', $send_order_id)->where('order_type', 'RECHARGE')->where('status', 'PAID')->count();
  241. }
  242. //获取派单下的VIP用户的订单总数
  243. static function getVipOrderCount($send_order_id)
  244. {
  245. return self::where('send_order_id', $send_order_id)->where('order_type', 'YEAR')->where('status', 'PAID')->count();
  246. }
  247. //获取渠道日付费用户数
  248. static function getDayPayUserNumByChannelId($distribution_channel_id, $date)
  249. {
  250. $begin_time = $date;
  251. $end_time = date("Y-m-d", strtotime($date) + 86400);
  252. return self::where('distribution_channel_id', $distribution_channel_id)
  253. ->where('created_at', '>=', $begin_time)
  254. ->where('created_at', '<', $end_time)
  255. ->where('status', 'PAID')
  256. ->distinct('uid')
  257. ->count('uid');
  258. }
  259. //获取渠道日包年vip付费用户数
  260. static function getDayVipPayUserNumByChannelId($distribution_channel_id, $date)
  261. {
  262. $begin_time = $date;
  263. $end_time = date("Y-m-d", strtotime($date) + 86400);
  264. return self::where('distribution_channel_id', $distribution_channel_id)
  265. ->where('created_at', '>=', $begin_time)
  266. ->where('created_at', '<', $end_time)
  267. ->where('status', 'PAID')
  268. ->where('order_type', 'YEAR')
  269. ->distinct('uid')
  270. ->count('uid');
  271. }
  272. //获取渠道日普通充值付费用户数
  273. static function getDayRechargePayUserNumByChannelId($distribution_channel_id, $date)
  274. {
  275. $begin_time = $date;
  276. $end_time = date("Y-m-d", strtotime($date) + 86400);
  277. return self::where('distribution_channel_id', $distribution_channel_id)
  278. ->where('created_at', '>=', $begin_time)
  279. ->where('created_at', '<', $end_time)
  280. ->where('status', 'PAID')
  281. ->where('order_type', 'RECHARGE')
  282. ->distinct('uid')
  283. ->count('uid');
  284. }
  285. //获取指定日期普通充值未支付订单数、支付订单数、总额
  286. static function getChannelDayTicketRechargeData($distribution_channel_id, $date)
  287. {
  288. $begin_time = $date;
  289. $end_time = date("Y-m-d", strtotime($date) + 86400);
  290. $paid_info = self::select([DB::Raw("sum(price) as success_amount,count(1) as paid_num")])
  291. ->where('distribution_channel_id', $distribution_channel_id)
  292. ->where('created_at', '>=', $begin_time)
  293. ->where('created_at', '<', $end_time)
  294. ->where('status', 'PAID')
  295. ->where('order_type', 'RECHARGE')
  296. ->first();
  297. $unpaid_count = self::where('distribution_channel_id', $distribution_channel_id)
  298. ->where('created_at', '>=', $begin_time)
  299. ->where('created_at', '<', $end_time)
  300. ->where('order_type', 'RECHARGE')
  301. ->where('status', 'UNPAID')
  302. ->count();
  303. return [
  304. 'ticket_recharge_paid_num' => $paid_info->paid_num,
  305. 'ticket_recharge_amount' => (float)$paid_info->success_amount,
  306. 'ticket_recharge_unpaid_num' => $unpaid_count
  307. ];
  308. }
  309. //获取指定日期包年充值未支付订单数、支付订单数、总额
  310. static function getChannelDayYearRechargeData($distribution_channel_id, $date)
  311. {
  312. $begin_time = $date;
  313. $end_time = date("Y-m-d", strtotime($date) + 86400);
  314. $paid_info = self::select([DB::Raw("sum(price) as success_amount,count(1) as paid_num")])
  315. ->where('distribution_channel_id', $distribution_channel_id)
  316. ->where('created_at', '>=', $begin_time)
  317. ->where('created_at', '<', $end_time)
  318. ->where('status', 'PAID')
  319. ->where('order_type', 'YEAR')
  320. ->first();
  321. $unpaid_count = self::where('distribution_channel_id', $distribution_channel_id)
  322. ->where('created_at', '>=', $begin_time)
  323. ->where('created_at', '<', $end_time)
  324. ->where('order_type', 'YEAR')
  325. ->where('status', 'UNPAID')
  326. ->count();
  327. return [
  328. 'year_recharge_paid_num' => $paid_info->paid_num,
  329. 'year_recharge_amount' => (float)$paid_info->success_amount,
  330. 'year_recharge_unpaid_num' => $unpaid_count
  331. ];
  332. }
  333. //获取渠道日首充数据
  334. static function getChannelDayFirstRechargeData($distribution_channel_id, $date)
  335. {
  336. $begin_time = $date;
  337. $end_time = date("Y-m-d", strtotime($date) + 86400);
  338. $first_data = DB::select("select count(distinct uid) count,sum(price) amount from orders a where distribution_channel_id = {$distribution_channel_id} and created_at>'{$begin_time}' and created_at<'{$end_time}' and status ='PAID' and not exists (select uid from orders where a.uid = uid and created_at < '{$begin_time}' and status= 'PAID' and distribution_channel_id = {$distribution_channel_id} limit 1)");
  339. return [
  340. "count" => (int)$first_data[0]->count,
  341. "amount" => (float)$first_data[0]->amount
  342. ];
  343. }
  344. //获取派单累计充值(追踪用户)
  345. static function getPromotionRegUserRechargeAmount($params = [])
  346. {
  347. $send_order_id = isset($params['send_order_id']) ? $params['send_order_id'] : 0;
  348. $end_time = isset($params['end_time']) ? $params['end_time'] : date('Y-m-d H:i:s');
  349. if ($send_order_id) {
  350. $data = DB::select("select sum(price) amount from orders where status='PAID' and created_at <= '{$end_time}' and uid in (select id from users where send_order_id = '{$send_order_id}' )");
  351. return (float)$data[0]->amount;
  352. }
  353. }
  354. static function getRechargeAmountGroupByOfficial($date)
  355. {
  356. $search_obj = self::orderBy('orders.id', 'desc')->join('force_subscribe_users', 'orders.uid', '=', 'force_subscribe_users.uid')
  357. ->join('official_accounts', 'force_subscribe_users.appid', '=', 'official_accounts.appid')->groupBy('official_accounts.appid')->groupBy('orders.distribution_channel_id');
  358. if ($date) {
  359. $search_obj->where('orders.pay_end_at', '>=', $date . ' 00:00:00');
  360. $search_obj->where('orders.pay_end_at', '<=', $date . ' 23:59:59');
  361. }
  362. $search_obj->where('orders.status', 'PAID');
  363. $data = $search_obj->select(DB::raw('sum(orders.price) as recharge_amount'), 'official_accounts.nickname', 'official_accounts.appid', 'orders.distribution_channel_id')->get()->toArray();
  364. $chanenelRechargeAmount = [];
  365. foreach ($data as $item) {
  366. $distribution_channel_id = $item['distribution_channel_id'];
  367. if (!isset($chanenelRechargeAmount[$distribution_channel_id])) {
  368. $chanenelRechargeAmount[$distribution_channel_id] = 0;
  369. }
  370. $chanenelRechargeAmount[$distribution_channel_id] += (float)$item['recharge_amount'];
  371. }
  372. foreach ($data as &$dataItem) {
  373. $dataItem['date'] = $date;
  374. $dataItem['created_at'] = date('Y-m-d H:i:s');
  375. $dataItem['updated_at'] = date('Y-m-d H:i:s');
  376. foreach ($chanenelRechargeAmount as $key => $chanenelRechargeAmountItem) {
  377. if ($dataItem['distribution_channel_id'] == $key) {
  378. $dataItem['channel_recharge_amount'] = $chanenelRechargeAmountItem;
  379. }
  380. }
  381. }
  382. return $data;
  383. }
  384. static function getRechargeAmountGroupByOfficialAccount($date)
  385. {
  386. $search_obj = self::orderBy('id', 'desc')->where('status', 'PAID')->select('uid', 'price', 'distribution_channel_id');
  387. if ($date) {
  388. $search_obj->where('created_at', '>=', $date . ' 00:00:00');
  389. $search_obj->where('created_at', '<=', $date . ' 23:59:59');
  390. }
  391. $result = $search_obj->get();
  392. $official_accounts_result = DB::table('official_accounts')->select('appid', 'nickname')->get();
  393. $temp_official_account = [];
  394. foreach ($official_accounts_result as $official_accounts_result_item) {
  395. $temp_official_account[$official_accounts_result_item->appid] = $official_accounts_result_item->nickname;
  396. }
  397. $temp_channel = [];
  398. $temp_channel_charge_amount = [];
  399. foreach ($result as $item) {
  400. if (!isset($temp_channel[$item->distribution_channel_id])) {
  401. $temp_channel[$item->distribution_channel_id] = [];
  402. }
  403. if (!isset($temp_channel_charge_amount[$item->distribution_channel_id])) {
  404. $temp_channel_charge_amount[$item->distribution_channel_id] = 0;
  405. }
  406. $force_subscribe_users_result = DB::table('force_subscribe_users')->where('uid', $item->uid)->where('is_subscribed', 1)->select('appid', 'distribution_channel_id')->first();
  407. if (!$force_subscribe_users_result) {
  408. $force_subscribe_users_result = DB::table('force_subscribe_users')->where('uid', $item->uid)->where('is_subscribed', 0)->select('appid', 'distribution_channel_id')->first();
  409. }
  410. if ($force_subscribe_users_result) {
  411. $temp_channel[$item->distribution_channel_id][$force_subscribe_users_result->appid][] = $item->price;
  412. } else {
  413. $temp_channel[$item->distribution_channel_id]['unknown'][] = $item->price;
  414. }
  415. $temp_channel_charge_amount[$item->distribution_channel_id] += $item->price;
  416. }
  417. //\Log::info($temp_channel);
  418. $finalArray = [];
  419. foreach ($temp_channel as $key => $temp_channel_item) {
  420. foreach ($temp_channel_item as $appid => $price) {
  421. $finalArray[] = [
  422. 'date' => $date,
  423. 'nickname' => isset($temp_official_account[$appid]) ? $temp_official_account[$appid] : 'unknown',
  424. 'appid' => $appid,
  425. 'distribution_channel_id' => $key,
  426. 'recharge_amount' => array_sum($price),
  427. 'channel_recharge_amount' => $temp_channel_charge_amount[$key],
  428. 'created_at' => date('Y-m-d H:i:s'),
  429. 'updated_at' => date('Y-m-d H:i:s')
  430. ];
  431. }
  432. }
  433. return $finalArray;
  434. }
  435. public static function getRechargeAmountGroupByPayMerchant($params)
  436. {
  437. $obj = self::join('pay_merchants', 'pay_merchants.id', '=', 'orders.pay_merchant_id')
  438. ->where('orders.status', '=', 'PAID');
  439. if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) {
  440. $obj->where('orders.distribution_channel_id', $params['distribution_channel_id']);
  441. }
  442. if (isset($params['is_self_channel']) && $params['is_self_channel']) {
  443. $obj->where('pay_merchants.is_self_channel', 1);
  444. }
  445. if (isset($params['start_time']) && $params['start_time']) {
  446. $obj->where('orders.created_at', '>=', $params['start_time']);
  447. }
  448. if (isset($params['end_time']) && $params['end_time']) {
  449. $obj->where('orders.created_at', '<=', $params['end_time']);
  450. }
  451. $obj->groupBy('pay_merchants.pay_company_id');
  452. return $obj->select(DB::raw('sum(orders.price) as amount,orders.distribution_channel_id,pay_company_id,pay_company_name'))->get();
  453. }
  454. public static function getPayMerchantRechargeAmount($params)
  455. {
  456. $obj = self::where('pay_merchant_id', $params['pay_merchant_id']);
  457. if (isset($params['status']) && $params['status']) {
  458. $obj->where('status', $params['status']);
  459. }
  460. if (isset($params['start_time']) && $params['start_time']) {
  461. $obj->where('created_at', '>=', $params['start_time']);
  462. }
  463. if (isset($params['end_time']) && $params['end_time']) {
  464. $obj->where('created_at', '<=', $params['end_time']);
  465. }
  466. return $obj->sum('price');
  467. }
  468. public static function IndexRaw($index_raw)
  469. {
  470. $model = new static;
  471. $model->setTable(DB::raw($model->getTable() . ' ' . $index_raw));
  472. return $model;
  473. }
  474. #region 派单按用户注册统计
  475. /**
  476. * 获取时间段内的派单号
  477. * @param $start
  478. * @param $end
  479. * @return array
  480. */
  481. public function getSendOrderIdByTime($start, $end): array
  482. {
  483. $result = self::leftjoin('users', 'users.id', 'orders.uid')
  484. ->where([
  485. ['orders.created_at', '>=', $start],
  486. ['orders.created_at', '<', $end],
  487. ['orders.status', '=', 'PAID'],
  488. ['users.send_order_id', '>', 0]
  489. ])
  490. ->select('users.send_order_id')->distinct()->get();
  491. return $result ? $result->toArray() : [];
  492. }
  493. /**
  494. * 获取时间段内派单首充数,和首充金额
  495. * @param $start
  496. * @param $end
  497. * @return array
  498. */
  499. public function getSendOrderFirstPayCountAndPriceByTime($start, $end): array
  500. {
  501. $result = self::leftjoin('users', 'users.id', 'orders.uid')
  502. ->where([
  503. ['orders.created_at', '>=', $start],
  504. ['orders.created_at', '<', $end],
  505. ['orders.status', '=', 'PAID'],
  506. ['orders.pay_type', '=', 1],
  507. ['users.send_order_id', '>', 0]
  508. ])
  509. ->select('users.send_order_id', DB::raw('count(1) first'), DB::raw('sum(orders.price) price'))
  510. ->groupby('users.send_order_id')->get();
  511. return $result ? $result->toArray() : [];
  512. }
  513. /**
  514. * 根据派单ID获取截止时间点前派单首充数,和首充金额
  515. * @param $send_order_id
  516. * @param $end
  517. * @return array
  518. */
  519. public function getSendOrderFirstPayCountAndPriceByID($send_order_id, $end): array
  520. {
  521. $result = self::join('users', 'users.id', 'orders.uid')
  522. ->where([
  523. ['users.send_order_id', '=', $send_order_id],
  524. ['orders.created_at', '<', $end],
  525. ['orders.status', '=', 'PAID'],
  526. ['orders.pay_type', '=', 1]
  527. ])
  528. ->select('users.send_order_id', DB::raw('count(1) first'), DB::raw('sum(orders.price) price'))
  529. ->groupby('users.send_order_id')->get();
  530. return $result->isEmpty() ? [] : $result->first()->toArray();
  531. }
  532. /**
  533. * 获取时间段内派单充值数和总金额
  534. * @param $start
  535. * @param $end
  536. * @return array
  537. */
  538. public function getSendOrderSuccessPayCountByTime($start, $end): array
  539. {
  540. $result = self::leftjoin('users', 'users.id', 'orders.uid')
  541. ->where([
  542. ['orders.created_at', '>=', $start],
  543. ['orders.created_at', '<', $end],
  544. ['orders.status', '=', 'PAID'],
  545. ['users.send_order_id', '>', 0]
  546. ])
  547. ->select('users.send_order_id', DB::raw('count(1) num'), DB::raw('sum(orders.price) price'))->groupby('users.send_order_id')->get();
  548. return $result ? $result->toArray() : [];
  549. }
  550. /**
  551. * 根据派单ID获取时间段内派单充值数和总金额
  552. * @param $send_order_id
  553. * @param $end
  554. * @return array
  555. */
  556. public function getSendOrderSuccessPayCountByID($send_order_id, $end): array
  557. {
  558. $result = self::join('users', 'users.id', 'orders.uid')
  559. ->where([
  560. ['orders.created_at', '<', $end],
  561. ['orders.status', '=', 'PAID'],
  562. ['users.send_order_id', '=', $send_order_id]
  563. ])
  564. ->select('users.send_order_id', DB::raw('count(1) num'), DB::raw('count(distinct(uid)) u_num'), DB::raw('sum(orders.price) price'))
  565. ->groupby('users.send_order_id')->get();
  566. return $result->isEmpty() ? [] : $result->first()->toArray();
  567. }
  568. /**
  569. * 获取时间段内派单付费人数
  570. * @param $start
  571. * @param $end
  572. * @return array
  573. */
  574. public function getSendOrderSuccessPayUserCountByTime($start, $end): array
  575. {
  576. $result = self::join('users', 'users.id', 'orders.uid')
  577. ->where([
  578. ['orders.created_at', '>=', $start],
  579. ['orders.created_at', '<', $end],
  580. ['orders.status', '=', 'PAID'],
  581. ['users.send_order_id', '>', 0]
  582. ])
  583. ->select('users.send_order_id', DB::raw('count(distinct(users.id)) num'))
  584. ->groupby(['users.send_order_id'])->get();
  585. return $result ? $result->toArray() : [];
  586. }
  587. /**
  588. * 获取时间段内派单N小时充值金额
  589. * @param $start
  590. * @param $end
  591. * @param $hour
  592. * @return array
  593. */
  594. public function getSendOrderPayPriceByHour($start, $end, $hour): array
  595. {
  596. $second = $hour * 60 * 60;
  597. $result = self::leftjoin('users', 'users.id', 'orders.uid')
  598. ->where([
  599. ['orders.created_at', '>=', $start],
  600. ['orders.created_at', '<', $end],
  601. ['orders.status', '=', 'PAID'],
  602. ['users.send_order_id', '>', 0]
  603. ])
  604. ->whereRaw('TIMESTAMPDIFF(SECOND,users.created_at,orders.created_at)<=' . $second)
  605. ->select('users.send_order_id', DB::raw('sum(orders.price) price'))
  606. ->groupby('users.send_order_id')->get();
  607. return $result ? $result->toArray() : [];
  608. }
  609. /**
  610. * 获取时间段内派单N小时充值金额
  611. * @param $send_order_id
  612. * @param $end
  613. * @param $hour
  614. * @return array
  615. */
  616. public function getSendOrderPayPriceByIdAndHour($send_order_id, $end, $hour): array
  617. {
  618. $where = [
  619. ['orders.created_at', '<', $end],
  620. ['orders.status', '=', 'PAID'],
  621. ['users.send_order_id', '=', $send_order_id]
  622. ];
  623. $second = $hour * 60 * 60;
  624. $result = self::leftjoin('users', 'users.id', 'orders.uid')
  625. ->where($where)
  626. ->whereRaw('TIMESTAMPDIFF(SECOND,users.created_at,orders.created_at)<=' . $second)
  627. ->select('users.send_order_id', DB::raw('sum(orders.price) price'))
  628. ->groupby('users.send_order_id')->get();
  629. return $result->isEmpty() ? [] : $result->first()->toArray();
  630. }
  631. /**
  632. * 获取时间段内派单N小时(首充或非首充33)用户数
  633. * @param $start
  634. * @param $end
  635. * @param $hour
  636. * @param int $first_pay_type 首充类型 -1全部 0非首充 1首充
  637. * @return array
  638. */
  639. public function getSendOrderPayUserCountByHour($start, $end, $hour, $first_pay_type = -1): array
  640. {
  641. $where = [
  642. ['orders.created_at', '>=', $start],
  643. ['orders.created_at', '<', $end],
  644. ['orders.status', '=', 'PAID'],
  645. ['users.send_order_id', '>', 0]
  646. ];
  647. if ($first_pay_type == 1) {
  648. $where = [
  649. ['orders.created_at', '>=', $start],
  650. ['orders.created_at', '<', $end],
  651. ['orders.status', '=', 'PAID'],
  652. ['users.send_order_id', '>', 0],
  653. ['orders.pay_type', '=', 1]
  654. ];
  655. } elseif ($first_pay_type == 0) {
  656. $where = [
  657. ['orders.created_at', '>=', $start],
  658. ['orders.created_at', '<', $end],
  659. ['orders.status', '=', 'PAID'],
  660. ['users.send_order_id', '>', 0],
  661. ['orders.pay_type', '>', 1]
  662. ];
  663. }
  664. $second = $hour * 60 * 60;
  665. $result = self::leftjoin('users', 'users.id', 'orders.uid')
  666. ->where($where)
  667. ->whereRaw('TIMESTAMPDIFF(SECOND,users.created_at,orders.created_at)<=' . $second)
  668. ->select('users.send_order_id', DB::raw('count(distinct(users.id)) num'))
  669. ->groupby('users.send_order_id')->get();
  670. return $result ? $result->toArray() : [];
  671. }
  672. /**
  673. * 获取时间段内派单N小时(首充或非首充33)用户数
  674. * @param $send_order_id
  675. * @param $end
  676. * @param $hour
  677. * @param int $first_pay_type 首充类型 -1全部 0非首充 1首充
  678. * @return array
  679. */
  680. public function getSendOrderPayUserCountByIdAndHour($send_order_id, $end, $hour, $first_pay_type = -1): array
  681. {
  682. $where = [
  683. ['orders.created_at', '<', $end],
  684. ['orders.status', '=', 'PAID'],
  685. ['users.send_order_id', '=', $send_order_id]
  686. ];
  687. if ($first_pay_type == 1) {
  688. $where = [
  689. ['orders.created_at', '<', $end],
  690. ['orders.status', '=', 'PAID'],
  691. ['users.send_order_id', '=', $send_order_id],
  692. ['orders.pay_type', '=', 1]
  693. ];
  694. } elseif ($first_pay_type == 0) {
  695. $where = [
  696. ['orders.created_at', '<', $end],
  697. ['orders.status', '=', 'PAID'],
  698. ['users.send_order_id', '=', $send_order_id],
  699. ['orders.pay_type', '>', 1]
  700. ];
  701. }
  702. $second = $hour * 60 * 60;
  703. $result = self::leftjoin('users', 'users.id', 'orders.uid')
  704. ->where($where)
  705. ->whereRaw('TIMESTAMPDIFF(SECOND,users.created_at,orders.created_at)<=' . $second)
  706. ->select('users.send_order_id', DB::raw('count(distinct(users.id)) num'))
  707. ->groupby('users.send_order_id')->get();
  708. return $result->isEmpty() ? [] : $result->first()->toArray();
  709. }
  710. #endregion
  711. }