<?php
/**
 *------Create thems Model------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-05 09:03:48------
 */
namespace App\Commons\Model;
use App\Commons\Controller\Validation;
use Illuminate\Support\Facades\DB;
use \System\Model;

class Account_detailed extends Model {

    protected $table = 'account_detailed';
    public $timestamps = false;

    public function updateLoginStatus($identity) {
        $account_detailed = [
            'last_time' => date('Y-m-d H:i:s'),
            'last_ip' => GETIP(),
            'statuss' => 2,
        ];
        $this->where("account_identity", $identity)->update($account_detailed);
    }
     public function getAccountInfos($ids){
        $data=$this->select('register_time','cash','account_identity')->whereIn('account_identity',$ids)->get();
        if(!$data){
            return -1020020022;
        }
        return $data->toArray();
    }


    //理财更新方法、扣除理财金额度
    protected function UpdateFinancecash($params=array()){
        /*$params = array(
            'account_identity' => 'dbf53485-fb80-8d1b-a6bf-2d173fc0033e',
            'account_name' => 'qwer1234',
            'available_cash' => 1002200,
            'money' => 100,
        );*/
        if (!isset($params['account_identity']) || !isset($params['available_cash'])) {
            return -4008;
        }
        
        if (!isset($params['money']) || $params['money'] < 1 || !Validation::isFloat ($params['money'])) {
            return -2115;
        }

        $params['money'] = floor ($params['money'] * 100) / 100;
        //获取充值金额
        $order_id = OrderID ();
        
        $rtype = 12;
        
        $omoney = new \Biz\Money\MoneyHandle;
        $checkCash = $omoney->checkWater($params['account_identity'], $params['available_cash']);
        if($checkCash<0){
            return $checkCash;
        }
        
        //更新用户余额、插入资金流水
        $remarks = sprintf(lang()->get('user finance money'), $params['account_name'],'扣除', $params['money'], $order_id);
        $pstr = "*" . $params['account_identity'] . "*,*" . $params['money'] . "*,*2*,*" . $rtype . "*,*" . $remarks . "*,*11*";
        $str = "select update_finance_ids('".$pstr."')";

        //$mdin = lm('Money_details', 'commons')->select($str);
        $mdin = S('DB')->select($str);
        $mdin = json_encode($mdin);
        $mdin = json_decode($mdin,1);
        if(!$mdin || empty($mdin)){
            return -2100;
        }
        return 1;
    }

    //理财更新方法、增加理财金
    protected function backFinance($params=array()){
        /*$params = array(
            'account_identity' => 'dbf53485-fb80-8d1b-a6bf-2d173fc0033e',
            'account_name' => 'qwer1234',
            'available_cash' => 1002200,
            'money' => 100,
        );*/
        if (!isset($params['account_identity']) || !isset($params['available_cash'])) {
            return -4008;
        }
        
        if (!isset($params['money']) || $params['money'] < 1 || !Validation::isFloat ($params['money'])) {
            return -2115;
        }

        $params['money'] = floor ($params['money'] * 100) / 100;
        //获取充值金额
        $order_id = OrderID ();
        
        $rtype = 12;
        
        $omoney = new \Biz\Money\MoneyHandle;
        $checkCash = $omoney->checkWater($params['account_identity'], $params['available_cash']);
        if($checkCash<0){
            return $checkCash;
        }
        
        //更新用户余额、插入资金流水
        $remarks = sprintf(lang()->get('user finance money'), $params['account_name'],'增加', $params['money'], $order_id);
        $pstr = "*" . $params['account_identity'] . "*,*" . $params['money'] . "*,*1*,*" . $rtype . "*,*" . $remarks . "*,*12*";
        $str = "select update_finance_ids('".$pstr."')";

        //$mdin = lm('Account_detailed', 'commons')->select($str);
        $mdin = S('DB')->select($str);
        $mdin = json_encode($mdin);
        $mdin = json_decode($mdin,1);
        if(!$mdin || empty($mdin)){
            return -2100;
        }
        return 1;
    }

    public function getUsernum($where=array()){
        return $this->where($where)->count();
    }

    /**
     * 查询用户详情
     *
     * @access public
     * @param mixed $select 查询字段
     * @param mixed $where 查询条件
     * @return array JsonString
     */
    public function accountDetailed($select, $where) {
        $result = $this
            -> select($select)
            -> where($where)
            -> first();

        return $result;
    }

    /**
     * 修改用户详情
     *
     * @access public
     * @param mixed $where 查询条件
     * @param mixed $updateData 修改字段
     * @return array JsonString
     */
    public function updateDetailed($where, $updateData) {
        $result = $this
            -> where($where)
            -> update($updateData);

        return $result;
    }


}
