<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 小程序公共API服务
 */
namespace app\bestbao\controller\api\v1;
use app\bestbao\controller\api\Base;
use app\bestbao\model\BestbaoAsk;
use app\bestbao\model\BestbaoAskCategory;
use app\bestbao\model\BestbaoAskOrder;
use app\common\facade\WechatPay;
use util\Util;

class Ask extends Base{

    
     /**
     * 获取问题
     **/
    public function category(){
        $data['signkey'] = $this->request->param('signkey');
        $data['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($data);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $rel = BestbaoAskCategory::where(['member_miniapp_id' => $this->miniapp_id])->field('title,id,picture')->order('sort desc,id desc')->select();
        if($rel->isEmpty()){
            return enjson(204);
        }else{
            return enjson(200,'成功',$rel);
        }
    }

     /**
     * 获取问题
     **/
    public function index(){
        $data['page']        = $this->request->param('page/d',1);
        $data['category_id'] = $this->request->param('category_id/d',0);
        $data['sign']        = $this->request->param('sign');
        $data['keyword']     = $this->request->param('keyword');
        $rel = $this->apiSign($data);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $condition['member_miniapp_id'] = $this->miniapp_id;
        $condition['is_ad']             = 0;
        if(empty($data['keyword']) && $data['category_id'] > 0){
            $condition['category_id'] = $data['category_id'];
        }
        $rel = BestbaoAsk::where($condition)->withSearch(['title'],['title' => $data['keyword']])->field('id,ad_img,title,note')->order('is_top desc,sort desc,id desc')->paginate(10);
        if($rel->isEmpty()){
            return enjson(204);
        }else{
            return enjson(200,'成功',$rel->toArray()['data']);
        }
    }

    /**
     * 相关问题
     **/
    public function related(){
        $data['category_id'] = $this->request->param('category_id/d',0);
        $data['sign']        = $this->request->param('sign');
        $rel = $this->apiSign($data);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $condition['member_miniapp_id'] = $this->miniapp_id;
        $condition['is_top']            = 0;
        $condition['is_ad']             = 0;
        $condition['category_id'] = $data['category_id'];
        $rel = BestbaoAsk::where($condition)->field('id,ad_img,title,note')->order('is_top desc,sort desc,id desc')->limit(10)->select();
        if($rel->isEmpty()){
            return enjson(204);
        }else{
            foreach ($rel as $key => $value) {
                $rel[$key]['note'] =Util::msubstr($value['note'],0,40);
            }
            return enjson(200,'成功',$rel);
        }
    }
    
     /**
     * 获取推荐的问题
     **/
    public function askAds(){
        $param['signkey'] = $this->request->param('signkey');
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $condition['member_miniapp_id'] = $this->miniapp_id;
        $condition['is_ad'] = 1;
        $rel = BestbaoAsk::where($condition)->order('sort desc,id desc')->limit(5)->select();
        if($rel->isEmpty()){
            return enjson(204);
        }else{
            return enjson(200,'成功',$rel);
        }
    }

     /**
     * 获取问题
     **/
    public function reView(){
        $param['id']   = $this->request->param('id/d',1);
        $param['sign'] = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $condition['member_miniapp_id'] = $this->miniapp_id;
        $condition['id']                = $param['id'];
        $rel = BestbaoAsk::where($condition)->find();
        if(empty($rel)){
            return enjson(204);
        }else{
            $data = $rel->toArray();
            $data['update_time'] = date('m/d H:i',$data['update_time']);
            $data['create_time'] = date('m/d H:i',$data['create_time']);
            $data['content']     = str_replace('<img', '<img class="img"',dehtml($rel['content']));
            $data['answer']      = str_replace('<img', '<img class="img"',dehtml($rel['answer']));
            $data['category']    = $rel->category->title;
            $data['is_buy_ask']  = 1;
            if($this->user && $rel->price > 0){
                $data['is_buy_ask'] = BestbaoAskOrder::where(['member_miniapp_id' =>$this->miniapp_id,'ask_id' =>$rel->id,'uid'=>$this->user->id,'paid_at' => 1])->count();
            }
            return enjson(200,'成功',$data);
        }
    }

    /**
     * 点赞
     **/
    public function like(){
        $this->isUserAuth();
        $param['id']   = $this->request->param('id/d',1);
        $param['sign'] = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $condition['member_miniapp_id'] = $this->miniapp_id;
        $condition['id']                = $param['id'];
        $rel = BestbaoAsk::where($condition)->setInc('like',1);
        if(empty($rel)){
            return enjson(204);
        }else{
            return enjson(200);
        }
    }
    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * 解决方案订阅
     * */
    public function doBuy(){
        $this->isUserAuth();
        $param['sign']    = $this->request->param('sign');
        $param['id']      = $this->request->param('id');
        $rel = $this->apiSign($param);
        if(!$this->user){
            return enjson(401,'请先登录');
        }
        $info = BestbaoAsk::where(['id' => $param['id']])->find();
        if(empty($info)){
            return enjson(204);
        }
        //唤醒微信支付参数
        $order_no = $this->user->invite_code.order_no(); //订单号
        $payparm = [
            'openid'     => $this->user->miniapp_uid,
            'miniapp_id' => $this->miniapp_id,
            'name'       => '订阅知识库解决方案',
            'order_no'   => $order_no,
            'total_fee'  => 1,
            'notify_url' => api(1,'bestbao/notify/ask',$this->miniapp_id),
        ];
        $ispay = WechatPay::orderPay($payparm);
        if ($ispay['code'] == 0) {
            return enjson(403,$ispay['msg']);
        }else{
            BestbaoAskOrder::create(['member_miniapp_id' => $this->miniapp_id,'ask_id' => $info->id,'uid' => $this->user->id,'amount' => $info->price,'order_no' => $order_no]);
            return enjson(200,'成功',$ispay['data']);
        }
    }
}