<?php
/**
 * Created by PhpStorm.
 * User: shaoguo
 * Date: 2020/4/24
 * Time: 3:31 PM
 */

namespace app\web\controller;


use model\Community;
use model\HouseInfo;
use model\UserInfo;
use service\DepartmentService;
use service\HouseMemberService;
use service\HouseService;
use service\PositionService;
use think\Controller;
use think\helper\Arr;

class CommonController extends Controller
{
    public function province(){
        return json([
            'code'=>200,
            'data'=>db('provinces')->where(['code'=>"32"])->field('code,name')->select()
        ]);
    }

    public function city(){
        $code=$this->request->param('code');
        return json([
            'code'=>200,
            'data'=>db('cities')->where('province_code',$code)->where('code',3205)->field('code,name')->select()
        ]);
    }

    public function area(){
        $code=$this->request->param('code');
        return json([
            'code'=>200,
            'data'=>db('areas')->where('city_code',$code)->field('code,name')->select()
        ]);
    }

    public function street(){
        $code=$this->request->param('code');
        return json([
            'code'=>200,
            'data'=>db('streets')->where('area_code',$code)->field('code,name')->select()
        ]);
    }


    public function village(){
        $code=$this->request->param('code');
        return json([
            'code'=>200,
            'data'=>db('villages')->where('street_code',$code)->field('code,name')->select()
        ]);
    }

    public function community(){
        $data=$this->request->get();
        $where[]=array('is_delete','=',0);
        if(isset($data['code']) && !empty($data['code'])){
            $where[]=['province_code|city_code|areas_code|street_code|village_code','=',$data['code']];
        }
        if(isset($data['id']) && !empty($data['id']))$where[]=['id','=',$data['id']];

        $communities=Community::where($where)->select();
        return json([
            'code'=>200,
            'data'=>$communities
        ]);


    }




    /**
     * @name 获取家庭
     */
    public function getHouse(){
        $where[]=['House.is_delete','=',0];
        $data=$this->request->param();
        if(isset($data['keyword']) && !empty($data['keyword'])){
            $where[]=['House.name','like','%'.$data['keyword'].'%'];
        }
        $lists=HouseService::get($where,"id,name",['House.create_time'=>'desc'],'',true);
        $count=HouseService::get($where,"id,name",['House.create_time'=>'desc'])->count();
        $result['item']=$lists->items();
        $result['count']=$count;
        $result = [
            'code' => 1,
            'msg'  => 'ok',
            'time' => time(),
            'data' => $result,
        ];
        return json($result,200);
    }


    /**
     * @name 获取家庭
     */
    public function getInfo(){
        $where[]=['is_delete','=',0];
        $data=$this->request->param();
        if(isset($data['keyword']) && !empty($data['keyword'])){
            $where[]=['name','like','%'.$data['keyword'].'%'];
        }
        $lists=HouseMemberService::get($where,"user_id as id,name",['create_time'=>'desc'],true);
        $count=HouseMemberService::get($where,"user_id as id,name",['create_time'=>'desc'])->count();
        $result['item']=$lists->items();
        $result['count']=$count;

        $result = [
            'code' => 1,
            'msg'  => 'ok',
            'time' => time(),
            'data' => $result,
        ];
        return json($result,200);
    }

    /**
     * @name 获取员工
     */
    public function user_info(){
        $with_where=array();
        $data=$this->request->param();

        if(isset($data['id']) && !empty($data['id']))$with_where[]=['pivot.department_id','=',$data['id']];
        $userinfos=DepartmentService::getOne([['id','=',$data['id']]])->userInfo;
        $result = [
            'code' => 1,
            'msg'  => 'ok',
            'time' => time(),
            'data' => $userinfos,
        ];
        return json($result,200);
    }

    public function position(){
        $id=$this->request->param('id');
        $where=array(['delete_time','=',0]);
        if(!empty($id)){
            $where[]=['id','in',$id];
        }
        $positions=PositionService::get($where);
        $result = [
            'code' => 1,
            'msg'  => 'ok',
            'time' => time(),
            'data' => $positions,
        ];
        return json($result,200);
    }

}
