<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\auth;

use think\facade\Request;
use app\model\auth\Departments;
use app\services\auth\DepartmentService;
use Xin\Support\Arr;
use app\controller\Base;

class Department extends Base {
	/**
	 * @title 部门列表
	 *
	 * @time 2020年01月09日
	 * @param Departments $department
	 * @return Array
	 */
	public function index() {
		$list = app()->make(DepartmentService::class)->getDepartmentList($this->request)->toArray();
		if(count($list) > 0){
			$root = '';
			foreach ($list as $value) {
				if($root == ''){
					$root = $value['parent_id'];
				}else{
					if($root > $value['parent_id']){
						$root = $value['parent_id'];
					}
				}
			}
			$tree = Arr::tree($list, null, 0, ['id'=>'id', 'parent' => 'parent_id', 'child' => 'children']);
			if(empty($tree)){
				$this->data['data'] = $list;
			}else{
				$this->data['data'] = $tree;
			}
		}else{
			$this->data['data'] = [];
		}
		
		return $this->data;
	}
	/**
	 * @title 添加部门
	 *
	 * @time 2020年01月09日
	 * @return Array
	 */
	public function add() {
		$data = request()->post();
		$data['creator_id'] = request()->user['uid'];
		try {
			$result = Departments::create($data);
			$this->data['message']  = '添加成功！';
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message']  = $e->getMessage();
		}
		return $this->data;
	}
	/**
	 * @title 更新部门
	 *
	 * @time 2020年01月09日
	 * @param $id
	 * @param Request $request
	 * @return Array
	 */
	public function edit() {
		$data = request()->post();
		$data['creator_id'] = request()->user['uid'];
		try {
			$result = Departments::update($data);
			$this->data['message']  = '更新成功！';
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message']  = $e->getMessage();
		}
		return $this->data;
	}
	/**
	 * @title 删除部门
	 *
	 * @time 2020年01月09日
	 * @param $id
	 * @return Array
	 */
	public function delete() {
		$data = request()->post('id');
		$map = [];
		if(is_array($data)){
			$map[] = ['id', 'IN', $data];
		}else if(is_numeric($data)){
			$map[] = ['id', '=', $data];
		}
		try {
			$result = Departments::destroy(function($query) use ($map){
				$query->where($map);
			});
			$this->data['message']  = '删除成功！';
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message']  = $e->getMessage();
		}
		return $this->data;
	}
	/**
	 * @title 获取班级
	 *
	 * @return void
	 */
	public function studentclass(){
		$this->data['data'] = app()->make(DepartmentService::class)->getStudentClassList();
		$this->data['code'] = 1;
		return $this->data;
	}
}