123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\admin\model\user;
- use traits\ModelTrait;
- use basic\ModelBasic;
- use service\PhpSpreadsheetService;
- /**
- * 用户充值记录
- * Class UserRecharge
- * @package app\admin\model\user
- */
- class UserRecharge extends ModelBasic
- {
- use ModelTrait;
- public static function orderCount()
- {
- $data['wz'] = self::statusByWhere(0, new self(), '')->count();
- $data['wf'] = self::statusByWhere(1, new self(), '')->count();
- $data['tk'] = self::statusByWhere(-1, new self(), '')->count();
- $data['yt'] = self::statusByWhere(-2, new self(), '')->count();
- return $data;
- }
- public static function getBadge($where)
- {
- $price = self::getOrderPrice($where);
- return [
- [
- 'name' => '订单数量',
- 'field' => '件',
- 'count' => $price['order_sum'],
- 'background_color' => 'layui-bg-blue',
- 'col' => 3
- ],
- [
- 'name' => '订单金额',
- 'field' => '元',
- 'count' => $price['price'],
- 'background_color' => 'layui-bg-blue',
- 'col' => 3
- ],
- [
- 'name' => '退款金额',
- 'field' => '元',
- 'count' => $price['refund_price'],
- 'background_color' => 'layui-bg-blue',
- 'col' => 3
- ],
- [
- 'name' => '微信支付金额',
- 'field' => '元',
- 'count' => $price['pay_price_wx'],
- 'background_color' => 'layui-bg-blue',
- 'col' => 3
- ],
- [
- 'name' => '余额支付金额',
- 'field' => '元',
- 'count' => $price['pay_price_yue'],
- 'background_color' => 'layui-bg-blue',
- 'col' => 3
- ],
- [
- 'name' => '支付宝支付金额',
- 'field' => '元',
- 'count' => $price['pay_price_zhifubao'],
- 'background_color' => 'layui-bg-blue',
- 'col' => 3
- ]
- ];
- }
- /**
- * 处理订单金额
- * @param $where
- * @return array
- */
- public static function getOrderPrice($where)
- {
- $price = array();
- $price['pay_price'] = 0;//支付金额
- $price['refund_price'] = 0;//退款金额
- $price['pay_price_wx'] = 0;//微信支付金额
- $price['pay_price_yue'] = 0;//余额支付金额
- $price['pay_price_zhifubao'] = 0;//支付宝支付金额
- $list = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field([
- 'sum(a.price) as price',
- 'sum(a.refund_price) as refund_price'])->find()->toArray();
- $price['price'] = $list['price'];//支付金额
- $price['refund_price'] = $list['refund_price'];//退款金额
- $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();
- foreach ($list as $v) {
- if ($v['recharge_type'] == 'weixin') {
- $price['pay_price_wx'] = $v['price'];
- } elseif ($v['recharge_type'] == 'yue') {
- $price['pay_price_yue'] = $v['price'];
- } elseif ($v['recharge_type'] == 'zhifubao') {
- $price['pay_price_zhifubao'] = $v['price'];
- }
- }
- $price['order_sum'] = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
- return $price;
- }
- /**
- * 处理where条件
- * @param $where
- * @param $model
- * @return mixed
- */
- public static function getOrderWhere($where, $model, $aler = '', $join = '')
- {
- if ($where['status'] != '') {
- $model = self::statusByWhere($where['status'], $model, $aler);
- } else {
- $model = $model->where($aler . 'is_del', 0);
- }
- if ($where['real_name'] != '') {
- $model = $model->where($aler . 'order_id|' . $aler . 'uid' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
- }
- if ($where['data'] !== '') {
- $model = self::getModelTime($where, $model, $aler . 'add_time');
- }
- return $model;
- }
- public static function statusByWhere($status, $model = null, $alert = '')
- {
- if ($model == null) $model = new self;
- $model = $model->where($alert . 'is_del', 0);
- if ('' === $status)
- return $model;
- else if ($status == 0)//未支付
- return $model->where($alert . 'paid', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
- else if ($status == 1)//已支付
- return $model->where($alert . 'paid', 1)->where($alert . 'status', 0)->where($alert . 'refund_status', 0);
- else if ($status == -1)//退款中
- return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 1);
- else if ($status == -2)//已退款
- return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 2);
- else
- return $model;
- }
- public static function systemPage($where)
- {
- $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*,r.nickname,r.phone');
- if ($where['order'] != '') {
- $model = $model->order(self::setOrder($where['order']));
- } else {
- $model = $model->order('a.id desc');
- }
- if (isset($where['excel']) && $where['excel'] == 1) {
- $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
- } else {
- $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
- }
- foreach ($data as &$item) {
- $item['order_name'] = '[充值订单]';
- $item['color'] = '#895612';
- if ($item['paid'] == 1) {
- switch ($item['recharge_type']) {
- case 'weixin':
- $item['pay_type_name'] = '微信支付';
- break;
- case 'yue':
- $item['pay_type_name'] = '余额支付';
- break;
- case 'zhifubao':
- $item['pay_type_name'] = '支付宝支付';
- break;
- }
- } else {
- $item['pay_type_name'] = '未支付';
- }
- if ($item['paid'] == 0 && $item['status'] == 0) {
- $item['status_name'] = '未支付';
- } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
- $item['status_name'] = '已支付';
- } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
- $item['status_name'] = '已退款';
- }
- if ($item['paid'] == 0 && $item['status'] == 0 && $item['refund_status'] == 0) {
- $item['_status'] = 1;
- } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
- $item['_status'] = 2;
- } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
- $item['_status'] = 7;
- }
- $item['pay_time'] = $item['pay_time'] > 0 ? date('Y-m-d H:i:s', $item['pay_time']) : '无';
- $item['refund_reason_time'] = date('Y-m-d H:i:s', $item['refund_reason_time']);
- }
- if (isset($where['excel']) && $where['excel'] == 1) {
- self::SaveExcel($data);
- }
- $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
- return compact('count', 'data');
- }
- /**
- * 保存并下载excel
- * $list array
- * return
- */
- public static function SaveExcel($list)
- {
- $export = [];
- foreach ($list as $index => $item) {
- if ($item['paid'] == 0 && $item['status'] == 0) {
- $item['status_name'] = '未支付';
- } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
- $item['status_name'] = '已支付';
- } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
- $item['status_name'] = '退款中';
- } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
- $item['status_name'] = '已退款';
- }
- $export[] = [
- $item['order_id'], $item['nickname'] . '/' . $item['uid'], $item['status_name'], $item['pay_type_name'],
- $item['paid'] == 1 ? '已支付' : '未支付' . '/支付时间: ' . $item['pay_time'],
- $item['gold_num'], $item['price'], $item['refund_price']
- ];
- }
- $filename = '充值订单导出' . time() . '.xlsx';
- $head = ['订单号', '用户信息', '订单状态', '支付方式', '支付状态', '虚拟币数量', '支付金额', '退款金额'];
- PhpSpreadsheetService::outdata($filename, $export, $head);
- }
- }
|