<?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 SiteDetail extends BaseModel
{


    private $config = [
        'commission'      => array('code' => "分站佣金", "content" => "{user}用户采购，订单号:{order_id}，订单单数:{count},可获取佣金{money}元（其中产品佣金:{pro_money}元,快递佣金:{exp_money}元）。"),
        'difference'      => array('code'=>'分站差价','content'=>'平台{sitename}，{user}用户采购，订单单数:{count},可获取快递佣金:{money}元'),
        'tx'              => array("code" => "分站提现", "content" => "你在{time}进行提现操作，提现金额:{money}元。"),
    ];


    /**
     * 佣金推广
     * @param $money
     * @param $uid
     * @param $param
     * @param $sassid 平台ID
     */
    public function commission($money,$sassid,$order_id,$param){
        try {
            self::beginTrans();
            $content         = $this->TplParam($this->config['commission']['content'], $param);
            $post['v']       = $money;
            $post['code']    = 'commission';
            $post['order_id'] = $order_id;
            $post['title']   = $this->config['commission']['code'];
            $post['content'] = $content;
            $post['type']    = 1;
            $post['sassid']  = $sassid;
            $post['time']    = time();
            $post['money']   = (new Site)->where('sassid', $sassid)->value('money') + $money;
            $this->insert($post);
            $bool = Db::name("site")->where('sassid',$sassid)->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 $uid
     * @param $param
     * @param $sassid 平台ID
     */
    public function difference($money,$sassid,$order_id,$param){
        try {
            self::beginTrans();
            $content         = $this->TplParam($this->config['difference']['content'], $param);
            $post['v']       = $money;
            $post['code']    = 'difference';
            $post['order_id'] = $order_id;
            $post['title']   = $this->config['difference']['code'];
            $post['content'] = $content;
            $post['type']    = 1;
            $post['sassid']  = $sassid;
            $post['time']    = time();
            $post['money']   = (new Site)->where('sassid', $sassid)->value('money') + $money;
            $this->insert($post);
            $bool = Db::name("site")->where('sassid',$sassid)->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 $uid
     * @param $param
     * @param $sassid 平台ID
     */
    public function tx($money,$sassid,$param = []){
        try {
            self::beginTrans();
            $content         = $this->TplParam($this->config['tx']['content'], $param);
            $post['v']       = $money;
            $post['code']    = 'tx';
            $post['order_id'] = '';
            $post['title']   = $this->config['tx']['code'];
            $post['content'] = $content;
            $post['type']    = 2;
            $post['sassid']  = $sassid;
            $post['time']    = time();
            $post['money']   = (new Site)->where('sassid', $sassid)->value('money') - $money;
            $this->insert($post);
            //echo $this->getLastSql();
            $bool = Db::name("site")->where('sassid',$sassid)->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;
        }
    }


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