<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2023/7/3
 * Time: 14:04
 */
namespace app\api\controller;

use app\admin\model\resource\Collect;
use app\admin\model\resource\Ethnography;
use app\api\model\Addr;
use app\common\controller\Api;
use \app\admin\model\order\Order as OrderItem;
use app\lib\model\FamilyModel;
use app\lib\model\SurnameModel;
use think\Db;


class Article extends Api{


    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $collect = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->collect = new Collect();
    }

    /**
     * 发表文章
     */
    public function createArticle()
    {
        $title = xss_clean($this->request->post('title',''));
        $family_id = $this->request->post('family_id','');
        $image_url = $this->request->post('image_url','');
        $content = xss_clean($this->request->post('content',''));
        $upload_name = $this->request->post('upload_name','');

        if(!$title || !$content || !$image_url){
            $this->error('缩略图、标题、内容不能为空');
        }

        $uid = $this->auth->id;
        $people = Db::name('family_people')
            ->alias('p')
            ->join('family_tree t','t.people_id = p.id','left')
            ->where(['p.user_id' =>$uid])
            ->field('p.*,t.family_id')
            ->find();
        if(!$people){
            $this->error('查无此人');
        }
        $Ethnography = new Ethnography();
        $Ethnography->title = $title;
        $Ethnography->family_id = $people['family_id'];
        $Ethnography->image_url = $image_url;
        $Ethnography->content = $content;
        $Ethnography->upload_name = $people['name'];
        $Ethnography->createtime = time();
        $Ethnography->status = 1;//隐藏,待审核
        $Ethnography->user_id = $uid;
        $Ethnography->save();
        $this->success('发布成功');
    }

    /**
     * 文章列表
     */
    public function getArticleList()
    {
        $page = $this->request->post('page',1);
        $keywords = $this->request->post('keywords','');
        $action = $this->request->post('action','');//center:个人中心自己发表的文章
        $type = $this->request->post('type',1);//1:推荐 2:同姓 3:同城  4:同县  5:同村
        $province_id = $this->request->post('province_id',0);
        $city_id = $this->request->post('city_id',0);
        $area_id = $this->request->post('area_id',0);
        $town_id = $this->request->post('town_id',0);
        $village_id = $this->request->post('village_id',0);

        $where['ea.status'] = 0;
        $uid = $this->auth->id;
        $people = Db::name('family_people')->where(['user_id' =>$uid])->find();
        if($action == 'center'){
            $where = ['ea.user_id' =>$uid];
        }
        if(!$this->auth->province_id){
            $addr_arr = [
                'province_id' =>$province_id,
                'city_id' =>$city_id,
                'area_id' =>$area_id,
                'town_id' =>$town_id,
                'village_id' =>$village_id,
            ];
            \app\common\model\User::update($addr_arr,['id' =>$this->auth->id]);
        }
       /* if(!$people){
            $this->error('查无此人');
        }*/
        if($keywords){
            $where['ea.title'] = ['like',"%$keywords%"];
        }
        switch ($type){
            case 2:
                $surname_info = SurnameModel::dataInfo(['Surname' =>$people['surname']]);
                if($surname_info){
                    $ids = FamilyModel::where(['surname_id' =>$surname_info['id']])->column('id');
                    $where['ea.family_id'] = ['in',$ids];
                }
                break;
            case 3:
                $city_id = Addr::where(['ID' =>$area_id])->value('ParentId');
                $where['ea.city_id'] = $this->auth->city_id;
                break;
            case 4:
                $where['ea.area_id'] = $this->auth->area_id;
                break;
            case 5:
                $where['ea.village_id'] = $this->auth->village_id;
                break;
        }

        $list = Ethnography::alias('ea')
            ->join('family_people p','p.user_id = ea.user_id','left')
            ->where($where)
            ->field("ea.*,p.name,p.photo,p.address,FROM_UNIXTIME(ea.createtime,'%Y-%m-%d %T') as createtime")
            ->page($page)
            ->limit($this->pageSize)
            ->order('ea.createtime desc')
            ->select();
        foreach ($list as $key => $val){
            $image_url = explode(',',$val['image_url']);
            $list[$key]['image_count'] = count($image_url);
        }
        $this->success('获取成功',['list' =>$list,'people' =>$people]);
    }

    /**
     * 文章详情
     */
    public function detail()
    {
        $article_id = $this->request->post('article_id',0);
        if(!$article_id){
            $this->error('查无此文章');
        }
        $info = Ethnography::alias('ea')
            ->join('family_people p','p.user_id = ea.user_id','left')
            ->where(['ea.id' =>$article_id])
            ->field("ea.*,p.name,p.photo,p.address,FROM_UNIXTIME(ea.createtime,'%Y-%m-%d %T') as createtime")
            ->find();
        $collect = Collect::where(['article_id' =>$article_id,'user_id' =>$this->auth->id])->find();
        if($collect && $collect['status'] == 1){
            $info['ca_status'] = 1;
        }else{
            $info['ca_status'] = 0;
        }
        $this->success('获取成功',['info' =>$info]);
    }

    /**
     * 收藏文章
     */
    public function collectArticle()
    {
        $article_id = $this->request->post('article_id',0);
        $uid = $this->auth->id;
        $where=[
            'article_id' =>$article_id,
            'user_id' =>$uid,
        ];
        $user_collect = Collect::where($where)->find();
        if($user_collect){
            if($user_collect->status==0){
                Collect::where($where)->update(['status' =>1]);
                $this->success('收藏成功');
            }else{
                $this->error('您已收藏');
            }
        }else{
            $this->collect->article_id = $article_id;
            $this->collect->user_id = $uid;
            $this->collect->createtime = time();
            $this->collect->status = 1;
            $this->collect->save();
            $this->success('收藏成功');
        }

    }


    /**
     * 获取收藏文章
     */
    public function getCollectArticle()
    {
        $page = $this->request->post('page',1);
        $uid = $this->auth->id;
        $where = [
            'c.user_id' =>$uid,
            'c.status' =>1,//已收藏
        ];
        $list = Collect::alias('c')
            ->join('ethnography_article ea','ea.id = c.article_id','left')
            ->join('family_people p','p.user_id = ea.user_id','left')
            ->where($where)
            ->page($page)
            ->limit($this->pageSize)
            ->field("ea.*,p.name,p.photo,p.address,FROM_UNIXTIME(ea.createtime,'%Y-%m-%d %T') as createtime")
            ->order('c.createtime desc')
            ->select();
        if(!$list){
            $this->error('暂无收藏文章');
        }
        $this->success('获取成功',['list' =>$list]);
    }

    /**
     * 取消收藏文章
     */
    public function cancelCollectArticle()
    {
        $article_id = $this->request->post('article_id',0);
        $where = [
            'article_id' =>$article_id,
            'status' =>1,//已收藏
        ];
        $collect = Collect::where($where)->find();
        if(!$collect){
            $this->error('状态不允许');
        }
        Collect::where($where)->update(['status' =>0,'updatetime' =>time()]);
        $this->success('取消成功');
    }

}