<?php
declare (strict_types=1);

namespace app\model\api;

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

/**
 * @mixin \think\Model
 */
class UserScoreDetail extends BaseModel
{
    private $config = [
        'admin_score_add'      => ["code" => "后台补加积分", "content" => "后台补加积分:{money}。"],
        'admin_score_jdd'      => ["code" => "后台补扣积分", "content" => "后台补扣积分:{money}。"],
        'income_score'         => ["code" => "商城下单赠送积分", "content" => "用户收入{money}积分"],
        'outcome_score'        => ["code" => "商城购买积分抵扣支出",  "content" => "{mono},支出{money}积分"],
    ];
    
    /**
     * 后台充值积分余额
     * @param $uid
     * @param $money
     * @param $admin_id
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function adminAddMoney($uid, $money,$admin_id,$mono="")
    {
        try {
            self::beginTrans();
            $post['uid']      = $uid;
            $post['v']     = floatval($money);
            $post['code']     = 'admin_score_add';
            $post['title']    = $this->config['admin_score_add']['code'];
            $post['content']  = $this->TplParam($this->config['admin_score_add']['content'],compact('money'));
            $post['type']     = 1;
            $post['money']    = (float)(new UserModel)->where('uid', $uid)->value('score') + floatval($money);
            $post['time']     = time();
            $post['admin_id'] = $admin_id;
            $post['othen']    = $mono;
            $this->insert($post);
            $bool = Db::name("user")->where('uid', $uid)->inc('score', 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,$mono="")
    {
        try {
            self::beginTrans();
            $post['uid']      = $uid;
            $post['v']     = floatval($money);
            $post['code']     = 'admin_score_jdd';
            $post['title']    = $this->config['admin_score_jdd']['code'];
            $post['content']  = $this->TplParam($this->config['admin_score_jdd']['content'],compact('money'));
            $post['type']     = -1;
            $post['money']    = (float)(new UserModel)->where('uid', $uid)->value('score') - floatval($money);
            $post['time']     = time();
            $post['admin_id'] = $admin_id;
            $post['othen']    = $mono;
            $this->insert($post);
            $bool = Db::name("user")->where('uid', $uid)->dec('score', 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 $orderSn
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function incomeScore($uid, $money, $orderSn="",$code="income_score",$parms=array())
    {
        try {
            $content          = $this->TplParam($this->config[$code]['content'],compact('money'));
            $post['uid']      = $uid;
            $post['v']        = $money;
            $post['code']     = $code;
            $post['title']    = $this->config[$code]['code'];
            $post['content']  = $content;
            $post['type']     = 1;
            $post['time']     = time();
            $post['order_sn'] = $orderSn;
            $post['money']    = (float)(new User)->where('uid', $uid)->value('score') + $money;
            $post['o_id']    = empty($parms["o_id"])?0:$parms["o_id"];
            $this->insert($post);
            $bool = Db::name("user")->where('uid', $uid)->inc('score', floatval($money))->inc('score_in', floatval($money))->update();
            if ($bool > 0) {
                return true;
            } else {
                return false;
            }
        } catch (DbException $db) {
            return false;
        }
    }
 
    /**
     * 消耗积分
     * @param type $uid
     * @param type $money
     * @param type $mono
     */
    public function payScore($uid,$money,$code,$mono,$parms=array()){
        try {
            $content         = $this->TplParam($this->config[$code]['content'],compact('money', 'mono'));
            $post['uid']     = $uid;
            $post['v']       = $money;
            $post['code']    = $code;
            $post['title']   = $this->config[$code]['code'];
            $post['content'] = $content;
            $post['type']    = -1;
            $post['time']    = time();
            $post['money']   = (float)(new User)->where('uid', $uid)->value('score') - $money;
            $post['o_id']    = empty($parms["o_id"])?0:$parms["o_id"];
            $this->insert($post);
            $bool = Db::name("user")->where('uid', $uid)->dec('score', floatval($money))->update();
            if ($bool > 0) {
                return true;
            } else {
                return false;
            }
        } catch (DbException $db) {
            return false;
        }
    }
    /**
     * 消耗积分，从冻结账户扣除
     * @param type $uid
     * @param type $money
     * @param type $mono
     */
    public function payFreezeScore($uid,$money,$code,$mono,$parms=[]){
        $content         = $this->TplParam($this->config[$code]['content'],compact('money', 'mono'));
        $post['uid']     = $uid;
        $post['v']       = $money;
        $post['code']    = $code;
        $post['title']   = $this->config[$code]['code'];
        $post['content'] = $content;
        $post['type']    = -1;
        $post['time']    = time();
        $post['money']   = (float)(new User)->where('uid', $uid)->value('score');
        $this->insert($post);
        $bool = Db::name("user")->where('uid', $uid)->dec('score_freeze', 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;
    }

}
