121 lines
3.2 KiB
PHP
121 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\Oldmen;
|
|
use App\Filters\Admin\OldmenFilter;
|
|
use App\Models\ConstFlow;
|
|
|
|
/**
|
|
* @method Oldmen getModel()
|
|
* @method Oldmen|\Illuminate\Database\Query\Builder query()
|
|
*/
|
|
class OldmenService extends BaseService
|
|
{
|
|
protected string $modelName = Oldmen::class;
|
|
|
|
protected string $modelFilterName = OldmenFilter::class;
|
|
|
|
public function store($data): bool
|
|
{
|
|
if ($this->hasRepeated($data)) {
|
|
return false;
|
|
}
|
|
|
|
$columns = $this->getTableColumns();
|
|
|
|
$model = $this->getModel();
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
if ($this->hasRepeated($data, $primaryKey)) {
|
|
return false;
|
|
}
|
|
|
|
$columns = $this->getTableColumns();
|
|
|
|
$model = $this->query()->whereKey($primaryKey)->first();
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
|
|
public function hasRepeated($data, $id = 0): bool
|
|
{
|
|
$query = $this->query()->when($id, fn($query) => $query->where('id', '<>', $id));
|
|
|
|
if ((clone $query)->where('card_no', $data['card_no'])->exists()) {
|
|
$this->setError('该身份证号码已存在');
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
//计算金额
|
|
public function totalFee($feeArr){
|
|
$money = 0;
|
|
if($feeArr){
|
|
foreach($feeArr as $k => $item){
|
|
switch($k){
|
|
case 'add_fee':
|
|
foreach($item as $value){
|
|
if(isset($value['name'])){
|
|
$money = bcadd($money, $value['fee'], 2);
|
|
}
|
|
}
|
|
|
|
break;
|
|
case 'del_fee':
|
|
foreach($item as $value){
|
|
if(isset($value['name'])){
|
|
$money = bcsub($money, $value['fee'], 2);
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
foreach($item as $value){
|
|
$money = bcadd($money, $value, 2);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return $money;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @param string $ids
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function delete(string $ids): mixed
|
|
{
|
|
//检查删除的记录中是否有记录已经有缴费记录;有,则无法删除
|
|
if(ConstFlow::whereIn('oldman_id', explode(',', $ids))->count()){
|
|
return $this->setError('数据存在关联信息,已无法删除,请联系开发者删除。');
|
|
};
|
|
return $this->query()->whereIn($this->primaryKey(), explode(',', $ids))->delete();
|
|
}
|
|
}
|