new-admin-api/app/controller/admin/system/merchant/MerchantBrand.php

115 lines
3.2 KiB
PHP

<?php
namespace app\controller\admin\system\merchant;
use app\common\repositories\system\merchant\MerchantBrandRepository;
use crmeb\basic\BaseController;
use think\App;
use think\exception\ValidateException;
class MerchantBrand extends BaseController{
protected $repository;
public function __construct(App $app, MerchantBrandRepository $repository){
parent::__construct($app);
$this->repository = $repository;
}
/**
* Common: 列表获取
* Author: wu-hui
* Time: 2024/02/04 15:36
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function lst(){
[$page, $limit] = $this->getPage();
$params = $this->request->params(['title']);
$data = $this->repository->getList($params, $page, $limit);
return app('json')->success($data);
}
/**
* Common: 参数获取
* Author: wu-hui
* Time: 2024/02/04 15:57
* @return array
*/
public function checkParams():array{
$id = $this->request->param('id', 0);
$data = $this->request->params(['title']);
if(empty($data['title'])) throw new ValidateException('请输入品牌名称!');
// 查询是否存在
$isHas = (int)$this->repository
->getSearchModel(['equivalent_to_title'=>$data['title']])
->when($id > 0,function($query) use ($id){
$query->where('id','<>', $id);
})
->count();
if($isHas > 0) throw new ValidateException('品牌已经存在,请勿重复添加!');
return $data;
}
/**
* Common: 表单生成
* Author: wu-hui
* Time: 2024/02/04 16:04
* @return mixed
* @throws \FormBuilder\Exception\FormBuilderException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function editForm(){
$id = $this->request->param('id',0);
$data = formToData($this->repository->form($id));
return app('json')->success($data);
}
/**
* Common: 添加品牌
* Author: wu-hui
* Time: 2024/02/04 15:45
* @return mixed
*/
public function create(){
$data = $this->checkParams();
$this->repository->create($data);
return app('json')->success('添加成功');
}
/**
* Common: 编辑信息
* Author: wu-hui
* Time: 2024/02/04 16:01
* @param $id
* @return mixed
*/
public function update($id){
$data = $this->checkParams();
if (!$this->repository->exists($id)) return app('json')->fail('数据不存在');
$this->repository->update($id, $data);
return app('json')->success('编辑成功');
}
/**
* Common: 删除数据
* Author: wu-hui
* Time: 2024/02/04 16:09
* @param $id
* @return mixed
*/
public function delete($id){
if (!$this->repository->exists($id)) return app('json')->fail('数据不存在');
$this->repository->update($id, ['is_del'=>1]);
return app('json')->success('删除成功');
}
}