UserRecharge.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/28
  5. */
  6. namespace app\admin\model\user;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\PHPExcelService;
  10. /**
  11. * 用户充值记录
  12. * Class UserRecharge
  13. * @package app\admin\model\user
  14. */
  15. class UserRecharge extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'user_recharge';
  27. use ModelTrait;
  28. public static function systemPage($where)
  29. {
  30. $model = new self;
  31. $model = $model->alias('A');
  32. if ($where['order_id'] != '') {
  33. $model = $model->whereOr('A.order_id', 'like', "%$where[order_id]%");
  34. $model = $model->whereOr('A.id', (int)$where['order_id']);
  35. $model = $model->whereOr('B.nickname', 'like', "%$where[order_id]%");
  36. }
  37. $model = $model->where('A.recharge_type', 'weixin');
  38. $model = $model->where('A.paid', 1);
  39. $model = $model->field('A.*,B.nickname');
  40. $model = $model->join('user B', 'A.uid = B.uid', 'RIGHT');
  41. $model = $model->order('A.id desc');
  42. return self::page($model, $where);
  43. }
  44. /*
  45. * 设置查询条件
  46. * @param array $where
  47. * @param object $model
  48. * */
  49. public static function setWhere($where, $model = null, $alias = '', $join = '')
  50. {
  51. $model = is_null($model) ? new self() : $model;
  52. if ($alias) {
  53. $model = $model->alias('a');
  54. $alias .= '.';
  55. }
  56. if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  57. if (isset($where['paid']) && $where['paid'] != '') $model = $model->where("{$alias}paid", $where['paid']);
  58. if (isset($where['status']) && $where['status'] != '') $model = $model->where("{$alias}paid", $where['paid']);
  59. if (isset($where['nickname']) && $where['nickname']) $model = $model->where("{$alias}uid|{$alias}order_id" . ($join ? '|' . $join : ''), 'LIKE', "%$where[nickname]%");
  60. return $model->order("{$alias}add_time desc");
  61. }
  62. /*
  63. * 查找用户充值金额记录
  64. * @param array $where
  65. * @return array
  66. *
  67. * */
  68. public static function getUserRechargeList($where)
  69. {
  70. $model = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->field(['a.*', 'u.nickname', 'u.avatar']);
  71. if (isset($where['excel']) && $where['excel']) self::saveExcel($model->select()->toArray());
  72. $data = $model->page($where['page'], $where['limit'])->select();
  73. $data = count($data) ? $data->toArray() : [];
  74. foreach ($data as &$item) {
  75. // switch ($item['recharge_type']) {
  76. // case 'routine':
  77. // $item['_recharge_type'] = '小程序充值';
  78. // break;
  79. // case 'weixin':
  80. // $item['_recharge_type'] = '公众号充值';
  81. // break;
  82. // default:
  83. // $item['_recharge_type'] = '其他充值';
  84. // break;
  85. // }
  86. $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '暂无';
  87. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  88. $item['paid_type'] = $item['paid'] == 1 ? '已支付' : ($item['paid'] == 0 ? '未支付/已驳回' : '待审核');
  89. }
  90. $count = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->count();
  91. return compact('data', 'count');
  92. }
  93. public static function saveExcel($data)
  94. {
  95. $excel = [];
  96. foreach ($data as $item) {
  97. // switch ($item['recharge_type']) {
  98. // case 'routine':
  99. // $item['_recharge_type'] = '小程序充值';
  100. // break;
  101. // case 'weixin':
  102. // $item['_recharge_type'] = '公众号充值';
  103. // break;
  104. // default:
  105. // $item['_recharge_type'] = '其他充值';
  106. // break;
  107. // }
  108. $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '暂无';
  109. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  110. // $item['paid_type'] = $item['paid'] ? '已支付' : '未支付';
  111. $item['paid_type'] = $item['paid'] == 1 ? '已支付' : ($item['paid'] == 0 ? '未支付/已驳回' : '待审核');
  112. $excel[] = [
  113. $item['nickname'],
  114. $item['price'],
  115. $item['paid_type'],
  116. $item['recharge_type'],
  117. $item['_pay_time'],
  118. // $item['paid'] == 1 && $item['refund_price'] == $item['price'] ? '已退款' : '未退款',
  119. $item['_add_time']
  120. ];
  121. }
  122. PHPExcelService::setExcelHeader(['昵称/姓名', '充值数量', '订单状态', '充值币种', '支付时间', '添加时间'])
  123. ->setExcelTile('充值记录', '充值记录' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  124. ->setExcelContent($excel)
  125. ->ExcelSave();
  126. }
  127. /*
  128. * 获取用户充值数据
  129. * @return array
  130. * */
  131. public static function getDataList($where)
  132. {
  133. return [
  134. [
  135. 'name' => '充值总金额',
  136. 'field' => '元',
  137. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  138. 'background_color' => 'layui-bg-cyan',
  139. 'col' => 3,
  140. ],
  141. [
  142. 'name' => '充值退款金额',
  143. 'field' => '元',
  144. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.refund_price'),
  145. 'background_color' => 'layui-bg-cyan',
  146. 'col' => 3,
  147. ],
  148. [
  149. 'name' => '小程序充值金额',
  150. 'field' => '元',
  151. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'routine')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  152. 'background_color' => 'layui-bg-cyan',
  153. 'col' => 3,
  154. ],
  155. [
  156. 'name' => '公众号充值金额',
  157. 'field' => '元',
  158. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'weixin')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  159. 'background_color' => 'layui-bg-cyan',
  160. 'col' => 3,
  161. ],
  162. ];
  163. }
  164. }