suyuan/app/adminapi/logic/land/LandPlantLogic.php

170 lines
5.7 KiB
PHP
Raw Normal View History

2023-11-23 14:15:41 +08:00
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\adminapi\logic\land;
use app\common\logic\BaseLogic;
2023-12-01 11:36:01 +08:00
use app\common\model\land\LandPlant;
use app\common\model\action\Action;
use app\common\model\land\Land;
use app\common\model\plant\Plant;
2023-11-23 14:15:41 +08:00
use think\facade\Db;
/**
* LandPlant逻辑
* Class LandPlantLogic
* @package app\adminapi\logic\land
*/
class LandPlantLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/11/23 11:32
*/
public static function add(array $params): bool
{
2023-11-30 10:05:04 +08:00
$userId = Db::name('land')->where('id', $params['land_id'])->value('user_id');
2023-11-23 14:15:41 +08:00
Db::startTrans();
try {
LandPlant::create([
2023-11-30 10:05:04 +08:00
'user_id' => $userId,
2023-11-27 12:00:26 +08:00
'land_id' => $params['land_id'],
'kind' => $params['kind'],
'breed' => $params['breed'],
'area' => $params['area'],
'user' => $params['user'],
'remark' => $params['remark'],
'status' => $params['status'],
'qr_code' => $params['qr_code'],
2023-11-23 14:15:41 +08:00
'plant_date' => strtotime($params['plant_date']),
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/11/23 11:32
*/
public static function edit(array $params): bool
{
2023-12-07 16:41:06 +08:00
$landPlant = LandPlant::findOrEmpty($params['id'])->toArray();
$land = Land::findOrEmpty($landPlant['land_id'])->toArray();
$userId = $land['user_id'];
$newArea = $land['residual_area'] + ($landPlant['area'] - $params['area']);
2023-11-23 14:15:41 +08:00
Db::startTrans();
try {
LandPlant::where('id', $params['id'])->update([
2023-11-30 10:05:04 +08:00
'user_id' => $userId,
2023-11-27 12:00:26 +08:00
'land_id' => $params['land_id'],
'kind' => $params['kind'],
'breed' => $params['breed'],
'area' => $params['area'],
'user' => $params['user'],
'remark' => $params['remark'],
'status' => $params['status'],
'qr_code' => $params['qr_code'],
2023-11-23 14:15:41 +08:00
'plant_date' => strtotime($params['plant_date']),
]);
2023-12-07 16:41:06 +08:00
Land::where('id', $landPlant['land_id'])->update(['residual_area'=>$newArea]);
2023-11-23 14:15:41 +08:00
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/11/23 11:32
*/
public static function delete(array $params): bool
{
2023-12-07 16:01:42 +08:00
$landPlant = LandPlant::findOrEmpty($params['id'])->toArray();
2023-12-07 16:05:59 +08:00
Land::where('id', $landPlant['land_id'])->inc('residual_area', $landPlant['area'])->update();
2023-11-29 17:29:12 +08:00
Db::name('land_plant_action')->where('plant_id', $params['id'])->delete();
2023-11-23 14:15:41 +08:00
return LandPlant::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author likeadmin
* @date 2023/11/23 11:32
*/
public static function detail($params): array
{
return LandPlant::findOrEmpty($params['id'])->toArray();
}
2023-12-01 11:36:01 +08:00
public static function suyuan($params): array|bool
{
if(empty($params['id'])){
self::setError('参数错误');
return false;
}
$plantInfo = Plant::where('id',$params['id'])->findOrEmpty();
if($plantInfo->isEmpty()){
self::setError('种植信息错误');
return false;
}
if($plantInfo['status'] != 2){
self::setError('种植信息状态错误');
return false;
}
$landInfo = Land::where('id',$plantInfo['land_id'])->findOrEmpty();
if($landInfo->isEmpty()){
self::setError('土地信息错误');
return false;
}
$plantInfo['pic'] = json_decode($plantInfo['pic'],true);
2023-12-01 11:47:23 +08:00
$plantInfo['group_day'] = floor(($plantInfo['harvest_date'] - $plantInfo['plant_date']) / 86400);
2023-12-01 11:36:01 +08:00
$plantInfo['plant_date'] = date('Y-m-d',$plantInfo['plant_date']);
2023-12-01 11:47:23 +08:00
$plantInfo['harvest_date'] = date('Y-m-d',$plantInfo['harvest_date']);
2023-12-01 11:36:01 +08:00
$plantInfo['land_name'] = $landInfo['title'];
$plantInfo['land_area'] = $landInfo['total_area'];
$plantInfo['land_address'] = $landInfo['province_name'].$landInfo['city_name'].$landInfo['county_name'].$landInfo['town_name'].$landInfo['village_name'].$landInfo['group_name'];
//获取操作
$action = Action::field('type,type_text,detail,create_time')->where('plant_id',$params['id'])->select()->each(function($item){
$item['detail'] = json_decode($item['detail'],true);
return $item;
})->toArray();
$plantInfo['actions'] = $action;
return $plantInfo->toArray();
}
2023-11-23 14:15:41 +08:00
}