162 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			162 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
<?php
 | 
						|
 | 
						|
namespace app\model;
 | 
						|
 | 
						|
use think\model\relation\HasOne;
 | 
						|
 | 
						|
/**
 | 
						|
 * 积分与佣金日志
 | 
						|
 *
 | 
						|
 * Class AccountDataLog
 | 
						|
 * @package app\model
 | 
						|
 */
 | 
						|
class AccountDataLog extends Base
 | 
						|
{
 | 
						|
    public const TYPE_SCORE      = 'score';//积分
 | 
						|
    public const TYPE_COMMISSION = 'commission';//佣金
 | 
						|
 | 
						|
    public const ACTION_ORDER             = 'order';//订单返佣
 | 
						|
    public const ACTION_ADMIN_RECHARGE    = 'admin_recharge';//后台充值
 | 
						|
    public const ACTION_ADMIN_OPERATION   = 'admin_operation';//后台操作
 | 
						|
    public const ACTION_WITHDRAWAL_RETURN = 'withdrawal_return';//提现-退回
 | 
						|
    public const ACTION_WITHDRAWAL        = 'withdrawal';//提现-扣除
 | 
						|
 | 
						|
    // 积分独有
 | 
						|
    public const ACTION_SHARE_REG         = 'share_reg';//分享注册
 | 
						|
    public const ACTION_SHARE_REG_CHILD   = 'share_reg_child';//分享注册-下级分享时获得积分
 | 
						|
    public const ACTION_SHARE_REG_SERVICE = 'share_reg_service';//分享注册-客户分享时客服获得积分
 | 
						|
    public const ACTION_SIGN              = 'sign';//签到
 | 
						|
 | 
						|
    /**
 | 
						|
     * 记录变更
 | 
						|
     *
 | 
						|
     * @param  int  $accountId
 | 
						|
     * @param  string  $name  日志名称(说明) 如 签到打卡、任务完成等等
 | 
						|
     * @param  string  $num  数量
 | 
						|
     * @param  string  $type  类型 目前仅 TYPE_SCORE TYPE_COMMISSION
 | 
						|
     * @param  string  $action  操作 如打卡=sign 任务=task 订单=order
 | 
						|
     * @param  string  $surplus  剩余积分或佣金
 | 
						|
     * @param  string  $operator  操作人
 | 
						|
     * @param  int  $operatorId  操作人ID
 | 
						|
     * @param  string  $remarks  备注
 | 
						|
     */
 | 
						|
    public static function log(int $accountId, string $name, string $num, string $type, string $action, string $surplus, string $operator = '', int $operatorId = 0, string $remarks = '')
 | 
						|
    {
 | 
						|
        self::create([
 | 
						|
            'account_id'  => $accountId,
 | 
						|
            'name'        => $name,
 | 
						|
            'num'         => $num,
 | 
						|
            'type'        => $type,
 | 
						|
            'action'      => $action,
 | 
						|
            'created_at'  => date('Y-m-d H:i:s'),
 | 
						|
            'surplus'     => $surplus,
 | 
						|
            'operator'    => $operator,
 | 
						|
            'operator_id' => $operatorId,
 | 
						|
            'remarks'     => $remarks,
 | 
						|
        ]);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     *  用户
 | 
						|
     *
 | 
						|
     */
 | 
						|
    public function account(): HasOne
 | 
						|
    {
 | 
						|
        return $this->hasOne(Account::class, 'id', 'account_id');
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     *  来源用户
 | 
						|
     *
 | 
						|
     */
 | 
						|
    public function sourceAccount(): HasOne
 | 
						|
    {
 | 
						|
        return $this->hasOne(Account::class, 'id', 'source_account_id');
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 积分操作类型
 | 
						|
     *
 | 
						|
     * @return string[]
 | 
						|
     */
 | 
						|
    public static function scoreAction(): array
 | 
						|
    {
 | 
						|
        return [
 | 
						|
            self::ACTION_ORDER             => '订单',
 | 
						|
            self::ACTION_ADMIN_RECHARGE    => '后台充值',
 | 
						|
            self::ACTION_ADMIN_OPERATION   => '后台操作',
 | 
						|
            self::ACTION_SHARE_REG         => '分享注册',
 | 
						|
            self::ACTION_SHARE_REG_CHILD   => '分享注册-下级分享',
 | 
						|
            self::ACTION_SHARE_REG_SERVICE => '分享注册-客服获得积分',
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 佣金操作类型
 | 
						|
     *
 | 
						|
     * @return string[]
 | 
						|
     */
 | 
						|
    public static function commissionAction(): array
 | 
						|
    {
 | 
						|
        return [
 | 
						|
            self::ACTION_ORDER             => '订单返佣',
 | 
						|
            self::ACTION_ADMIN_RECHARGE    => '后台充值',
 | 
						|
            self::ACTION_ADMIN_OPERATION   => '后台操作',
 | 
						|
            self::ACTION_WITHDRAWAL_RETURN => '提现退回',
 | 
						|
            self::ACTION_WITHDRAWAL        => '提现扣除',
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    //
 | 
						|
 | 
						|
    /**
 | 
						|
     * 分销明显 根据身份返回不同
 | 
						|
     *
 | 
						|
     * @param  int  $accountId
 | 
						|
     * @param  int  $position  身份 0=普通用户 1=一级代理 2=二级代理
 | 
						|
     * @param  array  $options
 | 
						|
     * @return array
 | 
						|
     * @throws \Exception
 | 
						|
     */
 | 
						|
    public static function salesList(int $accountId, int $position, array $options = []): array
 | 
						|
    {
 | 
						|
        $page   = $options['page'] ?? 1;
 | 
						|
        $size   = $options['size'] ?? 20;
 | 
						|
        $action = $options['action'] ?? '';//查询具体某类操作
 | 
						|
 | 
						|
        $where   = [];
 | 
						|
        $where[] = ['account_id', '=', $accountId];
 | 
						|
        $where[] = ['num', '>', 0];
 | 
						|
 | 
						|
        if (!empty($action)) {
 | 
						|
            $where[] = ['action', '=', $action];
 | 
						|
        }
 | 
						|
 | 
						|
        if ($position > 0) {
 | 
						|
            $where[] = ['type', '=', self::TYPE_COMMISSION];
 | 
						|
        } else {
 | 
						|
            $where[] = ['type', '=', self::TYPE_SCORE];
 | 
						|
            $where[] = ['action', '=', self::ACTION_ORDER];
 | 
						|
        }
 | 
						|
 | 
						|
        $field = ['id', 'account_id', 'source_account_id', 'name', 'num', 'created_at', 'type'];
 | 
						|
        $list  = self::findList($where, $field, $page, $size, function ($q) {
 | 
						|
            return $q->order('created_at', 'desc')->order('id', 'desc')->with([
 | 
						|
                'sourceAccount' => function ($query) {
 | 
						|
                    $query->field('id,nickname,headimgurl');
 | 
						|
                }
 | 
						|
            ]);
 | 
						|
        });
 | 
						|
 | 
						|
        $list['list']->each(function ($item) {
 | 
						|
            $item->account = $item->sourceAccount ? json_decode($item->sourceAccount, true) : [];
 | 
						|
            if ($item['type'] == self::TYPE_SCORE) {
 | 
						|
                $item->num = (int) $item->num;
 | 
						|
            }
 | 
						|
            unset($item->sourceAccount);
 | 
						|
        });
 | 
						|
 | 
						|
        return $list;
 | 
						|
    }
 | 
						|
}
 |