171 lines
4.5 KiB
PHP
171 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Model;
|
|
|
|
use App\Kernel\Str;
|
|
use Hyperf\DbConnection\Db;
|
|
|
|
/**
|
|
* @property int $org_id
|
|
* @property string $org_name
|
|
* @property string $org_code
|
|
* @property string $contact_name
|
|
* @property string $contact_mobile
|
|
* @property int $status
|
|
* @property int $del_flag
|
|
* @property string $create_time
|
|
* @property string $update_time
|
|
*/
|
|
class Org extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*/
|
|
protected ?string $table = 'org';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*/
|
|
protected array $fillable = [];
|
|
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*/
|
|
protected array $casts = ['org_id' => 'integer', 'status' => 'integer', 'del_flag' => 'integer'];
|
|
|
|
/**
|
|
* Author: cfn <cfn@leapy.cn>
|
|
* @param int $belong_id
|
|
* @param array $field
|
|
* @return array
|
|
*/
|
|
public static function getById(int $belong_id, array $field=['*'])
|
|
{
|
|
$info = self::where("del_flag",0)
|
|
->where("org_id",$belong_id)
|
|
->first($field);
|
|
return $info ? $info->toArray() : [];
|
|
}
|
|
|
|
/**
|
|
* Author: cfn <cfn@leapy.cn>
|
|
* @param array $param
|
|
* @return \Hyperf\Contract\LengthAwarePaginatorInterface
|
|
*/
|
|
public static function list(array $param)
|
|
{
|
|
$model = self::where('del_flag', 0);
|
|
if (isset($param['org_name']) && $param['org_name'] != '') {
|
|
$model = $model->where('org_name', "like", "%$param[tenant_name]%");
|
|
}
|
|
return $model->orderByDesc("org_id")
|
|
->paginate((int)$param['limit'], ["org_id", "org_name", "org_code","contact_name",
|
|
"contact_mobile", "status", "create_time"], 'page', (int)$param['page']);
|
|
}
|
|
|
|
/**
|
|
* Author: cfn <cfn@leapy.cn>
|
|
* @param array $param
|
|
* @return bool
|
|
*/
|
|
public static function add(array $param)
|
|
{
|
|
$param['create_time'] = date("Y-m-d H:i:s");
|
|
$param['del_flag'] = 0;
|
|
$password = $param['password'];
|
|
$org_code = "20" . date("Y") . Str::randInt(4);
|
|
$param['org_code'] = $org_code;
|
|
unset($param['password']);
|
|
Db::beginTransaction();
|
|
$org_id = self::insertGetId($param);
|
|
if (!$org_id) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
$salt = Str::randStr(6);
|
|
$res = Account::insert([
|
|
'account_type' => 1,
|
|
'belong_id' => $org_id,
|
|
'username' => $org_code,
|
|
'master_flag' => 1,
|
|
'password' => md5($salt . $password),
|
|
'salt' => $salt,
|
|
'create_time' => date("Y-m-d H:i:s")
|
|
]);
|
|
if (!$res) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 修改
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public static function edit(array $data)
|
|
{
|
|
$data['update_time'] = date("Y-m-d H:i:s");
|
|
$password = $data['password'];
|
|
unset($data['password']);
|
|
$info = self::getById($data['org_id']);
|
|
if (empty($info)) {
|
|
return false;
|
|
}
|
|
Db::beginTransaction();
|
|
$res = self::where("org_id", $data['org_id'])->where('del_flag', 0)->update($data);
|
|
if (!$res) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
if ($password) {
|
|
$salt = Str::randStr(6);
|
|
$res1 = Account::where("account_type", 1)
|
|
->where("belong_id", $data['org_id'])
|
|
->where("master_flag", 1)
|
|
->where("username", $info['org_code'])
|
|
->update([
|
|
'salt' => $salt,
|
|
'password' => md5($salt . $password),
|
|
'update_time' => date("Y-m-d H:i:s")
|
|
]);
|
|
if (!$res1) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
* @param string $ids
|
|
* @return int
|
|
*/
|
|
public static function del(string $ids)
|
|
{
|
|
return self::whereIn('org_id', explode(",", $ids))
|
|
->where("del_flag", 0)
|
|
->update([
|
|
'update_time' => date('Y-m-d H:i:s'),
|
|
'del_flag' => 1
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 选项
|
|
* @return array
|
|
*/
|
|
public static function options(): array
|
|
{
|
|
return self::where('del_flag', 0)
|
|
->select(["org_id","org_name", "org_code"])
|
|
->get()->toArray();
|
|
}
|
|
}
|