Server_AccessControl/models/Department.php

139 lines
3.9 KiB
PHP

<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "department".
*
* @property int $id
* @property int $code
* @property int $pid
* @property string $name
* @property int $created_at
* @property int $modified_at
*/
class Department extends \yii\db\ActiveRecord {
/**
* {@inheritdoc}
*/
public static function tableName() {
return 'department';
}
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['code', 'pid', 'name', 'created_at', 'modified_at'], 'required'],
[['code', 'pid', 'created_at', 'modified_at'], 'integer'],
[['name'], 'string', 'max' => 100],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels() {
return [
'id' => 'ID',
'code' => 'Mã phòng ban',
'pid' => 'Trực thuộc',
'name' => 'Tên phòng ban',
'created_at' => 'Thời gian tạo',
'modified_at' => 'Thời gian sửa',
];
}
public static function departmentArray() {
$lists = self::find()->all();
$results = [];
foreach ($lists as $key => $value) {
$results[$value->code] = $value->name;
}
return $results;
}
public static function departmentArrayWithOut($id) {
$lists = self::find()->andWhere(["<>", "id", $id])->all();
$results = [];
foreach ($lists as $key => $value) {
$results[$value->code] = $value->name;
}
return $results;
}
public function create($data) {
$r = $this->load([
"code" => $data["Code"],
"pid" => $data["Pid"],
"name" => $data["Name"],
"created_at" => time(),
"modified_at" => time()
], '');
if ($r) {
try {
$this->save();
return $this->id;
} catch (\Exception $ex) {
return false;
}
}
}
public function multiCreate($datas) {
$field = ['code', 'pid', 'name', 'created_at', 'modified_at'];
static::getDb()->createCommand()->batchInsert($this->tableName(), $field, $datas)->execute();
return;
}
public static function deleteDepartment($id) {
if ($id == 1)
return;
$model = self::findOne($id);
if ($model) {
$childs = self::find()->andWhere(["pid" => $model->code])->all();
foreach ($childs as $key => $value) {
self::deleteDepartment($value->id);
}
$model->delete();
common::insertSystemLogs(["action" => "delete", "description" => "Xóa phòng ban: " . $model->name, "type" => "department"]);
}
}
public function checkStatusImport($data) {
if ($this->findOne(["code" => $data["A"]]))
return ["status" => false, "description" => "Mã phòng ban đã tồn tại"];
if (!$this->findOne(["name" => $data["C"]]))
return ["status" => true, "description" => "Phòng ban trực thuộc không tồn tại"];
return ["status" => true, "description" => ""];
}
public function getChilds() {
return $this->find()->andWhere(['pid' => $this->code])->all();
}
public function getCountStaff() {
$childs = $this->childs;
$totals = 0;
foreach ($childs as $key => $value) {
$totals += $value->countStaff;
}
$totals += Staff::find()->andWhere(['department_id' => $this->code])->count();
return $totals;
}
public function departmentChilds($id, &$childs = []) {
$childs[] = $id;
$ls = $this->find()->andWhere(['pid' => $id])->all();
foreach ($ls as $key => $value) {
$this->departmentChilds($value->code, $childs);
}
return $childs;
}
}