StatisticsController.php 4.0 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use cmf\controller\AdminBaseController;
use app\portal\model\NeedAnswerModel;
use app\portal\model\MemberModel;
use app\portal\model\UserModel;
use app\portal\service\PostService;
use think\Db;
use think\Request;
use think\Loader;
/**
 * @title 保证金内容管理
 * @description 接口说明
 * @group 接口分组
 */

class StatisticsController extends AdminBaseController
{
    /**
     *  API返回信息格式函数 ;0失败,1成功,-1需要登录
     * @param string $code
     * @param string $message
     * @param array $data
     */
    public function apiResponse($code = '0', $message = '',$data = array(),$nums =0){
        header('Access-Control-Allow-Origin: *');
        header('Content-Type:application/json; charset=utf-8');
        $result = array(
            'code'=>$code,
            'message'=>$message,
            'data'=>$data,
            'nums'=>''.$nums
        );
        die(json_encode($result,JSON_UNESCAPED_UNICODE));
    }



//    列表
    public function index(Request $request)
    {
        return $this->fetch('index');
    }
    public function find(){
        //        注册人数
//        中介
        $data = $this->request->param();
        foreach ($data['time'] as $tk=>$tv){
            $time[$tk]['start_time'] = strtotime($tv);
            $time[$tk]['end_time'] = strtotime($tv)+86400;
        }
        $start_time = '';
        $end_time = '';
        $where_member['status'] = array('neq',9);
        $all_member = Db::name('Member')->where($where_member)->select()->toArray();
        $pie_user = 0;
        $pie_member = 0;
//                筛选时间判断
        if(!empty($data['start_time']) || !empty($data['end_time'])) {
//            搜索
            if (!empty($data['start_time'])) {
                $start_time = strtotime($data['start_time']);
            }
            if (!empty($data['end_time'])) {
                $end_time = strtotime($data['end_time']);
            }

        }else{
//           默认
            $date_str = date('Y-m-d',time());
            $date_time = strtotime($date_str);
            $end_time = time();
            $start_time = $date_time;
        }
//        饼状图
        foreach ($all_member as $mk => $mv) {
            if ($mv['create_time'] >= $start_time && $mv['create_time'] <= $end_time) {
                if ($mv['type'] == 1) {
                    $pie_user += 1;
                } else {
                    $pie_member += 1;
                }
            }
        }

//        柱状图
        foreach ($time as $timek=>$timev){
            $user_add = 0;
            $member_add = 0;
            foreach ($all_member as $memberk=>$memberv){
                //                判断时间
                if($memberv['create_time'] >=$timev['start_time'] && $memberv['create_time'] <=$timev['end_time']){
//                    判断类型
                    if($memberv['type'] == 1){
                        $user_add += 1;
                    }else{
                        $member_add += 1;
                    }
                }
            }
            $user[] = $user_add;
            $member[] = $member_add;

        }

        $final['user'] = $user;
        $final['member'] = $member;
        $final['start_time'] = $start_time;
        $final['end_time'] = $end_time;
        $final['pie_user'] = $pie_user;
        $final['pie_member'] = $pie_member;
        $this->apiResponse('1','成功',$final);


    }

}