<?php

namespace app\api\controller;

use app\api\BaseController;
use app\api\middleware\Auth;
use think\facade\Db;


/**
 * 我的综合文章
 */
class MyArticle extends BaseController
{
    /**
     * 控制器中间件 [不需要鉴权]
     * @var array
     */
    protected $middleware = [
        Auth::class => ['except' 	=> ['index','details'] ]
    ];

    public function index($search = '', $category_id = 1, $page = 1, $is_time=0,)
    {
        $where = [
            ['status', '=', 1],
            ['category_id', '=', $category_id],
            ['user_id', '=', JWT_UID]
        ];
        if (!$category_id) {
            unset($where[1]);
        }
        //总条数
        $count = Db::table('fa_article')->where($where)->count();
        //当月条数
        $month_count = Db::table('fa_article')->where($where)->whereMonth('view_time')->count();
        if ($search != '') {
            $where[] = ['title', 'like', '%' . $search . '%'];
        }
        //查询
        $select = Db::table('fa_article')->with('user')->where($where)->page($page)->limit(20)
            ->field('id,title,user_id,view,view_time,image,end_time,is_solve,is_vote,is_nickname,describe')->order('id DESC')->select()->toArray();
        foreach ($select as $key => $value) {
            if ($value['is_nickname'] == 1) {
                $select[$key]['nickname'] = "匿名人员";
                $select[$key]['avatar'] = "";
                $select[$key]['user_id'] = 0;
            }
            $select[$key]['nickname'] = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['user_id'])->value('name');
            //投票处理
            if ($value['is_vote'] == 1) {
                $article_vote = Db::table('fa_article_vote_side_tables')->where('article_id', $value['id'])->find();
                if ($article_vote) {
                    $data = $article_vote;
                    $data['agree_percentage'] = 0;
                    $data['opposition_percentage'] = 0;
                    $count_vote = $data['agree'] + $data['opposition'] + $data['other'];
                    if ($count_vote != 0) {
                        if ($data['agree'] != 0) {
                            $data['agree_percentage'] = round(($data['agree'] / $count_vote) * 100);
                        }
                        if ($data['opposition'] != 0) {
                            $data['opposition_percentage'] = round(($data['opposition'] / $count_vote) * 100);
                        }
                    }
                    $select[$key]['extend']['vote'] = $data;
                }
            } else {
                $select[$key]['extend'] = [];
            }
            if ($is_time==1){
                if ($value['end_time']<date('Y-m-d H:i:s')){
                    $cle=time()-strtotime($value['end_time']);
                    $select[$key]['overdue_time'] = ceil($cle / 3600 / 24);
                }else{
                    $select[$key]['overdue_time']=0;
                }

            }
            $select[$key]['is_read'] = Db::table('fa_article_comment')->where('vote_id',$value['id'])->value('is_read');
        }

        $name['address_name'] = Db::table('fa_szxc_information_usermsg')->where('user_id', JWT_UID)->value('address_name');
        $name['vartar'] = Db::table('fa_user')->where('id', JWT_UID)->value('avatar');
        $name['name'] = Db::table('fa_szxc_information_usermsg')->where('user_id', JWT_UID)->value('name');

        return $this->apiSuccess('ok', ['list' => $select, 'count' => ['count' => $count, 'month_count' => $month_count],'actor'=>$name]);
    }

    /**详情
     * @param $id
     * @return null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function details($id)
    {
        $where = [
            ['status', '=', 1],
            ['id', '=', $id],
            ['user_id', '=', JWT_UID]

        ];
        $find = Db::table('fa_article')->where($where)->withAttr('user_info', function ($data, $value) {
            $find['nickname'] = "匿名人员";
            $find['mobile'] = "";
            $find['avatar'] = "";
            $find['user_id'] = 0;
            $find['count'] = 0;
            $find['end_count'] = 0;
            $find['overdue_count'] = 0;
            if ($value['is_nickname'] == 0) {
                $user = Db::table('fa_user')->where('id', $value['user_id'])->field('nickname,avatar,mobile')->find();
                if ($user) {
                    $find = $user;
                }
            }
            $config_find = Db::table('fa_config')->where('id', 20)->find();
            //是否查询提案数
            if (in_array($value['category_id'], explode(',', $config_find['value']))) {
                //提案总数
                $where = [
                    ['category_id', '=', $value['category_id']],
                    ['user_id', '=', $value['user_id']]
                ];
                //处理总数
                $whereTwo = [
                    ['category_id', '=', $value['category_id']],
                    ['user_id', '=', $value['user_id']],
                    ['is_solve', '=', 1]
                ];
                $find['count'] = Db::table('fa_article')->where($where)->count();
                $find['end_count'] = Db::table('fa_article')->where($whereTwo)->count();
                //逾期总数
                $where = [
                    ['is_solve', '=', 0]
                ];
                $find['overdue_count'] = Db::table('fa_article')->where($where)->whereTime('end_time', '<=', date('Y-m-d H:i:s'))
                    ->count();
                $usermsg = Db::table('fa_szxc_information_usermsg')->where('user_id', $value['user_id'])->field('age,address_name')->find();
                $find['age'] = $usermsg['age'];
                $find['address'] = $usermsg['address_name'];
                $insurance=Db::table('fa_szxc_information_insurance')->where('user_id', $value['user_id'])->field('insurance_type')->find();
                if ($insurance){
                    $find['insurance_type']=$insurance['insurance_type'];
                }else{
                    $find['insurance_type']="";
                }
            }
            return $find;
        })->withAttr('category_type_title', function ($data, $value) {
            if ($value['category_type'] != 0) {
                $find = Db::table('fa_category')->where('id', $value['category_type'])->find();
                return $find['name'];
            }
        })
            ->find();
        //投票处理
        if ($find['is_vote'] == 1) {
            $article_vote = Db::table('fa_article_vote_side_tables')->where('article_id', $find['id'])->find();
            if ($article_vote) {
                $data = $article_vote;
                $data['agree_percentage'] = 0;
                $data['opposition_percentage'] = 0;
                $count_vote = $data['agree'] + $data['opposition'] + $data['other'];
                if ($count_vote != 0) {
                    if ($data['agree'] != 0) {
                        $data['agree_percentage'] = round(($data['agree'] / $count_vote) * 100);
                    }
                    if ($data['opposition'] != 0) {
                        $data['opposition_percentage'] = round(($data['opposition'] / $count_vote) * 100);
                    }
                }
                $find['extend']['vote'] = $data;
            }

        } else {
            $find['extend'] = [];

        }
        return $this->apiSuccess('ok', $find);
    }

}