diff --git a/app/Admin/Controllers/PersonController.php b/app/Admin/Controllers/PersonController.php index af71519..beee0b1 100644 --- a/app/Admin/Controllers/PersonController.php +++ b/app/Admin/Controllers/PersonController.php @@ -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(), ]), ]), diff --git a/app/Models/Person.php b/app/Models/Person.php index 82ac8fd..5596ba3 100644 --- a/app/Models/Person.php +++ b/app/Models/Person.php @@ -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'); diff --git a/app/Models/Zone.php b/app/Models/Zone.php new file mode 100644 index 0000000..519d76a --- /dev/null +++ b/app/Models/Zone.php @@ -0,0 +1,45 @@ +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); + } +} diff --git a/app/Services/Admin/PersonService.php b/app/Services/Admin/PersonService.php index b129fb9..29af17d 100644 --- a/app/Services/Admin/PersonService.php +++ b/app/Services/Admin/PersonService.php @@ -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]); } } \ No newline at end of file diff --git a/app/Traits/CustomActionTrait.php b/app/Traits/CustomActionTrait.php index ac85b65..20dd281 100644 --- a/app/Traits/CustomActionTrait.php +++ b/app/Traits/CustomActionTrait.php @@ -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) diff --git a/database/migrations/2024_01_29_110440_create_people_table.php b/database/migrations/2024_01_29_110440_create_people_table.php index d94ed5e..967df37 100644 --- a/database/migrations/2024_01_29_110440_create_people_table.php +++ b/database/migrations/2024_01_29_110440_create_people_table.php @@ -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']); }); } diff --git a/database/seeders/migrations/PersonSeeder.php b/database/seeders/migrations/PersonSeeder.php index 00b9734..cfe66ae 100644 --- a/database/seeders/migrations/PersonSeeder.php +++ b/database/seeders/migrations/PersonSeeder.php @@ -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 = '';