171 lines
5.4 KiB
PHP
171 lines
5.4 KiB
PHP
<?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\model\land\Product;
|
||
use app\common\logic\BaseLogic;
|
||
use think\facade\Db;
|
||
|
||
|
||
/**
|
||
* Product逻辑
|
||
* Class ProductLogic
|
||
* @package app\adminapi\logic\land
|
||
*/
|
||
class ProductLogic extends BaseLogic
|
||
{
|
||
|
||
|
||
/**
|
||
* @notes 添加
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2023/11/25 16:16
|
||
*/
|
||
public static function add(array $params): bool
|
||
{
|
||
$userId = (request()->adminInfo)['user_id'];
|
||
if (!empty($params['land_id'])) {
|
||
$userId = Db::name('land')->where('id', $params['land_id'])->value('user_id');
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
$product = Product::create([
|
||
'user_id' => $userId,
|
||
'code' => $params['code'],
|
||
'name' => $params['name'],
|
||
'status' => $params['status'],
|
||
]);
|
||
if (!empty($params['land_id'])) {
|
||
Db::name('land_product')->insert([
|
||
'land_id' => $params['land_id'],
|
||
'product_id' => $product['id'],
|
||
'create_time' => time(),
|
||
'update_time' => time()
|
||
]);
|
||
}
|
||
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/25 16:16
|
||
*/
|
||
public static function edit(array $params): bool
|
||
{
|
||
$userId = (request()->adminInfo)['user_id'];
|
||
if (!empty($params['land_id'])) {
|
||
$userId = Db::name('land')->where('id', $params['land_id'])->value('user_id');
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
Product::where('id', $params['id'])->update([
|
||
'user_id' => $userId,
|
||
'code' => $params['code'],
|
||
'name' => $params['name'],
|
||
'status' => $params['status'],
|
||
]);
|
||
Db::name('land_product')->where('product_id', $params['id'])->delete();
|
||
if (!empty($params['land_id'])) {
|
||
Db::name('land_product')->insert([
|
||
'land_id' => $params['land_id'],
|
||
'product_id' => $params['id'],
|
||
'create_time' => time(),
|
||
'update_time' => time()
|
||
]);
|
||
}
|
||
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/25 16:16
|
||
*/
|
||
public static function delete(array $params): bool
|
||
{
|
||
Db::name('land_product')->where('product_id', $params['id'])->delete();
|
||
Db::name('monitor_data')->where('product_id', $params['id'])->delete();
|
||
return Product::destroy($params['id']);
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取详情
|
||
* @param $params
|
||
* @return array
|
||
* @author likeadmin
|
||
* @date 2023/11/25 16:16
|
||
*/
|
||
public static function detail($params): array
|
||
{
|
||
return Product::findOrEmpty($params['id'])->toArray();
|
||
}
|
||
|
||
public static function bind($params): bool
|
||
{
|
||
$userId = (request()->adminInfo)['user_id'];
|
||
if (!empty($params['id'])) {
|
||
$userId = Db::name('product')->where('id', $params['id'])->value('user_id');
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
Db::name('product_device')->whereIn('device_id', $params['device_id'])->delete();
|
||
Db::name('product_device')->where('product_id', $params['id'])->delete();
|
||
Db::name('device')->whereIn('id', $params['device_id'])->update([
|
||
'user_id' => $userId
|
||
]);
|
||
$deviceList = Db::name('device')->whereIn('id', $params['device_id'])->select()->toArray();
|
||
$insertData = [];
|
||
foreach($deviceList as $d) {
|
||
$insertData[] = [
|
||
'product_id' => $params['id'],
|
||
'device_id' => $d['id'],
|
||
'device_type' => $d['type'],
|
||
'create_time' => time(),
|
||
'update_time' => time()
|
||
];
|
||
}
|
||
Db::name('product_device')->insertAll($insertData);
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
} |