<?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\allwin\controller\api\v4;
use app\allwin\controller\api\Base;
use app\allwin\model\AllwinInfoCate;
use app\allwin\model\AllwinInfo;
use app\allwin\model\AllwinInfoFollow;
use app\allwin\model\AllwinInfoMp;
use app\allwin\model\AllwinInfoConfig;
use app\allwin\model\AllwinInfoReply;
use app\allwin\model\AllwinStore;
use app\common\model\SystemUser;
use think\facade\Config;
use util\Util;

class Info extends Base{

    /**
     * 获取配置参数
     * @return void
     */
    public function getconfig(){
        $param['signkey'] = $this->request->param('signkey');
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $result = AllwinInfoConfig::config($this->miniapp_id);
        if(empty($result)){
            return json(['code'=>204,'msg'=>'失败']);
        }else{
            $data = json_decode($result->topday,true);
            foreach ($data as $key => $value) {
                $data[$key]['money'] = intval($value['money']/100);
            }
            return json(['data'=>$data,'code'=>200,'msg'=>'成功']);
        }        
    }

    /**
     * 读取信息列表
     * @param integer 读取ID
     * @return json
     */
    public function index(){
        $param['page']    = $this->request->param('page/d');
        $param['keyword'] = $this->request->param('keyword/s','');
        $param['cate_id'] = $this->request->param('cate_id/d',0);
        $param['state']   = $this->request->param('state/d',1);
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $order = "is_top desc,sort desc,id desc";
        $where = [];
        $where['member_miniapp_id'] = $this->miniapp_id;
        $where['is_lock']           = 0;
        if($param['cate_id']){
            $where['cate_id']       = $param['cate_id'];
        }else{
            if(empty($param['keyword'])){
                if(!$param['state']){  //关注
                    //读取我的关注UID    
                    $this->isUserAuth();   //我的关注要登录
                    $uid = AllwinInfoFollow::where(['uid' => $this->user->id])->column('like_uid');
                    if(empty($uid)){
                        return enjson(204,'没有内容');
                    }else{
                        $where['user_id'] = $uid;
                    }
                }else{ //热门
                    $order = "is_top desc,sort desc,like desc,id desc";
                }
            }
        }
        $data = AllwinInfo::apiLists($where,$order,$this->user,$param['keyword']);
        if(empty($data)){
            return enjson(204,'没有内容');
        }else{
            return enjson(200,'成功',$data);
        }
    }

   /**
     * 获取信息评论
     * @param integer 读取ID
     * @return json
     */
    public function reply(){
        $param['signkey'] = $this->request->param('signkey');
        $param['sign']    = $this->request->param('sign');
        $param['page']    = $this->request->param('page/d');
        $param['id']      = $this->request->param('id/d');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }   
        $where['info_id'] = $param['id'];
        $where['state']   = 0;
        $rel = AllwinInfoReply::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);
        }
    }    

    /**
     * 读取微信API
     * @param integer 读取ID
     * @return json
     */
    public function cate(){
        $param['signkey']   = $this->request->param('signkey');
        $param['sign']      = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $where = [];
        $where['member_miniapp_id'] = $this->miniapp_id;
        $data = AllwinInfoCate::where($where)->field('id,picture,name')->order('sort desc,id desc')->select()->toArray();
        return enjson(200,'成功',$data);
    }

   /**
     * 获取单个信息
     * @param integer 读取ID
     * @return json
     */
    public function getview(int $info_id){
        $param['info_id'] = $this->request->param('info_id/d',0);
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $rel = AllwinInfo::with([
            'user' => function($query) {
                $query->field('id,nickname,face');
            },
            'cate' => function($query) {
                $query->field('id,tpl_id,title');
            },
            'mp'   => function($query) {
                $query->field('id,uid,logo,title,is_vip');
            }
        ])->where(['id'=> $info_id])->find();
        if(empty($rel)){
            return enjson(404);
        }
        //模板信息
        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);
        }
        $images             = empty($rel->images) ? [] : json_decode($rel->images,true);
        foreach ($images as $i => $img) {
            $images[$i] = $img.'?x-oss-process=style/300';
        }
        $rel['images']      = $images;
        $rel['images_len']  = count($images);
        $rel['fields']      = empty($rel->fields) ? [] : json_decode($rel->fields,true);
        $rel['create_time'] = util::ftime($rel->create_time);
        $rel['is_mp']       = empty($rel->mp)?0:1;
        //是否点赞
        $rel['is_like']     = 0; 
        $rel['like_face']   = empty($rel->like_face) ? [] : json_decode($rel->like_face,true);
        //判断是否被关注
        $rel['is_follow']   = !empty($rel->follow) && !empty($this->user) && $rel->follow->uid = $this->user->id ? 1:0;  
        return enjson(200,'成功',$rel);
    }


    /**
     * 注册城市号
     */
    public function mp(){
        $param['uid']     = $this->request->param('uid/d',0);
        $param['mid']     = $this->request->param('mid/d',0);
        $param['signkey'] = $this->request->param('signkey/s');
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $where = [];
        if($param['mid']){
            $where['id']  = $param['mid'];
        }else{
            $where['uid'] = $param['uid'];
        }
        $rel = AllwinInfoMp::where($where)->find();
        if($rel){
            $rel->views = AllwinInfo::where(['user_id' => $rel->uid])->count();
            $rel->like  = AllwinInfoFollow::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);
            if($this->user){
                $rel->is_follow = AllwinInfoFollow::where(['uid' => $this->user->id,'like_uid'=>$rel->uid])->count();
            }
            //读取好店
            $condition['manage_uid']        = $rel->uid;
            $condition['member_miniapp_id'] = $this->miniapp_id;
            $condition['is_lock']           = 0;
            $info = AllwinStore::getView($condition);
            if(empty($info)){
                $rel->is_store = 0;
                $rel->store    = [];
            }else{
                $rel->is_store = 1;
                $info['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$info['longitude'],$info['latitude']);
                $chain = [];
                foreach ($info['chains'] as $key => $value) {
                    $chain[$key]['title']    = $value['title'];
                    $chain[$key]['address']  = $value['address'];
                    $chain[$key]['telphone'] = $value['telphone'];
                    $chain[$key]['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$value->longitude,$value->latitude);
                }
                $info['chain'] = $chain;
                unset($info['chains']);
                $rel->store  = $info;
            }
        }else{
            if($param['uid']){
                $rel['views']     = AllwinInfo      ::where(['user_id' => $param['uid']])->count();
                $rel['like']      = AllwinInfoFollow::where(['uid'     => $param['uid']])->count();
                $rel['fans']      = 0;
                $rel['is_follow'] = 0;
                $rel['vip_time']  = 0;
                $rel['reg_time']  = 0;
                $user = SystemUser::where(['id' => $param['uid']])->find();
                $rel['face']      = $user->face;
                $rel['nickname']  = $user->nickname; 
                $rel['note']      = '未开通城市号';
                $rel['skin']      = Config::get('upload.upload_relative').'static/allwin/skin/2.jpg';
            }
        }
        return enjson(200,'成功',$rel);
    }


    /**
     * 读取信息列表
     * @param integer 读取ID
     * @return json
     */
    public function me(){
        $param['page']  = $this->request->param('page/d',1);
        $param['state'] = $this->request->param('state/d',1);
        $param['uid']   = $this->request->param('uid/d',0);
        $param['mid']   = $this->request->param('mid/d',0);
        $param['sign']  = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $where = [];
        $where['member_miniapp_id'] = $this->miniapp_id;
        $where['is_lock']           = 0;
        if($param['state']){
            $order = "is_top desc,id desc";
            $where['user_id'] = $param['uid'];
        }else{ //热门
            $uid = AllwinInfoFollow::where(['uid' => $param['uid']])->column('like_uid');
            $where['user_id'] = $uid;
            $order = "views desc,sort desc,id desc";
        }
        $data = AllwinInfo::apiLists($where,$order,$this->user);
        if(empty($data)){
            return enjson(204,'没有内容');
        }else{
            return enjson(200,'成功',$data);
        }
    }
}
