<?php
declare (strict_types = 1);

namespace app\model\warehouse;

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

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


    private $config = [
        'refund'          => array("code" => "快递退款", "content" => "订单号:{order_id}，退款操作，扣除金额{money}元。"),
        'income'          => array('code' => "快递结算", "content" => "在{time}，总订单量:{count}单，结算金额{money}元。"),
        'warehouse_add'   => array('code' => "后台转账", "content" => "在{time}，后台向你转账{money}元,其他说明:{mono}。"),
        'warehouse_jdd'   => array('code' => "后台扣款", "content" => "在{time}，后台向你扣款{money}元,其他说明:{mono}。"),
        'tx'              => array('code' => "账号提现", "content" => "在{time}，你发起来提现操作！"),
    ];



    /**
     * 退款金额
     * @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,$warehouse_id,$param){
        try {
            self::beginTrans();
            //$param['time'] = date('Y-m-d H:i:s');
            $param['money'] = $money;
            $content         = $this->TplParam($this->config['income']['content'], $param);
            $post['warehouse_id']     = $warehouse_id;
            $post['v']       = $money;
            $post['code']    = 'income';
            $post['title']   = $this->config['income']['code'];
            $post['content'] = $content;
            $post['type']    = 1;
            $post['time']    = time();
            $post['money']   = (new \app\model\warehouse\Warehouse)->where('id', $warehouse_id)->value('money') + $money;
            $this->insert($post);
            $bool = Db::name("warehouse")->where('id',$warehouse_id)->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 $type
     * @param $param
     * @param $sassid
     * @return bool
     */
    public function adminInc($money,$warehouse_id,$param,$type){
        try {
            self::beginTrans();
            $param['time'] = date('Y-m-d H:i:s');
            $param['money'] = $money;
            $content         = $this->TplParam($this->config[$type == 'add' ? 'warehouse_add' : 'warehouse_jdd']['content'], $param);
            $post['warehouse_id']     = $warehouse_id;
            $post['v']       = $money;
            $post['code']    = 'admin_tz';
            $post['title']   =  $this->config[ $type == 'add' ? 'warehouse_add' : 'warehouse_jdd' ]['code'];
            $post['content'] = $content;
            $post['type']    = $type == 'add' ? 1 : 2;
            $post['time']    = time();
            if($type == 'add')
                $post['money']   = (new \app\model\warehouse\Warehouse)->where('id', $warehouse_id)->value('money') + $money;
            else
                $post['money']   = (new \app\model\warehouse\Warehouse)->where('id', $warehouse_id)->value('money') - $money;
            $this->insert($post);
            if($type == 'add')
                $bool = Db::name("warehouse")->where('id',$warehouse_id)->inc('money',floatval($money))->update();
            else
                $bool = Db::name("warehouse")->where('id',$warehouse_id)->dec('money',floatval($money))->update();
            self::commitTrans();
            if($bool > 0) {
                return true;
            } else {
                return false;
            }
        }catch (DbException $db){
            self::rollbackTrans();
            return false;
        }
    }


    /**
     * 提现
     * @param $money
     * @param $warehouse_id
     * @param $param
     * @param $type
     * @return bool
     */
    public function tx($money,$warehouse_id,$param){
        try {
            self::beginTrans();
            $param['time'] = date('Y-m-d H:i:s');
            $param['money'] = $money;
            $content         = $this->TplParam($this->config['tx']['content'], $param);
            $post['warehouse_id']     = $warehouse_id;
            $post['v']       = $money;
            $post['code']    = 'tx';
            $post['title']   =  $this->config['tx']['code'];
            $post['content'] = $content;
            $post['type']    = 2;
            $post['time']    = time();
            $post['money']   = (new \app\model\warehouse\Warehouse)->where('id', $warehouse_id)->value('money') - $money;
            $this->insert($post);
            $bool = Db::name("warehouse")->where('id',$warehouse_id)->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;
    }
}
