87 lines
2.2 KiB
PHP
87 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\{PlantHarvestLog, RegionPlantLog};
|
|
use App\Filters\Admin\PlantHarvestLogFilter;
|
|
use Illuminate\Support\Arr;
|
|
use Throwable;
|
|
use DB;
|
|
|
|
/**
|
|
* @method PlantHarvestLog getModel()
|
|
* @method PlantHarvestLog|\Illuminate\Database\Query\Builder query()
|
|
*/
|
|
class CropHarvestService extends BaseService
|
|
{
|
|
protected string $modelName = PlantHarvestLog::class;
|
|
|
|
protected string $modelFilterName = PlantHarvestLogFilter::class;
|
|
|
|
public function store($data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->getModel();
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
if ($model->plant_id) {
|
|
$model->region_id = RegionPlantLog::where('id', $model->plant_id)->value('region_id');
|
|
}
|
|
|
|
try{
|
|
DB::beginTransaction();
|
|
if($model->save()){
|
|
//处理关联设备
|
|
$isLast = Arr::get($data, 'is_last');
|
|
if($isLast){
|
|
$model->plant()->update([
|
|
'end_at' => $model->harvest_at
|
|
]);
|
|
}
|
|
}
|
|
DB::commit();
|
|
return true;
|
|
}catch(Throwable $th){
|
|
DB::rollBack();
|
|
report($th);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->query()->whereKey($primaryKey)->first();
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
try{
|
|
DB::beginTransaction();
|
|
if($model->save()){
|
|
$isLast = Arr::get($data, 'is_last');
|
|
if($isLast){
|
|
$model->plant()->update([
|
|
'end_at' => $model->harvest_at
|
|
]);
|
|
}
|
|
}
|
|
DB::commit();
|
|
return true;
|
|
}catch(Throwable $th){
|
|
DB::rollBack();
|
|
report($th);
|
|
return false;
|
|
}
|
|
}
|
|
}
|