<?php
namespace catchAdmin\permissions\controller;

use catcher\base\CatchController;
use catchAdmin\permissions\model\Department as DepartmentModel;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
use catcher\Tree;
use catchAdmin\system\model\SysDictData;
use think\facade\Db;
class Department extends CatchController
{
    protected $department;

    public function __construct(DepartmentModel $department)
    {
        $this->department = $department;
    }

  /**
   * 列表
   *
   * @time 2020年01月09日
   * @param CatchRequest $request
   * @return \think\response\Json
   * @throws \think\db\exception\DbException
   */
    public function index(): \think\response\Json
    {
        return CatchResponse::success($this->department->getList());
    }
     /**
   * 学校列表
   *
   * @time 2020年01月09日
   * @param CatchRequest $request
   * @return \think\response\Json
   * @throws \think\db\exception\DbException
   */
  public function getSchoolDeparts(): \think\response\Json
  {
      return CatchResponse::success($this->department->getSchoolList());
  }

  /**
   * 运营商列表
   *
   * @time 2020年01月09日
   * @param CatchRequest $request
   * @return \think\response\Json
   * @throws \think\db\exception\DbException
   */
  public function getYysDeparts(): \think\response\Json
  {
      return CatchResponse::success($this->department->getYysDepartList());
  }
  
    /**
   * 班级列表
   *
   * @time 2020年01月09日
   * @param CatchRequest $request
   * @return \think\response\Json
   * @throws \think\db\exception\DbException
   */
  public function getClassDeparts($pid): \think\response\Json
  {
      return CatchResponse::success($this->department->getClassList($pid));
  }
    /**
   * 带有学校的部门列表
   *
   * @time 2020年01月09日
   * @param CatchRequest $request
   * @return \think\response\Json
   * @throws \think\db\exception\DbException
   */
  public function getDepartsHasSchool(): \think\response\Json
  {
      return CatchResponse::success($this->department->getListHasSchool());
  }
  /**
   * 保存
   *
   * @time 2020年01月09日
   * @param CatchRequest $request
   * @return \think\response\Json
   */
    public function save(CatchRequest $request): \think\response\Json
    {
        $params=$request->param();
        if($params['parent_id']){
            $parent_level=Db::table('departments')->where('id',$params['parent_id'])->value('level');
            if($parent_level){
                $params['level']=$parent_level.'-'.$params['parent_id'];
            }else{
                $params['level']=$params['parent_id'];
            }
        }

        return CatchResponse::success($this->department->storeBy($params));
    }

  /**
   * 更新
   *
   * @time 2020年01月09日
   * @param $id
   * @param CatchRequest $request
   * @return \think\response\Json
   */
    public function update($id, CatchRequest $request): \think\response\Json
    {
        $params=$request->param();
       
        

        return CatchResponse::success($this->department->updateBy($id, $params));
    }

  /**
   * 删除
   *
   * @time 2020年01月09日
   * @param $id
   * @return \think\response\Json
   */
    public function delete($id): \think\response\Json
    {
        if ($this->department->where('parent_id', $id)->find()) {
            throw new FailedException('存在子部门,无法删除');
        }
        if(Db::table('users')->where('department_id',$id)->count()){
            throw new FailedException('部门存在用户,无法删除');
        }
        return CatchResponse::success($this->department->deleteBy($id,true));
    }
     /**
   * 获取部门类型
   *
   * @time 2020年01月09日
   * @param $id
   * @return \think\response\Json
   */
  public function getDepartmentType(CatchRequest $request): \think\response\Json
  {
      $params=$request->param();
      $options = (new SysDictData)->getTypesByCodeWithRemark('DepartmentType',$params['departType']);
      return CatchResponse::success($options);
  }

  /**
   * 获取部门区域
   *
   * @time 2020年01月09日
   * @param CatchRequest $request
   * @return 
   */
  public function getDepartmentAreaId(CatchRequest $request)
  {
    $params=$request->param();
    $departInfo=Db::table('departments')->where('id',$params['id'])->find();
    $area_id='';
    if($departInfo['district_id']){
        $area_id=$departInfo['district_id'];
    }elseif($departInfo['city_id']){
        $area_id=$departInfo['city_id'];
    }else{
        $area_id=$departInfo['province_id'];
    }
    return CatchResponse::success($area_id);
  }

  /**
   * 仅获取学校数据
   */
  public function onlySchoolData(CatchRequest $request)
  {
    return CatchResponse::success($this->department->getOnlySchoolData());
  }

   /**
   * 仅获取学校下拉
   */
  public function getOnlySchoolOption(CatchRequest $request)
  {
    return CatchResponse::success($this->department->getOnlySchoolOption());
  }
  
    /**
     * 根据子级下拉
     * @time 2021年05月18日 13:50
     * @param $id
     */
    public function getChildrenOption($id)
    {
        $options = $this->department->where('parent_id',$id)->field('id as value,department_name as text')->select();
        return CatchResponse::success($options);
    }

}