<?php
declare (strict_types = 1);
namespace app\system\controller\v1;

use app\BaseController;
use app\model\api\AdverPage;
use app\model\api\Advert;
use app\model\api\User as UserModel;
use app\model\api\GoodsCate;
use app\model\api\UserCircle;
use app\Request;
use library\services\UtilService;
use think\facade\Db;

// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | [ 圈子管理 ]
// +----------------------------------------------------------------------
// | Date: 2020-09-06 21:53
// +----------------------------------------------------------------------

class Circle extends  BaseController{
    
    /**
     * 动态列表
     * @param Request $request
     * @return type
     */
    public function list(Request $request) {
        $pageSize = 50;
        $post =  UtilService::getMore([
                ['page',1],
//                ['title',''],
                ['nickname',''],
                ['status',''],
                ['uid',''],
                ['mobile',''],
                ['uip',''],
                ['time',[]],
        ],$request);
        $where = [];
        //创建时间
        $startTime="";
        $endTime="";
        if(!empty($post['time'][0]) && !empty($post['time'][1])) {
            $startTime = strtotime($post['time'][0]);
            $endTime = strtotime($post['time'][1]);
            $where[]=["c.time","between","{$startTime},{$endTime}"];
        }
        //用户
        if(!empty($post['uid'])){
            $where[]=["c.uid","=",$post['uid']];
        }else if (!empty($post['uip'])) {
            $m  = Db::name("user")->where("uip",$post['uip'])->find();
            if(!empty($m)) {
                $where[]=["c.uid","=",$m['uid']];
            }
        }else if (!empty($post['mobile'])) {
            $m  = Db::name("user")->where("mobile",$post['mobile'])->find();
            if(!empty($m)) {
                $where[]=["c.uid","=",$m['uid']];
            }
        }else if(!empty($post['nickname'])){
            $m  = Db::name("user")->where("nickname",$post['nickname'])->find();
            if(!empty($m)) {
                $where[]=["c.uid","=",$m['uid']];
            }
        }
//        if (!empty($post["title"])){
//            $where[]=["c.title","like","%{$post['title']}%"];
//        }
        $where2 = $where;
        $where2[]=["c.status","=",0];//待审核
        if(in_array((string)$post["status"],['0','1','-1'])){
            $where[]=["c.status","=",(int)$post["status"]];
        }
        $data = (new UserCircle)
                ->field("c.*,u.nickname,g.imgs as pro_imgs,g.title as pro_title")
                ->alias("c")
                ->where($where)
                ->leftJoin("user u", "u.uid = c.uid")
                ->leftJoin("goods g", "g.id = c.pro_id")
                ->page((int)$post["page"], (int)$pageSize)
                ->order("c.id","desc")
                ->select()
                ->toArray();
        $pageCount = (new UserCircle)->alias("c")->where($where)->count();
        $waitCount = (new UserCircle)->alias("c")->where($where2)->count();
        $data = empty($data)?[]:$data;
        foreach($data as $k=>$v){
            $data[$k]["imgs"] = empty($v["imgs"])?[]:explode(",",$v["imgs"]);
            $data[$k]["time"] = date("Y-m-d H:i:s",$v["time"]);
            $data[$k]["pro_img"] = empty($v["pro_imgs"]) ? "" : explode(",", $v["pro_imgs"])[0];
        }
        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post["page"],
            'waitCount' => $waitCount,
        ]);
    }
    /**
     * 审核动态
     * @param Request $request
     */
    public function editStatus(Request $request) {
        [$id,$status] = UtilService::getMore([
            ['id',0,'empty','参数错误'],
            ['status','','empty','参数错误']
        ],$request,true);
        
        $data = (new UserCircle)->where("id",$id)->find();
        if(empty($data)){
            return app('json')->fail("数据不存在");
        }
        //禁用
        if($status==-1){
            (new UserCircle)->where("id",$id)->update(["status"=>-1]);
        }
        //删除
        if($status==-10){
            (new UserCircle)->where("id",$id)->delete();
        }
        //发布
        if($status==1){
            (new UserCircle)->where("id",$id)->update(["status"=>1]);
        }
        return app('json')->success("操作成功");
    }
    
    

    
}