<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\admin\controller\bbfxshop\payroute;

use app\admin\controller\bbfxshop\Base;
use think\Db;
use app\common\model\bbfxshop\FenxiaoLevel;

class Payroute extends Base
{

    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Payroute;
    }


    public function getList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        
        $where = ["uniacid"=>UNIACID];
        
    	$list = Db::name("bbfx_payroute_pay")->where($where)->order('sort desc,id desc')->limit(($pindex - 1) * $psize,$psize)->select();
        $levels = FenxiaoLevel::getLevels(1);
      	foreach($list as $key => &$val){
      	    
            $val['levelname'] = $levels[$val['level']]['title'];

            if(empty($val['levelname'])){
                $val['levelname'] = "";
            }
            
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
        }
        unset($val);
    	$total = Db::name("bbfx_payroute_pay")->where($where)->count();

        __success("操作成功",['total'=>$total,'list'=>$list]);
    }
    
    public function detail(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $item = Db::name("bbfx_payroute_pay")->where(['id'=>$id])->find();
        if(!empty($item)){
            $pay = iunserializer($item['pay']);
        }

        if(empty($pay)){
            $pay = [
                "mch_id"=>"",
                "paykey"=>"",
                "cert"=>"",
                "certkey"=>"",
                "publickeyId"=>"",
                "publickey"=>"",
                "wxapp_mch_id"=>"",
                "wxapp_paykey"=>"",
                "wxapp_cert"=>"",
                "wxapp_certkey"=>"",
                "wxapp_publickeyId"=>"",
                "wxapp_publickey"=>"",
                "wap_AppId"=>"",
                "wap_mch_id"=>"",
                "wap_paykey"=>"",
                "wap_cert"=>"",
                "wap_certkey"=>"",
                "zfb_appid"=>"",
                "zfb_privateKey"=>"",
                "zfb_publickey_cert"=>"",
            ];
        }

        $levels = FenxiaoLevel::getLevels();
        
        if(!empty($item)){
            $item['pay'] = $pay;
            __success("操作成功",['pay'=>$pay,'info'=>$item,'levels'=>$levels]);
        }
        else{
            __error("数据不存在",['pay'=>$pay,'levels'=>$levels]);
        }
    }
    
    public function post(){
        $params = $this->param();
        
        $id = intval($params['id']);

        $item = Db::name("bbfx_payroute_pay")->where(['id'=>$id])->find();

        $pay = $params['pay'];
         
        // 小程序
        $pay['wxapp_cert'] = replaceSiteroot($pay['wxapp_cert']);
        $pay['wxapp_certkey'] = replaceSiteroot($pay['wxapp_certkey']);
        $pay['wxapp_publickey'] = replaceSiteroot($pay['wxapp_publickey']);
        // 公众号
        $pay['cert'] = replaceSiteroot($pay['cert']);
        $pay['certkey'] = replaceSiteroot($pay['certkey']);
        $pay['publickey'] = replaceSiteroot($pay['publickey']);
        // H5
        $pay['wap_cert'] = replaceSiteroot($pay['wap_cert']);
        $pay['wap_certkey'] = replaceSiteroot($pay['wap_certkey']);
        // 支付宝
        $pay['zfb_publickey_cert'] = replaceSiteroot($pay['zfb_publickey_cert']);
        
    	$data = array(
        	"uniacid"=>UNIACID,
            "title"=>$params['title'],
          	"level"=>intval($params['level']),
          	"pay"=>iserializer($pay),
          	"sort"=>intval($params['sort']),
        );

        if($data['level'] < 0){
            __error("请选择绑定等级");
        }

        $query =  Db::name("bbfx_payroute_pay")->where(["uniacid"=>UNIACID,"level"=>$data['level']])->find();
        if(!empty($query)){
            if(empty($item) || (!empty($item) && $item['id'] != $query['id'])){
                __error("该等级已被绑定");
            }
        }

      	if(!empty($item)){
            $res = Db::name("bbfx_payroute_pay")->where(["id"=>$item['id']])->update($data);
            $id = $item['id'];
        }
      	else{
          	$data['createtime'] = time();
            $res = Db::name("bbfx_payroute_pay")->insertGetId($data);
            $id = $res;
        }
        
        if($res){
            $data['id'] = $id;
            \app\admin\model\AdminLog::record("","",$data,"bbfx_payroute_pay");
            __success("编辑成功");
        }
        else{
            __success("操作成功");
        }
        
    }
    
    public function delete(){
        $this->commonDelete("bbfx_payroute_pay");
    }
}