UserRecharge.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\user;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use service\PhpSpreadsheetService;
  15. /**
  16. * 用户充值记录
  17. * Class UserRecharge
  18. * @package app\admin\model\user
  19. */
  20. class UserRecharge extends ModelBasic
  21. {
  22. use ModelTrait;
  23. public static function orderCount()
  24. {
  25. $data['wz'] = self::statusByWhere(0, new self(), '')->count();
  26. $data['wf'] = self::statusByWhere(1, new self(), '')->count();
  27. $data['tk'] = self::statusByWhere(-1, new self(), '')->count();
  28. $data['yt'] = self::statusByWhere(-2, new self(), '')->count();
  29. return $data;
  30. }
  31. public static function getBadge($where)
  32. {
  33. $price = self::getOrderPrice($where);
  34. return [
  35. [
  36. 'name' => '订单数量',
  37. 'field' => '件',
  38. 'count' => $price['order_sum'],
  39. 'background_color' => 'layui-bg-blue',
  40. 'col' => 3
  41. ],
  42. [
  43. 'name' => '订单金额',
  44. 'field' => '元',
  45. 'count' => $price['price'],
  46. 'background_color' => 'layui-bg-blue',
  47. 'col' => 3
  48. ],
  49. [
  50. 'name' => '退款金额',
  51. 'field' => '元',
  52. 'count' => $price['refund_price'],
  53. 'background_color' => 'layui-bg-blue',
  54. 'col' => 3
  55. ],
  56. [
  57. 'name' => '微信支付金额',
  58. 'field' => '元',
  59. 'count' => $price['pay_price_wx'],
  60. 'background_color' => 'layui-bg-blue',
  61. 'col' => 3
  62. ],
  63. [
  64. 'name' => '余额支付金额',
  65. 'field' => '元',
  66. 'count' => $price['pay_price_yue'],
  67. 'background_color' => 'layui-bg-blue',
  68. 'col' => 3
  69. ],
  70. [
  71. 'name' => '支付宝支付金额',
  72. 'field' => '元',
  73. 'count' => $price['pay_price_zhifubao'],
  74. 'background_color' => 'layui-bg-blue',
  75. 'col' => 3
  76. ]
  77. ];
  78. }
  79. /**
  80. * 处理订单金额
  81. * @param $where
  82. * @return array
  83. */
  84. public static function getOrderPrice($where)
  85. {
  86. $price = array();
  87. $price['pay_price'] = 0;//支付金额
  88. $price['refund_price'] = 0;//退款金额
  89. $price['pay_price_wx'] = 0;//微信支付金额
  90. $price['pay_price_yue'] = 0;//余额支付金额
  91. $price['pay_price_zhifubao'] = 0;//支付宝支付金额
  92. $list = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field([
  93. 'sum(a.price) as price',
  94. 'sum(a.refund_price) as refund_price'])->find()->toArray();
  95. $price['price'] = $list['price'];//支付金额
  96. $price['refund_price'] = $list['refund_price'];//退款金额
  97. $list = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('sum(a.price) as price,a.recharge_type')->group('a.recharge_type')->select()->toArray();
  98. foreach ($list as $v) {
  99. if ($v['recharge_type'] == 'weixin') {
  100. $price['pay_price_wx'] = $v['price'];
  101. } elseif ($v['recharge_type'] == 'yue') {
  102. $price['pay_price_yue'] = $v['price'];
  103. } elseif ($v['recharge_type'] == 'zhifubao') {
  104. $price['pay_price_zhifubao'] = $v['price'];
  105. }
  106. }
  107. $price['order_sum'] = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  108. return $price;
  109. }
  110. /**
  111. * 处理where条件
  112. * @param $where
  113. * @param $model
  114. * @return mixed
  115. */
  116. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  117. {
  118. if ($where['status'] != '') {
  119. $model = self::statusByWhere($where['status'], $model, $aler);
  120. } else {
  121. $model = $model->where($aler . 'is_del', 0);
  122. }
  123. if ($where['real_name'] != '') {
  124. $model = $model->where($aler . 'order_id|' . $aler . 'uid' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  125. }
  126. if ($where['data'] !== '') {
  127. $model = self::getModelTime($where, $model, $aler . 'add_time');
  128. }
  129. return $model;
  130. }
  131. public static function statusByWhere($status, $model = null, $alert = '')
  132. {
  133. if ($model == null) $model = new self;
  134. $model = $model->where($alert . 'is_del', 0);
  135. if ('' === $status)
  136. return $model;
  137. else if ($status == 0)//未支付
  138. return $model->where($alert . 'paid', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  139. else if ($status == 1)//已支付
  140. return $model->where($alert . 'paid', 1)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
  141. else if ($status == -1)//退款中
  142. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 1);
  143. else if ($status == -2)//已退款
  144. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 2);
  145. else
  146. return $model;
  147. }
  148. public static function systemPage($where)
  149. {
  150. $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*,r.nickname,r.phone');
  151. if ($where['order'] != '') {
  152. $model = $model->order(self::setOrder($where['order']));
  153. } else {
  154. $model = $model->order('a.id desc');
  155. }
  156. if (isset($where['excel']) && $where['excel'] == 1) {
  157. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  158. } else {
  159. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  160. }
  161. foreach ($data as &$item) {
  162. $item['order_name'] = '[充值订单]';
  163. $item['color'] = '#895612';
  164. if ($item['paid'] == 1) {
  165. switch ($item['recharge_type']) {
  166. case 'weixin':
  167. $item['pay_type_name'] = '微信支付';
  168. break;
  169. case 'yue':
  170. $item['pay_type_name'] = '余额支付';
  171. break;
  172. case 'zhifubao':
  173. $item['pay_type_name'] = '支付宝支付';
  174. break;
  175. }
  176. } else {
  177. $item['pay_type_name'] = '未支付';
  178. }
  179. if ($item['paid'] == 0 && $item['status'] == 0) {
  180. $item['status_name'] = '未支付';
  181. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  182. $item['status_name'] = '已支付';
  183. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  184. $item['status_name'] = '已退款';
  185. }
  186. if ($item['paid'] == 0 && $item['status'] == 0 && $item['refund_status'] == 0) {
  187. $item['_status'] = 1;
  188. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  189. $item['_status'] = 2;
  190. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  191. $item['_status'] = 7;
  192. }
  193. $item['pay_time'] = $item['pay_time'] > 0 ? date('Y-m-d H:i:s', $item['pay_time']) : '无';
  194. $item['refund_reason_time'] = date('Y-m-d H:i:s', $item['refund_reason_time']);
  195. }
  196. if (isset($where['excel']) && $where['excel'] == 1) {
  197. self::SaveExcel($data);
  198. }
  199. $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  200. return compact('count', 'data');
  201. }
  202. /**
  203. * 保存并下载excel
  204. * $list array
  205. * return
  206. */
  207. public static function SaveExcel($list)
  208. {
  209. $export = [];
  210. foreach ($list as $index => $item) {
  211. if ($item['paid'] == 0 && $item['status'] == 0) {
  212. $item['status_name'] = '未支付';
  213. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  214. $item['status_name'] = '已支付';
  215. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  216. $item['status_name'] = '退款中';
  217. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  218. $item['status_name'] = '已退款';
  219. }
  220. $export[] = [
  221. $item['order_id'], $item['nickname'] . '/' . $item['uid'], $item['status_name'], $item['pay_type_name'],
  222. $item['paid'] == 1 ? '已支付' : '未支付' . '/支付时间: ' . $item['pay_time'],
  223. $item['gold_num'], $item['price'], $item['refund_price']
  224. ];
  225. }
  226. $filename = '充值订单导出' . time() . '.xlsx';
  227. $head = ['订单号', '用户信息', '订单状态', '支付方式', '支付状态', '虚拟币数量', '支付金额', '退款金额'];
  228. PhpSpreadsheetService::outdata($filename, $export, $head);
  229. }
  230. }