<?php
namespace app\mainapp\controller;

use app\mainapp\BaseController;
use think\facade\Db;
use think\facade\Session;

use app\common\model\Slide as SlideModel;
use app\common\model\Notice as NoticeModel;
use app\common\model\Article as ArticleModel;
use app\common\model\Navigation as NavigationModel;

use app\common\model\User as UserModel;
use app\common\model\Worker as WorkerModel;

use app\common\model\Comjobs as ComjobsModel;
use app\common\model\Param as ParamModel;

use app\common\model\Sinpage as SinpageModel;

class Index extends BaseController
{

	public function getHomeData()
	{
		$userid = input('userid/d', 0);
		$workerall = WorkerModel::where('userid','=',$userid)->select();
		$sinpage = SinpageModel::where(1)->find();
		page_result(0, "", array(
			'apptreaty' 	=> $sinpage->apptreaty,
			'workerall'		=> $workerall->isEmpty() ? null : $workerall
		)); 
	}
	
	public function getIndexData()
	{
		$param = ParamModel::where(1)->find();
		$slide = SlideModel::where('pagetab','=','index')->order(['priority'=>'desc','id'=>'desc'])->limit(10)->select();
		$navigation = NavigationModel::where('pagetab','=','index')->order(['priority'=>'desc','id'=>'desc'])->select();
		$article = ArticleModel::where([['status','=',1],['priority','>',0],['status','=',1],['createtime','<=',time()]])->order(['priority'=>'desc','id'=>'desc'])->limit(10)->select();
		page_result(0, "", array(
			'param'			=> $param,
			'slide'			=> $slide,
			'navigation'	=> $navigation,
			'article'		=> $article 
		));
	}

	public function listComjobs()
	{
		ComjobsModel::where('enddate', '<', time()-86400)->where('status', '=', 3)->update(['status' => 4]);		
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map[] = ['createtime','<=',time()];
		$map[] = ['priority','>',0]; 
		$map[] = ['status','in','3,4'];
		$plist = ComjobsModel::with(['worker'])->where($map)->order(['status'=>'asc','priority'=>'desc','updatetime'=>'desc','id'=>'desc'])->page($ppage)->limit($psize)->select();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
    /*
     **
     * 附近职位
     * */
    public function listComjobsofnear()
    {
        $lat=input('lat',0); //纬度
        $lng=input('lng',0); //经度
        $radius=input('radius',0); //半径
        //$scope = $this->calcScope($lat, $lng, $radius);   // 调用范围计算函数，获取最大最小经纬度
        if(input('ppage/d') == '' or input('ppage/d') == 0){
            $page = 1;
        }else{
            $page=input('ppage/d');
        }
        if(input('psize/d') == ''){
            $limit = 20;
        }else{
            $limit = input('psize/d');
        }
        $amount = 0;
        $pagelistcount = $limit; //每页显示条数
        $sql = distance_sql($lng,$lat);
        //$sql = distance_sql();
        $row = Db::query("select * ,".$sql."as distance from echo_comjobs where deletetime=0 and status in (3,4) order by distance limit ".$pagelistcount*($page-1).",".$pagelistcount);

        $count = Db::table('echo_comjobs')->count();
        $Pages = ceil($count/$limit);
        foreach ($row as $k => $v){
            $row[$k]["tags"]=json_decode($v["tags"]);
            $workerinfo=WorkerModel::find($v["workerid"]);
            $row[$k]["worker"]["title"]=$workerinfo["title"];
            $row[$k]["distance"]=round(calcDistance($lat,$lng,$v["latitude"],$v["longitude"]),2)."千米";
            //$row[$k]["distance"]=calcDistance($lat,$lng,$v["latitude"],$v["longitude"])/1000;
        }
        page_result(0, "", array(
            'plist' => $row,
            'pstatus' => $limit > count($row) ? 'noMore' : 'more'
        ));

    }


	
}