完善人口新增,删除,编辑

main
vine_liutk 2024-02-20 17:00:51 +08:00
parent 2358300875
commit d43eebf310
7 changed files with 161 additions and 19 deletions

View File

@ -26,7 +26,7 @@ class PersonController extends AdminController
{
$crud = $this->baseCRUD()->tableLayout('fixed')
->headerToolbar([
$this->createTypeButton('drawer', 'xl'),
$this->createTypeButton('drawer', '', '50%'),
amis('reload')->align('right'),
amis('filter-toggler')->align('right'),
])
@ -47,7 +47,7 @@ class PersonController extends AdminController
]),
]))
->columns([
amis()->TableColumn('domicile_code', __('admin.persons.domicile_code'))->width('120px')->copyable(),
amis()->TableColumn('domicile_code', __('admin.persons.domicile_code'))->width('100px'),
amis()->TableColumn('name', __('admin.persons.name'))->width('100px')->copyable()->searchable(),
amis()->TableColumn('master_connect', __('admin.persons.master_connect'))->width('60px'),
amis()->TableColumn('master.name', __('admin.persons.master_name'))->width('100px')->copyable()->searchable(),
@ -64,7 +64,8 @@ class PersonController extends AdminController
amis()->TableColumn('now_address', __('admin.persons.now_address'))->copyable(),
amisMake()->Operation()->label(__('admin.actions'))->buttons([
$this->rowEditTypeButton('drawer', 'xl'),
$this->rowEditTypeButton('drawer', '', '50%'),
$this->rowDeleteButton(),
])
]);
@ -200,7 +201,7 @@ class PersonController extends AdminController
]),
]
]
)->description('输入 户主姓名 进行筛选,忽略则为添加户主'),
)->description('输入 户主姓名 进行筛选,忽略则为添加户主')->disabledOn('this.id > 0'),
amis()->TextControl('master_connect', __('admin.persons.master_connect'))->required(),
]),
]),

View File

@ -14,8 +14,6 @@ class Person extends Model
use Filterable;
protected $table = 'persons';
protected $fillable = ['type','name', 'used_name', 'value', 'parent_id', 'parent_key', 'path', 'sort', 'lv', 'oid'];
protected $appends = ['age', 'now_address'];
@ -24,6 +22,31 @@ class Person extends Model
return $date->format('Y-m-d H:i:s');
}
protected static function boot()
{
parent::boot();
// 监听 oldman 的创建事件,用于初始化 位置信息
static::saving(function ($person) {
if($person->origin_city_code){
list($originProvince, $originCity, $originArea) = Zone::codeToZone($person->origin_city_code);
$person->origin_province_id = $originProvince?->id ?? 0;
$person->origin_city_id = $originCity?->id ?? 0;
$person->origin_area_id = $originArea?->id ?? 0;
}
if($person->card_city_code){
list($cardProvince, $cardCity, $cardArea) = Zone::codeToZone($person->card_city_code);
$person->card_province_id = $cardProvince?->id ?? 0;
$person->card_city_id = $cardCity?->id ?? 0;
$person->card_area_id = $cardArea?->id ?? 0;
$person->card_complete_address = ($cardProvince?->name ?? '未知').'-'.($cardCity?->name ?? '未知').'-'.($cardArea?->name ?? '未知').$person->card_address;
}
});
}
public function scopeValid($q){
$q->where('valid', 1);
}
public function scopeSort($q)
{
$q->orderBy('id', 'asc');

View File

@ -0,0 +1,45 @@
<?php
namespace App\Models;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class Zone extends Model
{
use Filterable;
protected $fillable = ['name', 'parent_id', 'type', 'index', 'code'];
public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
}
public function children()
{
return $this->hasMany(self::class, 'parent_id');
}
public static function codeToZone($code = null)
{
$province = $city = $area = null;
$zone = self::where('code', $code)->first();
if($zone){
if($zone->type == 'area'){
$area = $zone;
$city = $area->parent;
$zone = $city;
}
if($zone->type == 'city'){
$city = $zone;
$province = $city->parent;
$zone = $province;
}
if($zone->type == 'province'){
$province = $zone;
}
}
return array($province, $city, $area);
}
}

View File

@ -26,6 +26,56 @@ class PersonService extends BaseService
public function query(): Builder
{
return $this->modelName::query()->whereIn('type', [11, 12]);
return $this->modelName::query()->valid()->whereIn('type', [11, 12]);
}
public function store($data): bool
{
$columns = $this->getTableColumns();
$model = $this->getModel();
$data['avatar'] = $this->saveImage('avatar', 'persons/avatar')[0] ?? '';
foreach ($data as $k => $v) {
if (!in_array($k, $columns)) {
continue;
}
$model->setAttribute($k, $v);
}
return $model->save();
}
public function update($primaryKey, $data): bool
{
$columns = $this->getTableColumns();
$model = $this->query()->whereKey($primaryKey)->first();
if(isset($data['avatar'])){
$data['avatar'] = $this->saveImage('avatar', 'persons/avatar')[0] ?? '';
}
foreach ($data as $k => $v) {
if (!in_array($k, $columns)) {
continue;
}
$model->setAttribute($k, $v);
}
return $model->save();
}
/**
* 软删除
*
* @param string $ids
*
* @return mixed
*/
public function delete(string $ids): mixed
{
return $this->query()->whereIn($this->primaryKey(), explode(',', $ids))->update(['valid'=> 0]);
}
}

View File

@ -16,18 +16,23 @@ trait CustomActionTrait
*
* @param string $type
* @param string $size
* @param string $width
*
* @return DialogAction|DrawerAction|LinkAction
*/
protected function createTypeButton(string $type = '', string $size = ''): DialogAction|DrawerAction|LinkAction
protected function createTypeButton(string $type = '', string $size = '', string $width = ''): DialogAction|DrawerAction|LinkAction
{
switch ($type) {
case 'drawer':
$form = $this->form(false)->api($this->getStorePath())->onEvent([]);
$button = DrawerAction::make()->drawer(
Drawer::make()->title(__('admin.create'))->body($form)->size($size)
);
$drawer = Drawer::make()->title(__('admin.create'))->body($form);
if($width){
$drawer->width($width);
}else{
$drawer->size($size);
}
$button = DrawerAction::make()->drawer($drawer);
break;
case 'dialog':
$form = $this->form(false)->api($this->getStorePath())->onEvent([]);
@ -49,10 +54,11 @@ trait CustomActionTrait
*
* @param string $type
* @param string $size
* @param string $width
*
* @return DialogAction|DrawerAction|LinkAction
*/
protected function rowEditTypeButton(string $type = '', string $size = ''): DialogAction|DrawerAction|LinkAction
protected function rowEditTypeButton(string $type = '', string $size = '', string $width = ''): DialogAction|DrawerAction|LinkAction
{
switch ($type) {
case 'drawer':
@ -62,9 +68,15 @@ trait CustomActionTrait
->redirect('')
->onEvent([]);
$button = DrawerAction::make()->drawer(
Drawer::make()->title(__('admin.edit'))->body($form)->size($size)
);
$drawer = Drawer::make()->title(__('admin.edit'))->body($form);
if($width){
$drawer->width($width);
}else{
$drawer->size($size);
}
$button = DrawerAction::make()->drawer($drawer);
break;
case 'dialog':
$form = $this->form(true)

View File

@ -17,7 +17,7 @@ return new class extends Migration
$table->string('avatar')->nullable()->comment('头像');
$table->string('used_name')->nullable()->comment('曾用名');
$table->string('idcard', 50)->unique()->nullable()->comment('身份证号码');
$table->string('idcard', 50)->nullable()->comment('身份证号码');
$table->unsignedBigInteger('gender')->nullable()->comment('性别:0未知1男2女');
$table->date('birthday')->nullable()->comment('生日');
@ -67,7 +67,13 @@ return new class extends Migration
$table->unsignedTinyInteger('state')->nullable()->comment('状态1正常2死亡3迁出');
$table->date('deathday')->nullable()->comment('死亡日期');
$table->unsignedTinyInteger('valid')->default(1)->comment('是否有效');
$table->timestamps();
$table->unique(['valid', 'idcard']);
});
}

View File

@ -108,14 +108,19 @@ class PersonSeeder extends Seeder
}
}
//处理籍贯地址选中问题-todo;如果是重庆市-巴南区,重庆市-九龙坡,重庆-巴县,重庆市-綦江区
//处理籍贯地址选中问题;如果是重庆市-巴南区,重庆市-九龙坡,重庆-巴县,重庆市-綦江区
if(Str::startsWith($person->nativePlace, '重庆市') || Str::startsWith($person->nativePlace, '重庆')){
$_person['origin_city_code'] = '500100';
$_person['origin_province_id'] = 2221;
$_person['origin_city_id'] = 2222;
}
//处理户籍地址选中问题*-doto
//处理户籍地址选中问题;如果是重庆市-巴南区,重庆市-九龙坡,重庆-巴县,重庆市-綦江区
if(Str::startsWith($person->birthAddr, '重庆市') || Str::startsWith($person->birthAddr, '重庆')){
$_person['origin_city_code'] = '500100';
$_person['origin_province_id'] = 2221;
$_person['origin_city_id'] = 2222;
}
//处理居住地址
if($person->courtId){
$houseCompleteAddress = '';