<?php
/**
 * Created by PhpStorm.
 * User: guanxl
 * Date: 2018/3/31
 * Time: 18:51
 */

namespace App\Services;
use App\Models\Department;
use App\Models\User;
use App\Traits\Singleton;
use Illuminate\Support\Facades\Redis;

class DepartmentService
{
    use Singleton;
    const DEPARTMENTS = "bs_departments";
    /**
     * 获得所有部门信息
     * @return mixed
     */
    public function all($activityId){
        $departments = Department::where("activity_id",$activityId)->where("status",1)->pluck("department_name","department_id");
        //$departments = collect($departments)->toArray();
        return $departments;
    }

    /**
     * 获得所有部门的总人数
     * @return mixed
     */
    public function allPcount($activityId){
        $departments = Department::where("activity_id",$activityId)->where("status",1)->pluck("user_num","department_id");
        return collect($departments)->toArray();
    }

    /**
     * 获取部门人员Id
     * @param $departmentId
     * @return mixed
     */
    public function getUserIdsByDepartmentId($departmentId){
        $userIds = User::where("department_id",$departmentId)->pluck("user_id");
        return $userIds;
    }

    /**
     * 获得部门名称
     * @param $departmentId
     * @return string
     */
    public function getDepartmentName($departmentId){
        $departmentName = "";
        if(!empty($departmentId)){
            $departmentName = Redis::get(self::DEPARTMENTS.":".$departmentId);
            if(empty($departmentName)){
                $department = Department::find($departmentId);
                if($department){
                    $departmentName = $department->department_name;
                    Redis::set(self::DEPARTMENTS.":".$departmentId,$departmentName);
                    Redis::expire(self::DEPARTMENTS.":".$departmentId,600);
                }
            }
        }
        return $departmentName;
    }
}