<?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\citys\controller\api\v1;
use app\citys\controller\api\Base;
use app\citys\model\Citys;
use app\citys\model\CitysReply;
use app\citys\model\CitysFollow;
use app\citys\model\CitysMp;
use app\citys\model\CitysCate;
use app\common\model\SystemUser;
use think\facade\Config;
use util\Util;

class Info extends Base{

   /**
     * 获取单个信息
     * @param integer 读取ID
     * @return json
     */
    public function getview(int $id){
        $param['id'] = $id;
        $this->apiSign($param);
        $user = $this->user;
        $rel = Citys::with([
            'user' => function($query) {
                $query->field('id,nickname,face');
            },
            'cate' => function($query) {
                $query->field('id,tpl_id,price,title');
            },
            'types' => function($query) {
                $query->field('id,title,price,endtips');
            },
            'mp'   => function($query) {
                $query->field('id,uid,logo,title,is_vip');
            },
            'showphone' => function($query) use($user){
                $query->field('paid_at,info_id')->where(['uid' => $user->id ?? 0,'paid_at' => 1]);
            }
        ])->where(['id'=> $id])->find();
        if(empty($rel)){
            return enjson(404);
        }
        $rel->views = $rel->views+rand(10,200);
        $rel->save();
        //模板信息
        if(empty($rel->cate->tpl)){
            $rel->button_name = '立即下单';
            $rel->tips        = '任何有用户发布,下单后请电话再次确认';
            $rel->is_shop     = 0;
            $rel->users       = [];
        }else{
            $rel->button_name = $rel->cate->tpl->button_name;
            $rel->tips        = $rel->cate->tpl->tips;
            $rel->is_shop     = $rel->cate->tpl->is_shop;
            $rel->users       = json_decode($rel->cate->tpl->users,true);
        }
        $rel->fields      = $rel->fields;
        $rel->create_time = Util::ftime($rel->create_time);
        $rel->is_mp       = empty($rel->mp)?0:1;
        //是否点赞
        $rel->is_like     = 0; 
        $rel->like_face   = $rel->like_face;
        //判断是否被关注
        $rel->is_follow   = !empty($rel->follow) && !empty($this->user) && $rel->follow->uid = $this->user->id ? 1:0;  
        $rel->showphone    = empty($rel->showphone) ? 0 : 1;
        return enjson(200,'成功',$rel);
    }

   /**
     * 获取信息评论
     * @param integer 读取ID
     * @return json
     */
    public function reply(){
        $param['page'] = $this->request->param('page/d');
        $param['id']   = $this->request->param('id/d');
        $this->apiSign($param);
        $where['info_id'] = $param['id'];
        $where['state']   = 0;
        $rel = CitysReply::with([
            'user' => function($query) {
                $query->field('id,nickname,face');
            },
        ])->where($where)->order('id desc')->paginate(10);
        $data = $rel->toArray()['data'];
        array_walk($data,function(&$item,$key){
            $item['is_like'] = 0;
        });
        if(empty($data)){
            return enjson(204);
        }else{
            return enjson(200,'成功',$data);
        }
    }    

    /**
     * 读取城市号信息
     */
    public function mp(){
        $param['uid'] = $this->request->param('uid/d',0);
        $param['mid'] = $this->request->param('mid/d',0);
        $this->apiSign($param);
        $where = [];
        if($param['mid']){
            $where['id']  = $param['mid'];
        }else{
            $where['uid'] = $param['uid'];
        }
        $rel = CitysMp::where($where)->find();
        if($rel){
            $rel->views = Citys::where(['uid' => $rel->uid])->count();
            $rel->like  = CitysFollow::where(['uid' => $rel->uid])->count();
            $rel->is_follow =  0;
            $rel->vip_time = empty($rel->vip_time)?'0':date('Y-m-d',$rel->vip_time);
            $rel->reg_time = empty($rel->reg_time)?'0':date('Y-m-d',$rel->reg_time);
            $rel->is_reg   = 1;
            if($this->user){
                $rel->is_follow = CitysFollow::where(['uid' => $this->user->id,'like_uid'=>$rel->uid])->count();
            }
        }else{
            if($param['uid']){
                $user = SystemUser::where(['id' => $param['uid']])->find();
                if(empty($user)){
                    return enjson(404,'用户被锁定或未找到');
                }
                $rel['logo']      = $user->face;
                $rel['title']     = $user->nickname;
                $rel['note']      = '未开通城市号';
                $rel['views']     = Citys::where(['uid' => $param['uid']])->count();
                $rel['like']      = CitysFollow::where(['uid'     => $param['uid']])->count();
                $rel['fans']      = 0;
                $rel['is_follow'] = 0;
                $rel['is_apply']  = 0;
                $rel['reg_time']  = 0;
                $rel['is_vip']    = 0;
                $rel['vip_time']  = 0;
                $rel['vip_title'] = '未认证';
                $rel['vip_about'] = '未开通城市号';
                $rel['is_reg']    = 0;
                $rel['skin']      = Config::get('upload.upload_relative').'static/citys/skin/2.jpg';
            }
        }
        return enjson(200,$rel);
    }

    /**
     * 读取城市号的信息
     * @param integer 读取ID
     * @return json
     */
    public function me(){
        $this->isUserAuth();
        $param['page']  = $this->request->param('page/d',1);
        $param['uid']   = $this->request->param('uid/d',0);
        $param['mid']   = $this->request->param('mid/d',0);
        $this->apiSign($param);
        $where = [];
        $where['member_miniapp_id'] = $this->miniapp_id;
        if($param['uid'] > 0){
            if($param['uid'] ==$this->user->id){
                $where['uid'] = $this->user->id;
            }else{
                $where['uid']     = $param['uid'];
                $where['is_lock'] = 0;
            }
        }
        $data = Citys::apiLists($where,"is_top desc,id desc",$this->user);
        return enjson(empty($data)?204:200,$data);
    }
}
