suyuan-breed/app/adminapi/logic/fence_house/FenceHouseLogic.php

156 lines
4.9 KiB
PHP
Raw Normal View History

2024-01-10 15:48:12 +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\fence_house;
2024-01-31 15:58:18 +08:00
use app\common\model\device\Device;
2024-01-13 14:20:38 +08:00
use app\common\model\farm\Farm;
2024-01-10 15:48:12 +08:00
use app\common\model\fence_house\FenceHouse;
use app\common\logic\BaseLogic;
2024-01-31 15:58:18 +08:00
use app\common\model\land\Product;
use app\common\model\product\ProductDevice;
2024-01-10 15:48:12 +08:00
use think\facade\Db;
/**
* FenceHouse逻辑
* Class FenceHouseLogic
* @package app\adminapi\logic\fence_house
*/
class FenceHouseLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/10 15:15
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
FenceHouse::create([
'farm_id' => $params['farm_id'],
2024-01-18 11:21:28 +08:00
'animal_name' => $params['animal_name'],
2024-01-10 15:48:12 +08:00
'fence_house_name' => $params['fence_house_name'],
'animal_type' => $params['animal_type'],
'fence_house_type' => $params['fence_house_type'],
'capacity' => $params['capacity'],
'master' => $params['master'],
2024-01-25 10:00:59 +08:00
'master_phone' => $params['master_phone'],
2024-01-25 13:37:49 +08:00
'status' => 1,
'pic' => $params['pic'],
2024-01-10 15:48:12 +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 2024/01/10 15:15
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
FenceHouse::where('id', $params['id'])->update([
'farm_id' => $params['farm_id'],
2024-01-18 11:21:28 +08:00
'animal_name' => $params['animal_name'],
2024-01-10 15:48:12 +08:00
'fence_house_name' => $params['fence_house_name'],
'animal_type' => $params['animal_type'],
'fence_house_type' => $params['fence_house_type'],
'capacity' => $params['capacity'],
'master' => $params['master'],
2024-01-25 10:00:59 +08:00
'master_phone' => $params['master_phone'],
2024-01-10 15:48:12 +08:00
'status' => $params['status'],
2024-01-25 13:37:49 +08:00
'pic' => $params['pic'],
2024-01-10 15:48:12 +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 2024/01/10 15:15
*/
public static function delete(array $params): bool
{
return FenceHouse::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/01/10 15:15
*/
public static function detail($params): array
{
2024-01-31 15:58:18 +08:00
$data = FenceHouse::findOrEmpty($params['id'])->toArray();
2024-01-31 16:29:50 +08:00
$product = Product::where('fence_house_id', $data['id'])->findOrEmpty()->toArray();
$data['video_url'] = '';
$data['device_id'] = 0;
if (!empty($product)) {
$deviceIds = ProductDevice::where('product_id', $product['id'])->column('device_id');
$device = Device::whereIn('id', $deviceIds)->where('type', 2)->findOrEmpty();
$data['video_url'] = $device['video_url'];
$data['device_id'] = $device['id'];
}
2024-01-31 15:58:18 +08:00
return $data;
2024-01-10 15:48:12 +08:00
}
2024-01-13 14:20:38 +08:00
public static function datas($params)
{
$queryWhere = [];
2024-01-18 11:21:28 +08:00
if (!empty($params['keyword'])) {
$queryWhere[] = ['fence_house_name', 'like', '%' . $params['keyword'] . '%'];
2024-01-13 14:20:38 +08:00
}
if (!empty($params['user_id'])) {
$farmIds = Farm::where('user_id', $params['user_id'])->column('id');
$queryWhere[] = ['farm_id', 'in', $farmIds];
}
2024-01-18 11:21:28 +08:00
$lists = FenceHouse::where($queryWhere)->order(['id' => 'desc'])->select()->toArray();
2024-01-13 14:20:38 +08:00
foreach ($lists as &$item) {
$item['fencehouseinfo'] = 'ID' . $item['id'] . ' / 名称:' . $item['fence_house_name'];
}
return $lists;
}
2024-01-10 15:48:12 +08:00
}