UserRecharge.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 ($where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  57. if ($where['paid'] != '') $model = $model->where("{$alias}paid", $where['paid']);
  58. if ($where['nickname']) $model = $model->where("{$alias}uid|{$alias}order_id" . ($join ? '|' . $join : ''), 'LIKE', "%$where[nickname]%");
  59. return $model->order("{$alias}add_time desc");
  60. }
  61. /*
  62. * 查找用户充值金额记录
  63. * @param array $where
  64. * @return array
  65. *
  66. * */
  67. public static function getUserRechargeList($where)
  68. {
  69. $model = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->field(['a.*', 'u.nickname', 'u.avatar']);
  70. if ($where['excel']) self::saveExcel($model->select()->toArray());
  71. $data = $model->page($where['page'], $where['limit'])->select();
  72. $data = count($data) ? $data->toArray() : [];
  73. foreach ($data as &$item) {
  74. switch ($item['recharge_type']) {
  75. case 'routine':
  76. $item['_recharge_type'] = '小程序充值';
  77. break;
  78. case 'weixin':
  79. $item['_recharge_type'] = '公众号充值';
  80. break;
  81. default:
  82. $item['_recharge_type'] = '其他充值';
  83. break;
  84. }
  85. $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '暂无';
  86. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  87. $item['paid_type'] = $item['paid'] ? '已支付' : '未支付';
  88. $item['type'] = $item['type'] == 1? '余额充值' : '广告值充值';
  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. $excel[] = [
  112. $item['nickname'],
  113. $item['price'],
  114. $item['paid_type'],
  115. $item['_recharge_type'],
  116. $item['_pay_time'],
  117. $item['paid'] == 1 && $item['refund_price'] == $item['price'] ? '已退款' : '未退款',
  118. $item['_add_time']
  119. ];
  120. }
  121. PHPExcelService::setExcelHeader(['昵称/姓名', '充值金额', '是否支付', '充值类型', '支付事件', '是否退款', '添加时间'])
  122. ->setExcelTile('充值记录', '充值记录' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  123. ->setExcelContent($excel)
  124. ->ExcelSave();
  125. }
  126. /*
  127. * 获取用户充值数据
  128. * @return array
  129. * */
  130. public static function getDataList($where)
  131. {
  132. return [
  133. [
  134. 'name' => '充值总金额',
  135. 'field' => '元',
  136. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  137. 'background_color' => 'layui-bg-cyan',
  138. 'col' => 3,
  139. ],
  140. [
  141. 'name' => '充值退款金额',
  142. 'field' => '元',
  143. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.refund_price'),
  144. 'background_color' => 'layui-bg-cyan',
  145. 'col' => 3,
  146. ],
  147. [
  148. 'name' => '小程序充值金额',
  149. 'field' => '元',
  150. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'routine')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  151. 'background_color' => 'layui-bg-cyan',
  152. 'col' => 3,
  153. ],
  154. [
  155. 'name' => '公众号充值金额',
  156. 'field' => '元',
  157. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'weixin')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  158. 'background_color' => 'layui-bg-cyan',
  159. 'col' => 3,
  160. ],
  161. ];
  162. }
  163. }