cập nhật tính năng quản trị phòng ban
This commit is contained in:
111
models/Department.php
Normal file
111
models/Department.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?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 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();
|
||||
self::insertSystemLogs(["action" => "delete", "description" => "Xóa phòng ban: " . $model->name]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function insertSystemLogs($data) {
|
||||
$model = new LogsDepartment();
|
||||
return $model->create($data);
|
||||
}
|
||||
|
||||
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" => ""];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user