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

228 lines
7.5 KiB
PHP
Raw Normal View History

2023-11-25 18:13:50 +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;
2023-12-06 15:09:38 +08:00
use app\common\model\land\Land;
2023-11-25 18:13:50 +08:00
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
{
2023-12-06 10:26:33 +08:00
$root = (request()->adminInfo)['root'];
2023-11-30 12:00:50 +08:00
$userId = (request()->adminInfo)['user_id'];
2023-11-30 14:58:46 +08:00
$status = 1;
2023-12-06 10:26:33 +08:00
if ($root && !empty($params['user_id'])) {
$userId = $params['user_id'];
}
2023-11-30 12:00:50 +08:00
if (!empty($params['land_id'])) {
2023-12-06 15:09:38 +08:00
$land = Land::findOrEmpty($params['land_id'])->toArray();
$userId = $land['user_id'] ?? 0;
2023-11-30 14:58:46 +08:00
$status = 2;
2023-11-30 12:00:50 +08:00
}
2023-11-25 18:13:50 +08:00
Db::startTrans();
try {
$product = Product::create([
2023-11-30 10:05:04 +08:00
'user_id' => $userId,
2023-11-25 18:13:50 +08:00
'code' => $params['code'],
'name' => $params['name'],
2023-11-30 14:58:46 +08:00
'status' => $status,
2023-11-25 18:13:50 +08:00
]);
2023-11-30 12:00:50 +08:00
if (!empty($params['land_id'])) {
2023-12-06 15:09:38 +08:00
Db::name('land_product')->where('land_id', $params['land_id'])->delete();
2023-11-30 12:00:50 +08:00
Db::name('land_product')->insert([
'land_id' => $params['land_id'],
'product_id' => $product['id'],
'create_time' => time(),
'update_time' => time()
]);
}
2023-11-25 18:13:50 +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/25 16:16
*/
public static function edit(array $params): bool
{
2023-12-06 10:26:33 +08:00
$root = (request()->adminInfo)['root'];
2023-11-30 12:00:50 +08:00
$userId = (request()->adminInfo)['user_id'];
2023-11-30 14:58:46 +08:00
$status = 1;
2023-12-06 10:26:33 +08:00
if ($root && !empty($params['user_id'])) {
$userId = $params['user_id'];
}
2023-11-30 12:00:50 +08:00
if (!empty($params['land_id'])) {
2023-12-06 15:09:38 +08:00
$land = Land::findOrEmpty($params['land_id'])->toArray();
$userId = $land['user_id'] ?? 0;
2023-11-30 14:58:46 +08:00
$status = 2;
2023-11-30 12:00:50 +08:00
}
2023-11-25 18:13:50 +08:00
Db::startTrans();
try {
Product::where('id', $params['id'])->update([
2023-11-30 10:05:04 +08:00
'user_id' => $userId,
2023-11-25 18:13:50 +08:00
'code' => $params['code'],
'name' => $params['name'],
2023-11-30 14:58:46 +08:00
'status' => $status,
2023-11-25 18:13:50 +08:00
]);
2023-12-06 15:09:38 +08:00
if (!empty($params['land_id'])) {
Db::name('land_product')->where('land_id', $params['land_id'])->delete();
}
2023-11-25 18:13:50 +08:00
Db::name('land_product')->where('product_id', $params['id'])->delete();
2023-11-30 12:00:50 +08:00
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()
]);
}
2023-11-25 18:13:50 +08:00
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
2023-12-06 13:45:44 +08:00
public function datas($params): array
{
$userWhere['p.user_id'] = -1;
if (!empty($params['user_id'])) {
$userWhere['p.user_id'] = $params['user_id'];
}
if (!request()->adminInfo['root'] && request()->adminInfo['user_id']) {
$userWhere['p.user_id'] = request()->adminInfo['user_id'];
}
if (request()->adminInfo['root']) {
unset($userWhere['p.user_id']);
}
$queryWhere = [];
2023-12-06 14:20:28 +08:00
if (!empty($params['name'])) {
$queryWhere[] = ['p.name', 'like', '%' . $params['name'] . '%'];
2023-12-06 13:45:44 +08:00
}
2023-12-08 14:26:46 +08:00
$productIdArray = Db::name('land_product')->column('product_id');
if (!empty($params['all'])) {
$productIdArray= [];
}
2023-12-06 13:45:44 +08:00
$lists = Db::name('product')->alias('p')
2023-12-08 14:26:46 +08:00
->where($userWhere)->where($queryWhere)->whereNotIn('p.id', $productIdArray)
2023-12-06 13:45:44 +08:00
->leftJoin('user u','u.id = p.user_id')
->field('p.*')
->limit(0, 100)
->order(['p.id' => 'desc'])
->select()->toArray();
foreach ($lists as &$item) {
$item['productinfo'] = 'ID' . $item['id'] . ' / 名称:' . $item['name'];
}
return $lists;
}
2023-11-25 18:13:50 +08:00
/**
* @notes 删除
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/11/25 16:16
*/
public static function delete(array $params): bool
{
2023-11-27 15:22:18 +08:00
Db::name('land_product')->where('product_id', $params['id'])->delete();
2023-11-29 17:29:12 +08:00
Db::name('monitor_data')->where('product_id', $params['id'])->delete();
2023-11-25 18:13:50 +08:00
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();
}
2023-11-30 14:20:58 +08:00
public static function bind($params): bool
{
2023-12-06 14:20:28 +08:00
$root = (request()->adminInfo)['root'];
2023-11-30 14:20:58 +08:00
$userId = (request()->adminInfo)['user_id'];
2023-12-06 14:20:28 +08:00
if ($root && !empty($params['user_id'])) {
$userId = $params['user_id'];
2023-11-30 14:20:58 +08:00
}
2023-12-06 15:09:38 +08:00
if (!empty($params['id'])) {
$product = Product::findOrEmpty($params['id'])->toArray();
$userId = $product['user_id'] ?? 0;
}
2023-11-30 14:20:58 +08:00
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;
}
}
2023-11-25 18:13:50 +08:00
}