DealerMoneyLog.php 3.6 KB
<?php

namespace app\api\model;

use think\Model;
use addons\shopro\exception\Exception;
use addons\shopro\model\User;
use think\Db;
use app\admin\library\Auth as AdminAuth;

/**
 * 经销商余额日志
 */
class DealerMoneyLog extends Model
{

    // 表名,不含前缀
    protected $name = 'dealer_money_log';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $hidden = ['deletetime'];


    // 追加属性
    protected $append = [
        'type_name',
    ];

    public static $typeAll = [
        // money
        'order' => ['code' => 'order', 'name' => '下单'],
        'cash' => ['code' => 'cash', 'name' => '提现'],
        'cash_error' => ['code' => 'cash_error', 'name' => '提现驳回'],
    ];

    public function scopeAdd($query)
    {
        return $query->where('money', '>', 0);
    }

    public function scopeToday($query)
    {
        return $query->where('money', '>', 0)->whereTime('createtime','today');
    }

    public function scopeSeven($query)
    {
        return $query->where('money', '>', 0)->whereTime('createtime','-7 days');
    }


    public static function doAdd($dealer, $money, $type, $item_id, $is_add = 0)
    {

        // 自动获取操作人
        if (strpos(request()->url(), 'api') !== false) {
            // 用户
            $oper = User::info();
            $oper_type = 'user';
            $oper_id = $oper ? $oper->id : $dealer['id'];
        } else {
            $adminAuth = AdminAuth::instance();     // 没有登录返回的还是这个类实例
            $oper = null;
            if ($adminAuth){
                $oper = $adminAuth->getUserInfo();
            }
            if ($oper) {
                $oper_type = 'admin';
                $oper_id = $oper['id'];
            } else {
                $oper_type = 'system';
                $oper_id = 0;
            }
        }

        $self = self::create([
            "dealer_id" => $dealer->id,
            "money" => $is_add ? $money : -$money,       // 符号直接存到记录里面
            "type" => $type,
            "item_id" => $item_id,
            "oper_type" => $oper_type,
            "oper_id" => $oper_id
        ]);

        return $self;
    }


    public static function getList($status = 'all')
    {
        $user = User::info();

        $walletLogs = new self();

        if ($status != 'all') {
            $walletLogs = $walletLogs->{$status}();
        }

        $walletLogs = $walletLogs->where(['user_id' => $user->id])
            ->order('id', 'DESC')->paginate(10);
        foreach ($walletLogs as $w) {
            $w->createtime = date('Y/m/d',$w->createtime);
            switch ($w['type']) {
                case 'package_commission':
                case 'order_commssion':
                    $oper = \addons\shopro\model\User::get($w->oper_id);
                    $w->avatar = $oper['avatar'] ?? '';
                    $w->nickname = $oper['nickname'] ?? '';
                    break;
                case 'cash':
                case 'cash_error':
                    $w->avatar = $user->avatar;
                    $w->nickname = $user->nickname;
                    break;
            }
        }
        return $walletLogs;
    }



    public static function getTypeName($type)
    {
        return isset(self::$typeAll[$type]) ? self::$typeAll[$type]['name'] : '';
    }


    public function getTypeNameAttr($value, $data)
    {
        return self::getTypeName($data['type']);
    }
}