<?php
declare (strict_types = 1);

namespace app\model\api;

use think\Model;
use library\basic\BaseModel;
use think\facade\Db;
use app\model\api\User as UserModel;

/**
 * @mixin \think\Model
 */
class UserDetail extends BaseModel
{
    private $config = [
        'admin_add'       => ["code" => "后台补加余额",     "content" => "后台补加余额:{money}。"],
        'admin_cut'       => ["code" => "后台补扣余额",     "content" => "后台补扣余额:{money}。"],
        
        
        'tx_apply'        => ['code' => "提现余额",        "content" => "您发起提现，扣除余额:{money}。"],
        'tx_refund'       => ['code' => "提现失败",        "content" => "提现失败，返还余额:{money}。"],
        'show_temp_pay'   => ['code' => "购买皮肤模板",     "content" => "您使用余额支付，扣除余额:{money}。"],
        
        
        'show_temp_buy'   => ['code' => "购买皮肤模板消费",  "content" => "{mono}，用户消费:{money}。"],
        
    ];
    
    
    
    /**
     * 余额支付
     * @param type $uid
     * @param type $money
     * @param type $mono
     * @param type $parms
     */
    public function balancePay($uid,$money,$code,$parms=[]){
        $money = bcadd("0", $money."",2);
        $post['uid']      = $uid;
        $post['into']     = floatval($money);
        $post['code']     = $code;
        $post['title']    = $this->config[$code]['code'];
        $post['content']  = $this->TplParam($this->config[$code]['content'],compact('money'));
        $post['type']     = -1;//收入为1，支出为-1
        $post['money']    = (float)(new UserModel)->where('uid', $uid)->value('money') - floatval($money);//余额
        $post['time']     = time();
        $post['tx_id']    = 0;
        $post['cash_uid'] = empty($parms["cash_uid"]) ? 0 : $parms["cash_uid"];
        $post['p_id']     = empty($parms["p_id"])     ? 0 : $parms["p_id"];
        $post['o_id']     = empty($parms["o_id"])     ? 0 : $parms["o_id"];
        $post['to_id']    = empty($parms["to_id"])    ? 0 : $parms["to_id"];//模板购买id
        $this->insert($post);
        $bool = Db::name("user")->where('uid', $uid)->dec('money', floatval($money))->update();
        if ($bool > 0) {
            return true;
        } else {
            return false;
        }
    }
    
    
    
    /**
     * 消费记录[不影响余额进出]
     * @param type $uid
     * @param type $money
     * @param type $code
     * @param type $mono
     * @return bool
     */
    public function consumeLog($uid, $money,$code,$mono=""){
        $money = bcadd((string)0, (string)$money, 2);
        $post['uid']      = $uid;
        $post['into']     = 0;//消费现金不影响余额进出
        $post['code']     = $code;
        $post['title']    = $this->config[$code]['code'];
        $post['content']  = $this->TplParam($this->config[$code]['content'],compact('money','mono'));
        $post['type']     = -1;
        $post['money']    = (float)(new UserModel)->where('uid', $uid)->value('money');
        $post['time']     = time();
        $post['consume']  = floatval($money);//消费现金金额
        $this->insert($post);
        return true;
    }
    
    /**
     * 后台充值余额
     * @param $uid
     * @param $money
     * @param $admin_id
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function adminAddMoney($uid, $money,$admin_id,$othen="")
    {
        try {
            self::beginTrans();
            $post['uid']      = $uid;
            $post['into']     = floatval($money);
            $post['code']     = 'admin_add';
            $post['title']    = $this->config['admin_add']['code'];
            $post['content']  = $this->TplParam($this->config['admin_add']['content'],compact('money'));
            $post['type']     = 1;
            $post['money']    = (float)(new UserModel)->where('uid', $uid)->value('money') + floatval($money);
            $post['time']     = time();
            $post['admin_id'] = $admin_id;
            $post['othen']    = $othen;
            $this->insert($post);
            $bool = Db::name("user")->where('uid', $uid)->inc('money', floatval($money))->update();
            self::commitTrans();
            if ($bool > 0) {
                return true;
            } else {
                return false;
            }
        } catch (DbException $db) {
            self::rollbackTrans();
            return false;
        }
    }
    /**
     * 后台扣除余额
     * @param $uid
     * @param $money
     * @param $admin_id
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function adminCutMoney($uid, $money,$admin_id,$othen="")
    {
        try {
            self::beginTrans();
            $post['uid']      = $uid;
            $post['into']     = floatval($money);
            $post['code']     = 'admin_cut';
            $post['title']    = $this->config['admin_cut']['code'];
            $post['content']  = $this->TplParam($this->config['admin_cut']['content'],compact('money'));
            $post['type']     = -1;
            $post['money']    = (float)(new UserModel)->where('uid', $uid)->value('money') - floatval($money);
            $post['time']     = time();
            $post['admin_id'] = $admin_id;
            $post['othen']    = $othen;
            $this->insert($post);
            $bool = Db::name("user")->where('uid', $uid)->dec('money', floatval($money))->update();
            self::commitTrans();
            if ($bool > 0) {
                return true;
            } else {
                return false;
            }
        } catch (DbException $db) {
            self::rollbackTrans();
            return false;
        }
    }
    /**
     * 用户提现余额
     * @param $uid
     * @param $money
     * @param $admin_id
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function txApplyMoney($uid, $money,$tx_id)
    {
        $post['uid']      = $uid;
        $post['into']     = floatval($money);
        $post['code']     = 'tx_apply';
        $post['title']    = $this->config['tx_apply']['code'];
        $post['content']  = $this->TplParam($this->config['tx_apply']['content'],compact('money'));
        $post['type']     = -1;
        $post['money']    = (float)(new UserModel)->where('uid', $uid)->value('money') - floatval($money);
        $post['time']     = time();
        $post['tx_id']    = $tx_id;
        $this->insert($post);
        $bool = Db::name("user")->where('uid', $uid)->dec('money', floatval($money))->update();
        if ($bool > 0) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 用户提现余额驳回返还
     * @param $uid
     * @param $money
     * @param $admin_id
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function txRefundMoney($uid, $money,$tx_id)
    {
        $post['uid']      = $uid;
        $post['into']     = floatval($money);
        $post['code']     = 'tx_refund';
        $post['title']    = $this->config['tx_refund']['code'];
        $post['content']  = $this->TplParam($this->config['tx_refund']['content'],compact('money'));
        $post['type']     = 1;
        $post['money']    = (float)(new UserModel)->where('uid', $uid)->value('money') + floatval($money);
        $post['time']     = time();
        $post['tx_id']    = $tx_id;
        $this->insert($post);
        $bool = Db::name("user")->where('uid', $uid)->inc('money', floatval($money))->update();
        if ($bool > 0) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 转义的模板信息
     */
    private function TplParam($content)
    {
        $data = func_get_args();
        foreach ($data[1] as $k => $v) {
            $content = str_replace('{' . $k . '}', $v, $content);
        }
        return $content;
    }
}
