<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: kangkang
 * @Date: 2020-10-16 14:44:02
 * @LastEditors: “wanyongkang” “937888580@qq.com”
 * @LastEditTime: 2023-09-17 17:39:17
 */

namespace app\manager\model;

use fastphp\base\Model;
use enum\account\ProductAccount as ProductAccountEnum;

class ProductAccount extends Model
{
    protected $table = 'product_account';

    /**
     * @description: 获取使用中和已经过期额的帐号
     * @param {*}
     * @return {*}
     */
    public function getUesdAccount($where = [])
    {
        return $this->field('UserId, sum(ConnectCount) used_count')->where($where)->group(['UserId'])->fetchAll();
    }

    /**
     * @description: 获取用户帐号信息
     * @param {*}
     * @return {*}
     */
    public function getUserAccount($user_id, $index)
    {
        $product_account_enum = new ProductAccountEnum;
        $where = [];
        $wheres = [];
        switch($index){
            case 1:
                $where = ['DeleteTag' => 0, 'AccountType' => ['<>', $product_account_enum::$Test]];
            break;
            case 2:
                $where = ['DeleteTag' => 0, 'EndTime' => ['<', date('Y-m-d H:i:s')], 'AccountType' => ['<>', $product_account_enum::$Test]];
            break;
            case 3:
                $where = ['DeleteTag' => 0, 'EndTime' => ['>', date('Y-m-d H:i:s')], 'AccountType' => ['<>', $product_account_enum::$Test]];
            break;
        }
        return $this->field('ProductName,PackageName,Account,StartTime,EndTime,UpdateTime')
            ->where(['UserId' => $user_id])
            ->where($where)
            ->order('Id desc')
            ->fetchAll();
    }

    /**
     * @description: 按照排序条件进行查询
     * @param {*}
     * @return {*}
     */
    public function getSort($where = [],$order = 1, $limit = '50',$index)
    {
        $order_type = 'account_count';
        if($order == '0'){
            $order_type = 'account_count desc';
        }
        $product_account_enum = new ProductAccountEnum;
        $wheres = [];
        switch($index){
            case 1:
                $wheres = "product_account.DeleteTag=0 and AccountType<>".$product_account_enum::$Test;
            break;
            case 2:
                $wheres = "product_account.DeleteTag=0 and AccountType<>".$product_account_enum::$Test." and product_account.EndTime>'".date('Y-m-d H:i:s')."'";
            break;
            case 3:
                $wheres = "product_account.DeleteTag=0 and AccountType<>".$product_account_enum::$Test." and product_account.EndTime<='".date('Y-m-d H:i:s')."'";
            break;
        }
        return $this->field('product_account.UserId,sum(product_account.ConnectCount) account_count')
            ->join('user ON product_account.UserId=user.Id')
            ->where($where)
            ->where($wheres)
            ->group(['product_account.UserId'])
            ->order($order_type)
            ->limit($limit)
            ->fetchAll();
    }

    //获取账号总连接数
    public function getCountConnect($where1 = [], $where2 = '') {
        return $this->field('sum(ConnectCount) as sum')
            ->where($where1)
            ->where($where2)
            ->fetch();
    }
}
