UserRecharge.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. }
  89. $count = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->count();
  90. return compact('data', 'count');
  91. }
  92. public static function saveExcel($data)
  93. {
  94. $excel = [];
  95. foreach ($data as $item) {
  96. switch ($item['recharge_type']) {
  97. case 'routine':
  98. $item['_recharge_type'] = '小程序充值';
  99. break;
  100. case 'weixin':
  101. $item['_recharge_type'] = '公众号充值';
  102. break;
  103. default:
  104. $item['_recharge_type'] = '其他充值';
  105. break;
  106. }
  107. $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '暂无';
  108. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  109. $item['paid_type'] = $item['paid'] ? '已支付' : '未支付';
  110. $excel[] = [
  111. $item['nickname'],
  112. $item['price'],
  113. $item['paid_type'],
  114. $item['_recharge_type'],
  115. $item['_pay_time'],
  116. $item['paid'] == 1 && $item['refund_price'] == $item['price'] ? '已退款' : '未退款',
  117. $item['_add_time']
  118. ];
  119. }
  120. PHPExcelService::setExcelHeader(['昵称/姓名', '充值金额', '是否支付', '充值类型', '支付事件', '是否退款', '添加时间'])
  121. ->setExcelTile('充值记录', '充值记录' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  122. ->setExcelContent($excel)
  123. ->ExcelSave();
  124. }
  125. /*
  126. * 获取用户充值数据
  127. * @return array
  128. * */
  129. public static function getDataList($where)
  130. {
  131. return [
  132. [
  133. 'name' => '充值总金额',
  134. 'field' => '元',
  135. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  136. 'background_color' => 'layui-bg-cyan',
  137. 'col' => 3,
  138. ],
  139. [
  140. 'name' => '充值退款金额',
  141. 'field' => '元',
  142. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.refund_price'),
  143. 'background_color' => 'layui-bg-cyan',
  144. 'col' => 3,
  145. ],
  146. [
  147. 'name' => '小程序充值金额',
  148. 'field' => '元',
  149. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'routine')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  150. 'background_color' => 'layui-bg-cyan',
  151. 'col' => 3,
  152. ],
  153. [
  154. 'name' => '公众号充值金额',
  155. 'field' => '元',
  156. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'weixin')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  157. 'background_color' => 'layui-bg-cyan',
  158. 'col' => 3,
  159. ],
  160. ];
  161. }
  162. public static function getStoreRechargeList($where)
  163. {
  164. $model = self::where('paid', 1);
  165. if (isset($where['store_id']) && $where['store_id'] != '') {
  166. $model = $model->where('store_id', $where['store_id']);
  167. }
  168. $sum = $model->sum('price');
  169. $count = $model->count();
  170. $list = $model->page((int)$where['page'], (int)$where['limit'])->order('pay_time', 'desc')->select();
  171. return compact('sum', 'count', 'list');
  172. }
  173. }