<?php
/**
*------Create thems Model------
*------SCWPHP  Version 1.0.0------
*------Dev Model Jions------
*------Create Time 2017-06-28 15:23:54------
*/
namespace App\Commons\Model;
use Illuminate\Support\Facades\DB;
use \System\Model;
class Money_recharge extends Model {

public $timestamps = false;
protected $table = 'money_recharge';
    function countRimitUser($timearea) {
        $data = $this->select('account_identity')->whereBetween('apply_time', $timearea)->where('recharge_type', '汇款')->groupBy('account_identity')->get();
        if (!$data) {
            return 0;
        }
        $data = $data->toArray();
        return count($data);
    }


    //充值方法
    protected function AddRecharge($params=array()){
        /*$params = array(
            'account_identity' => 'dbf53485-fb80-8d1b-a6bf-2d173fc0033e',
            'account_name' => 'qwer1234',
            'available_cash' => 1002200,
            'money' => 100,
            'name' => 'wxe',
            'type' => '2',
            'status' => 2,
            'rate' => '',
        );*/
        if (!isset($params['account_identity']) || !isset($params['available_cash'])) {
            return -4008;
        }
        
        if (!isset($params['money']) || $params['money'] < 1 || !is_numeric($params['money'])) {
            return -2115;
        }
        $name = isset($params['name']) ? ($params['name']) : '';

        $name = strip_tags (trim ($name));
        $params['money'] = floor ($params['money'] * 100) / 100;
        //获取充值金额
        $order_id = OrderID ();
        
        $rtype = 6;
        $rtypename = $this->RtypeName($rtype);
        $paytypes = $rtypename;  
        $paytype = $this->RmethodName($params['type']);
        
        _beginTransaction();
        $omoney = new \Biz\Money\MoneyHandle;
        $checkCash = $omoney->checkWater($params['account_identity'], $params['available_cash']);
        if($checkCash<0){
            return $checkCash;
        }
        $allmoney = $params['available_cash'] + $params['money'];
        
        //插入充值记录信息
        $remark = isset($params['remark']) ? strip_tags ($params['remark']) : '';
        $mrrin = $this->addMreRemark($params['name'],$order_id,$params['money'],$remark,date('Y-m-d H:i:s'));
        if($mrrin<0){
            _rollBack();
            return $mrrin;
        }
        $rate = isset($params['rate'])?$params['rate']:'';
        $status = isset($params['status'])?$params['status']:1;
        //插入充值信息
        $mrin = $this->addMoney($params,$order_id,$params['money'],$rtypename,$allmoney,$paytype,$status,$rate);
        if($mrin<0){
            _rollBack();
            return $mrin;
        }

        //更新用户余额、插入资金流水
        $remarks = sprintf(lang()->get('user charge money'), $params['account_name'], $params['money'], $order_id);
        $pstr = "*" . $params['account_identity'] . "*,*" . $params['money'] . "*,*1*,*" . $rtype . "*,*" . $remarks . "*,*" . $order_id . "*";
        $str = "select update_money_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)){
            _rollBack();
            return -2100;
        }
        _commit();
        return 1;
    }

    //插入充值记录信息
    protected function addMreRemark($name,$order_id,$money,$remark,$time){
        $rechargeRecord = lm ('money_recharge_remark', 'commons')->select ('id')->where ('name', $name)->where ('money', $money)->where ('recharge_time', $time)->first ();
        if ($rechargeRecord) {
            return -2050;
        }
        $rechareDate = array(
            'name' => $name,
            'order_id' => $order_id,
            'money' => $money,
            'remark' => $remark,
            'recharge_time' => $time,
        );
        $dataRow = lm ('money_recharge_remark', 'commons')->insert ($rechareDate);
        if ($dataRow > 0) {
           return 1;
        } else {
           return -2100;
        }
    }

    //插入充值信息
    protected function addMoney($uinfo,$order_id,$money,$rtypename,$allmoney,$paytype,$status,$rate){
        $rechareInfo = array(
            'info_identity' => UUID (),
            'account_identity' => $uinfo['account_identity'],
            'account_name' => $uinfo['account_name'],
            'order_id' => $order_id,
            'money' => $money,
            'status' => $status,
            'recharge_type' => $rtypename,
            'money_cash' => $allmoney,
            'apply_time' => date ('Y-m-d H:i:s', time ()),
            'remark' => $paytype,
            'rate' => $rate,
        );
        $res = lm ('money_recharge', 'commons')->insert ($rechareInfo);
        if ($res > 0) {
            return 1;
        } else {
            return -2100;
        }
    }

    //获取充值类型名
    protected function RtypeName($key){
        $arr = array(
            '6' => '在线充值',
            '11' => '后台加款',
            '12' => '活动',
            '13' => '充值赠送',
            '14' => '汇款',
        );
        return $arr[$key];
    }

    //获取充值方式名
    protected function RmethodName($key){
        $arr = [
            2 => '微信',
            3 => '支付宝',
            4 => 'QQ',
            5 => '银联',
            6 => '京东',
            7 => '多合一'
        ];
        return $arr[$key];
    }

    /**
     * 充值记录
     *
     * @access public
     * @param mixed $rechargeRecordSelect 查询条件字段
     * @param mixed $rechargeRecordWhere 查询条件
     * @param mixed $rechargeRecordBetween 查询条件区间
     * @param mixed $begin 起始查询位置
     * @param mixed $pageSize 分页大小
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function rechargeRecord($rechargeRecordSelect, $rechargeRecordWhere, $rechargeRecordBetween, $begin, $pageSize, $orderBy = 'apply_time') {
        $result = $this
            -> select($rechargeRecordSelect)
            -> where($rechargeRecordWhere)
            -> whereBetween('apply_time', $rechargeRecordBetween)
            -> offset($begin)
            -> limit($pageSize)
            -> orderBy($orderBy)
            -> get();

        return $result;
    }

    /**
     * 充值记录总数
     *
     * @access public
     * @param mixed $rechargeRecordWhere 查询条件
     * @param mixed $rechargeRecordBetween 查询条件区间
     * @return array JsonString
     */
    public function rechargeRecordTotal($rechargeRecordWhere, $rechargeRecordBetween) {
        $result = $this
            -> where($rechargeRecordWhere)
            -> whereBetween('apply_time', $rechargeRecordBetween)
            -> count();

        return $result;
    }
}
