<?php


namespace App\Models\demo\mysql;


use Api\PhpUtils\Mysql\MysqlBase2;

class TestMysql extends MysqlBase2
{
    const TABLE_NAME = 'test';
    const CONFIG_INDEX = 'merchant';

    const LIFE_ACCOUNT_TYPE_PERSIONAL       = 1;    // 个人
    const LIFE_ACCOUNT_TYPE_ENTERPRISE      = 2;    // 企业

    const LIFE_ACCOUNT_STATUS_ON    = 1;            // 上线
    const LIFE_ACCOUNT_STATUS_OFF   = 2;            // 下线
    const LIFE_ACCOUNT_STATUS_UPGRADE = 3;          // 升级中

    const LIFE_ACCOUNT_AUTH_STATUS_NO   = 1;        // 未认证生活号
    const LIFE_ACCOUNT_AUTH_STATUS_YES  = 2;        // 已认证生活号

    const ENTERPRISE_AUTH_STATUS_ING    = 1;        // 升级审核中
    const ENTERPRISE_AUTH_STATUS_FAIL   = 2;        // 升级失败


    public static function getRecord($where, $colums = [])
    {
        if (empty($colums)) {
            $colums = '*';
        }
        return self::get($colums, $where);
    }
    public static function getRecordMaster($where, $colums = [])
    {
        if (empty($colums)) {
            $colums = '*';
        }
        return self::selectMaster($colums, $where);
    }

    public static function insertRecord($colums)
    {
        return self::insert($colums);
    }

    public static function updateRecord($colums, $where)
    {
        return self::update($colums, $where);
    }

    public static function save($data, $where = [])
    {
        if (empty($where)) {
            return self::insert($data);
        }
        return self::update($data, $where);
    }

    public static function deleteRecord($where)
    {
        return self::delete($where);
    }


    public static function getAccountList($data, $colums = [])
    {
       $colums = " * ";
       $where = " 1=1 ";
       if (!empty($data['life_account_name'])) {
           $where .=  " and (`life_account_name` LIKE '%{$data['life_account_name']}%') ";
       }

       if (!empty($data['life_account_type'])) {
          $where .=  " and life_account_type = '{$data['life_account_type']}'";
       }

       if (!empty($data['ORDER'])) {
            $where .=  " ORDER BY `create_time` DESC ";
       }

       if (!empty($data['LIMIT'])) {
            $where .=  " LIMIT {$data['LIMIT'][1]} OFFSET {$data['LIMIT'][0]} ";
       }
       return $data = self::query("SELECT {$colums} FROM `life_account` WHERE $where ")->fetchAll();
    }

    public static function getAccountCount($data, $colums = [])
    {
        $colums = " * ";
        $where = " 1=1 ";
        if (!empty($data['life_account_name'])) {
           $where .=  " and (`life_account_name` LIKE '%{$data['life_account_name']}%') ";
        }

        if (!empty($data['life_account_type'])) {
            $where .=  " and life_account_type = '{$data['life_account_type']}'";
        }

        $data = self::query("SELECT COUNT(*) as count FROM `life_account` WHERE $where ")->fetch();
        return !empty($data['count']) ? $data['count'] : 0;
    }

    public static function getRecords($where, $colums = [])
    {
        if (empty($colums)) {
            $colums = '*';
        }
        return self::select($colums, $where);
    }

    public static function getCount($where)
    {
        return self::count($where);
    }

    public static function duplicate($data, $duplicate)
    {
        return self::insertDuplicate($data, $duplicate);
    }
    
    public static function forupdate($columns, $where)
    {
        return self::selectForUpdate($columns, $where);
    }
}
