pull/1/head
parent
278b68f1b9
commit
adceb8e41f
@ -0,0 +1,61 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\common\model\server; |
||||
|
||||
use app\common\model\UploadFile; |
||||
use cores\BaseModel; |
||||
use think\db\exception\DataNotFoundException; |
||||
use think\db\exception\DbException; |
||||
use think\db\exception\ModelNotFoundException; |
||||
use think\model\relation\HasOne; |
||||
|
||||
class RecoveryCategory extends BaseModel |
||||
{ |
||||
|
||||
// 定义表名 |
||||
protected $name = 'server_recovery_category'; |
||||
|
||||
// 定义主键 |
||||
protected $pk = 'category_id'; |
||||
|
||||
/** |
||||
* 分类图片 |
||||
* @return HasOne |
||||
*/ |
||||
public function image(): HasOne |
||||
{ |
||||
return $this->hasOne(UploadFile::class, 'file_id', 'image_id'); |
||||
} |
||||
|
||||
/** |
||||
* @notes:分类详情 |
||||
* @param $where |
||||
* @param array $with |
||||
* @return static|array|null |
||||
* @author: wanghousheng |
||||
*/ |
||||
public static function detail($where, array $with = []) |
||||
{ |
||||
return static::get($where, $with); |
||||
} |
||||
|
||||
/** |
||||
* @notes:获取全部记录 |
||||
* @param array $where |
||||
* @return array |
||||
* @throws DataNotFoundException |
||||
* @throws DbException |
||||
* @throws ModelNotFoundException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function getList(array $where = []): array |
||||
{ |
||||
$where = $this->setQueryDefaultValue($where); |
||||
return $this->with(['image']) |
||||
->where($where) |
||||
->order(['sort', 'create_time']) |
||||
->select() |
||||
->toArray(); |
||||
} |
||||
} |
@ -0,0 +1,76 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\common\model\server; |
||||
|
||||
use app\common\model\UploadFile; |
||||
use cores\BaseModel; |
||||
use think\db\exception\DbException; |
||||
use think\model\concern\SoftDelete; |
||||
use think\model\relation\HasOne; |
||||
use think\Paginator; |
||||
|
||||
class ServerRecovery extends BaseModel |
||||
{ |
||||
// 定义表名 |
||||
protected $name = 'server_recovery'; |
||||
|
||||
// 定义主键 |
||||
protected $pk = 'recovery_id'; |
||||
|
||||
use SoftDelete; |
||||
|
||||
protected string $deleteTime = 'delete_time'; |
||||
protected $defaultSoftDelete = 0; |
||||
|
||||
/** |
||||
* 图片 |
||||
* @return HasOne |
||||
*/ |
||||
public function image(): HasOne |
||||
{ |
||||
return $this->hasOne(UploadFile::class, 'file_id', 'image_id') |
||||
->bind(['recovery_image' => 'preview_url']); |
||||
} |
||||
|
||||
public function category(): HasOne |
||||
{ |
||||
return $this->hasOne(RecoveryCategory::class, 'category_id', 'category_id') |
||||
->bind(['recovery_category' => 'name']); |
||||
} |
||||
|
||||
/** |
||||
* @notes:回收详情 |
||||
* @param $where |
||||
* @param array $with |
||||
* @return static|array|null |
||||
* @author: wanghousheng |
||||
*/ |
||||
public static function detail($where, array $with = []) |
||||
{ |
||||
return static::get($where, $with); |
||||
} |
||||
|
||||
/** |
||||
* @notes:获取全部记录 |
||||
* @param array $where |
||||
* @param int $listRows |
||||
* @param string $sort |
||||
* @param string $sort_type |
||||
* @return Paginator |
||||
* @throws DbException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function getList(array $where = [], int $listRows = 15, string $sort = '', string $sort_type = 'desc'): Paginator |
||||
{ |
||||
$where = $this->setQueryDefaultValue($where); |
||||
$sort_arr = ['sort' => $sort_type, 'create_time' => $sort_type]; |
||||
if ($sort) { |
||||
$sort_arr = [$sort => $sort_type, 'create_time' => $sort_type]; |
||||
} |
||||
return $this->with(['image'])->withJoin(['category' => ['category_id', 'name']]) |
||||
->where($where) |
||||
->order($sort_arr) |
||||
->paginate($listRows); |
||||
} |
||||
} |
@ -0,0 +1,198 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\store\controller; |
||||
|
||||
use app\common\enum\RecoveryStatusEnum; |
||||
use app\store\model\server\RecoveryCategory; |
||||
use app\store\model\server\ServerRecovery; |
||||
use think\db\exception\DataNotFoundException; |
||||
use think\db\exception\DbException; |
||||
use think\db\exception\ModelNotFoundException; |
||||
use think\response\Json; |
||||
|
||||
class Recovery extends Controller |
||||
{ |
||||
/** |
||||
* @notes:分类列表 |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function categoryList(): Json |
||||
{ |
||||
$name = $this->request->post('name'); |
||||
$where = []; |
||||
if (!empty($name)) { |
||||
$where[] = ['name', 'like', `%$name%`]; |
||||
} |
||||
$model = new RecoveryCategory(); |
||||
try { |
||||
$list = $model->getList($where); |
||||
} catch (DataNotFoundException|ModelNotFoundException|DbException $e) { |
||||
return $this->renderError($e->getMessage() ?: '接口异常'); |
||||
} |
||||
return $this->renderSuccess(compact('list')); |
||||
} |
||||
|
||||
/** |
||||
* @notes:添加分类 |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function addCategory(): Json |
||||
{ |
||||
$data = $this->postForm(); |
||||
if (!$data) { |
||||
return $this->renderError('缺少必要参数'); |
||||
} |
||||
$model = new RecoveryCategory(); |
||||
if ($model->add($data)) { |
||||
return $this->renderSuccess('添加成功'); |
||||
} |
||||
return $this->renderError($model->getError() ?: '添加失败'); |
||||
} |
||||
|
||||
/** |
||||
* @notes:编辑分类 |
||||
* @param int $categoryId |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function editCategory(int $categoryId): Json |
||||
{ |
||||
$data = $this->postForm(); |
||||
if (!$data) { |
||||
return $this->renderError('缺少必要参数'); |
||||
} |
||||
$model = RecoveryCategory::detail($categoryId); |
||||
if ($model->edit($data)) { |
||||
return $this->renderSuccess('编辑成功'); |
||||
} |
||||
return $this->renderError($model->getError() ?: '编辑失败'); |
||||
} |
||||
|
||||
public function deleteCategory(int $categoryId): Json |
||||
{ |
||||
$model = RecoveryCategory::detail($categoryId); |
||||
if ($model->remove()) { |
||||
return $this->renderSuccess('删除成功'); |
||||
} |
||||
return $this->renderError('删除失败'); |
||||
} |
||||
|
||||
/** |
||||
* @notes:回收列表 |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function recoveryList(): Json |
||||
{ |
||||
// 获取列表记录 |
||||
$model = new ServerRecovery(); |
||||
$recovery_name = $this->request->post('recovery_name'); |
||||
$category_id = intval($this->request->post('category_id')); |
||||
$status = intval($this->request->post('status')); |
||||
$where = []; |
||||
if ($recovery_name) { |
||||
$where[] = ['recovery.server_name', 'like', "%$recovery_name%"]; |
||||
} |
||||
if ($category_id) { |
||||
$where[] = ['recovery.category_id', '=', $category_id]; |
||||
} |
||||
if ($status) { |
||||
$where[] = ['recovery.status', '=', $status]; |
||||
} |
||||
try { |
||||
$list = $model->getList($where); |
||||
} catch (DbException $e) { |
||||
return $this->renderError($e->getMessage()); |
||||
} |
||||
return $this->renderSuccess(compact('list')); |
||||
} |
||||
|
||||
public function recoveryDetail(int $recoveryId): Json |
||||
{ |
||||
// 获取商品详情 |
||||
$model = new ServerRecovery; |
||||
$info = $model->getDetail($recoveryId); |
||||
return $this->renderSuccess(compact('info')); |
||||
} |
||||
|
||||
/** |
||||
* @notes:添加回收 |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function addRecovery(): Json |
||||
{ |
||||
$data = $this->postForm(); |
||||
if (!$data) { |
||||
return $this->renderError('缺少必要参数'); |
||||
} |
||||
$model = new ServerRecovery(); |
||||
if ($model->add($data)) { |
||||
return $this->renderSuccess('添加成功'); |
||||
} |
||||
return $this->renderError($model->getError() ?: '添加失败'); |
||||
} |
||||
|
||||
/** |
||||
* @notes:编辑回收 |
||||
* @param int $recoveryId |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function editRecovery(int $recoveryId): Json |
||||
{ |
||||
$data = $this->postForm(); |
||||
if (!$data) { |
||||
return $this->renderError('缺少必要参数'); |
||||
} |
||||
$model = ServerRecovery::detail($recoveryId); |
||||
if ($model->edit($data)) { |
||||
return $this->renderSuccess('编辑成功'); |
||||
} |
||||
return $this->renderError($model->getError() ?: '编辑失败'); |
||||
} |
||||
|
||||
/** |
||||
* @notes:订单状态 |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function orderStatus(): Json |
||||
{ |
||||
$list = array_values(RecoveryStatusEnum::data()); |
||||
return $this->renderSuccess(compact('list')); |
||||
} |
||||
|
||||
/** |
||||
* @notes:删除回收 |
||||
* @param array $recoveryId |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function deleteRecovery(array $recoveryId): Json |
||||
{ |
||||
$model = new ServerRecovery; |
||||
if ($model->remove($recoveryId)) { |
||||
return $this->renderSuccess('删除成功'); |
||||
} |
||||
return $this->renderError('删除失败'); |
||||
} |
||||
|
||||
/** |
||||
* 修改回收状态(上下架) |
||||
* @param array $recoveryIds id集 |
||||
* @param bool $state 为true表示上架 |
||||
* @return Json |
||||
*/ |
||||
public function recoveryStatus(array $recoveryIds, bool $state): Json |
||||
{ |
||||
$model = new ServerRecovery; |
||||
if (!$model->setStatus($recoveryIds, $state)) { |
||||
return $this->renderError($model->getError() ?: '操作失败'); |
||||
} |
||||
return $this->renderSuccess('操作成功'); |
||||
} |
||||
} |
@ -0,0 +1,48 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\store\model\server; |
||||
|
||||
use app\common\model\server\RecoveryCategory as BaseRecoveryCategory; |
||||
|
||||
class RecoveryCategory extends BaseRecoveryCategory |
||||
{ |
||||
/** |
||||
* @notes:新增 |
||||
* @param $data |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function add($data): bool |
||||
{ |
||||
$data['store_id'] = self::$storeId; |
||||
return $this->save($data); |
||||
} |
||||
|
||||
/** |
||||
* @notes:编辑 |
||||
* @param $data |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function edit($data): bool |
||||
{ |
||||
// 是否删除图片 |
||||
!isset($data['image_id']) && $data['image_id'] = 0; |
||||
return $this->save($data) !== false; |
||||
} |
||||
|
||||
/** |
||||
* @notes:删除 |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function remove(): bool |
||||
{ |
||||
if (!static::detail(['category_id' => $this['category_id']])) { |
||||
$this->error = '记录不存在'; |
||||
return false; |
||||
} |
||||
return $this->delete(); |
||||
} |
||||
} |
@ -0,0 +1,62 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\store\model\server; |
||||
|
||||
use app\common\model\server\ServerRecovery as BaseServerRecovery; |
||||
|
||||
class ServerRecovery extends BaseServerRecovery |
||||
{ |
||||
/** |
||||
* @notes:新增 |
||||
* @param $data |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function add($data): bool |
||||
{ |
||||
$data['store_id'] = self::$storeId; |
||||
return $this->save($data); |
||||
} |
||||
|
||||
/** |
||||
* @notes:编辑 |
||||
* @param $data |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function edit($data): bool |
||||
{ |
||||
// 是否删除图片 |
||||
!isset($data['image_id']) && $data['image_id'] = 0; |
||||
return $this->save($data) !== false; |
||||
} |
||||
|
||||
/** |
||||
* @notes:删除 |
||||
* @param array $recoveryId |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function remove(array $recoveryId): bool |
||||
{ |
||||
return static::whereIn('server_id', $recoveryId)->delete(); |
||||
} |
||||
|
||||
/** |
||||
* 修改回收状态 |
||||
* @param array $recoveryIds 商品id集 |
||||
* @param bool $state 为true表示上架 |
||||
* @return bool|false |
||||
*/ |
||||
public function setStatus(array $recoveryIds, bool $state): bool |
||||
{ |
||||
// 批量更新记录 |
||||
return static::updateBase(['status' => $state ? 1 : 2], [['recovery_id', 'in', $recoveryIds]]); |
||||
} |
||||
|
||||
public function getDetail(int $recoveryId) |
||||
{ |
||||
return static::detail($recoveryId, ['image', 'category']); |
||||
} |
||||
} |
Loading…
Reference in new issue