<?php
declare (strict_types = 1);

namespace app\model\api;

use library\basic\BaseModel;
use think\db\exception\DbException;
use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class MemberDetail extends BaseModel
{


    private $config = [
        'consumption' => array("code" => "消费余额", "content" => "你在{name}进行余额支付，订单号:{order_id}，订单总金额:{allmoney}元，消耗:{money}"),
        'recharge'    => array("code" => "在线充值", "content" => "你在{time}进行充值操作，充值金额:{money}元。"),
        'refund'      => array("code" => "退款操作", "content" => "订单{order_id}进行退款，订单单数:{count}笔，共{money}元"),
        'income'      => array('code' => "推广佣金", "content" => "推广{user}用户采购礼品，订单号:{order_id}，订单单数:{count},可获取佣金{money}数"),
        'admin_add'   => array('code' => "后台转账", "content" => "在{time}，后台向你转账{money}元,其他说明:{mono}。"),
        'admin_jdd'   => array('code' => "后台扣款", "content" => "在{time}，后台向你扣款{money}元,其他说明:{mono}。"),
    ];


    /**
     * 消费余额
     * @param $money
     * @param $uid
     * @param $param
     * @param $sassid 平台ID
     */
    public function consumption($money,$uid,$param,$sassid){
        try {
            self::beginTrans();
            $content         = $this->TplParam($this->config['consumption']['content'], $param);
            $post['uid']     = $uid;
            $post['v']       = $money;
            $post['code']    = 'consumption';
            $post['title']   = $this->config['consumption']['code'];
            $post['content'] = $content;
            $post['type']    = 2;
            $post['sassid']  = $sassid;
            $post['time']    = time();
            $post['money']   = (new Member)->where('uid', $uid)->value('money') - $money;
            $this->insert($post);
            $bool = Db::name("member")->where('uid',$uid)->dec('money',floatval($money))->update();
            self::commitTrans();
            if($bool > 0) {
                return true;
            } else {
                return false;
            }
        }catch (DbException $db){
          //  var_dump($db->getMessage());
            self::rollbackTrans();
            return false;
        }
    }




    /**
     * 充值余额
     * @param $money
     * @param $contentId
     * @param $uid
     * @param $param
     */
    public function recharge($money,$uid,$param = [],$sassid){
        try {
            self::beginTrans();
            $param['time'] = date('Y-m-d H:i:s');
            $param['money'] = $money;
            $content         = $this->TplParam($this->config['recharge']['content'], $param);
            $post['uid']     = $uid;
            $post['v']       = $money;
            $post['code']    = 'recharge';
            $post['title']   = $this->config['recharge']['code'];
            $post['content'] = $content;
            $post['type']    = 1;
            $post['sassid']  = $sassid;
            $post['time']    = time();
            $post['money']   = (new Member)->where('uid', $uid)->value('money') + $money;
            $this->insert($post);
            $bool = Db::name("member")->where('uid',$uid)->inc('money',floatval($money))->update();
            self::commitTrans();
            if($bool > 0) {
                return true;
            } else {
                return false;
            }
        }catch (DbException $db){
            //  var_dump($db->getMessage());
            self::rollbackTrans();
            return false;
        }
    }


    /**
     * 退款金额
     * @param $money
     * @param $contentId
     * @param $uid
     * @param $param
     */
    public function refund($money,$uid,$param = [],$sassid){
        try {
            self::beginTrans();
            $param['time'] = date('Y-m-d H:i:s');
            $param['money'] = $money;
            $content         = $this->TplParam($this->config['refund']['content'], $param);
            $post['uid']     = $uid;
            $post['v']       = $money;
            $post['code']    = 'refund';
            $post['title']   = $this->config['refund']['code'];
            $post['content'] = $content;
            $post['type']    = 1;
            $post['sassid']  = $sassid;
            $post['time']    = time();
            $post['money']   = (new Member)->where('uid', $uid)->value('money') + $money;
            $this->insert($post);
            $bool = Db::name("member")->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 $money
     * @param $contentId
     * @param $uid
     * @param $param
     */
    public function income($money,$uid,$param,$sassid){
        try {
            self::beginTrans();
            $param['time'] = date('Y-m-d H:i:s');
            $param['money'] = $money;
            $content         = $this->TplParam($this->config['income']['content'], $param);
            $post['uid']     = $uid;
            $post['v']       = $money;
            $post['code']    = 'income';
            $post['title']   = $this->config['income']['code'];
            $post['content'] = $content;
            $post['type']    = 1;
            $post['sassid']  = $sassid;
            $post['time']    = time();
            $post['money']   = (new Member)->where('uid', $uid)->value('tx_money') + $money;
            $this->insert($post);
            $bool = Db::name("member")->where('uid',$uid)->inc('tx_money',floatval($money))->update();
            self::commitTrans();
            if($bool > 0) {
                return true;
            } else {
                return false;
            }
        }catch (DbException $db){
            self::rollbackTrans();
            return false;
        }
    }


    /**
     * 减少数据
     * @param $money
     * @param $uid
     * @param $type
     * @param $param
     * @param $sassid
     * @return bool
     */
    public function adminInc($money,$uid,$param,$sassid,$type){
        try {
            self::beginTrans();
            $param['time'] = date('Y-m-d H:i:s');
            $param['money'] = $money;
            $content         = $this->TplParam($this->config[$type == 'add' ? 'admin_add' : 'admin_jdd']['content'], $param);
            $post['uid']     = $uid;
            $post['v']       = $money;
            $post['code']    = 'admin_tz';
            $post['title']   =  $this->config[ $type == 'add' ? 'admin_add' : 'admin_jdd' ]['code'];
            $post['content'] = $content;
            $post['type']    = $type == 'add' ? 1 : 2;
            $post['sassid']  = $sassid;
            $post['time']    = time();
            if($type == 'add')
                $post['money']   = (new Member)->where('uid', $uid)->value('money') + $money;
            else
                $post['money']   = (new Member)->where('uid', $uid)->value('money') - $money;
            $this->insert($post);
            if($type == 'add')
                $bool = Db::name("member")->where('uid',$uid)->inc('money',floatval($money))->update();
            else
                $bool = Db::name("member")->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;
        }
    }


    /**
     * 转义的模板信息
     */
   private function TplParam($content) {
        $data = func_get_args();
        foreach ($data[1] as $k => $v) {
            $content = str_replace('{' . $k . '}', $v, $content);
        }
        return $content;
    }
}
