123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace app\admin\controller;
- use app\user\model\AccountItem;use think\App;use think\facade\Validate;
- class Brand extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model=new \app\admin\model\Brand();
- }
- /**
- * 列出品牌管理列表
- *
- * 根据请求参数分页查询品牌管理信息,支持根据品牌名称、状态、创建时间范围进行过滤。
- * 返回品牌的分页列表,包含每条品牌信息的详细数据。
- *
- * @param string $param 搜索条件,包括页码、每页数量、品牌名称、状态、时间范围等。
- * @return \think\Response|\think\response\Json 返回查询结果的分页对象或JSON响应。
- * @throws \think\db\exception\DbException 如果数据库查询出错,则抛出异常。
- */
- public function list()
- {
- // 获取请求参数,包括页码、每页数量、品牌名称、状态和时间范围,并进行trim操作。
- $param = $this->request->param(['page' => 1, 'size' => 10, 'brand_name' => '', 'status' => '', "start" => "", "end" => ""], "post", "trim");
- // 定义初始查询条件,排除已删除的记录。
- $where = [["is_del", "=", 0]];
- // 如果品牌名称不为空,则添加到查询条件中,进行模糊搜索。
- if ($param['brand_name'] != "") $where[] = ["brand_name", "like", "%{$param['brand_name']}%"];
- // 如果状态不为空,则添加到查询条件中,进行精确查询。
- if ($param['status'] !== "") $where[] = ["status", "=", $param['status']];
- // 如果开始时间不为空,则添加到查询条件中,查询大于等于开始时间的记录。
- if ($param['start'] != "") $where[] = ["addtime", ">=", startTime($param['start'])];
- // 如果结束时间不为空,则添加到查询条件中,查询小于结束时间的记录。
- if ($param['end'] != "") $where[] = ["addtime", "<", endTime($param['end'])];
- // 根据查询条件进行查询,并按id降序排序,分页获取数据。
- $list = $this->model->where($where)
- ->order("id desc")
- ->paginate(["page" => $param['page'], "list_rows" => $param['size']]);
- // 遍历查询结果,补充每个品牌的创建者所属公司的名称。
- $list->each(function (&$item) {
- $userinfo = AccountItem::with(['ItemName'])->where(["account_id" => $item['createrid']])->findOrEmpty();
- $item['company_name'] = $userinfo->depart_name ?? "";
- });
- // 返回成功响应,包含品牌列表数据和总条数。
- return success("获取成功", ["list" => $list->items(), "count" => $list->total()]);
- }
- /**
- * 添加品牌
- *
- * 根据请求参数添加新的品牌信息。
- * 返回添加成功或失败的消息。
- *
- * @param string $param 添加品牌信息的请求参数,包括品牌名称、logo URL、创建者ID、创建者名称等。
- * @return \think\Response|\think\response\Json 返回添加成功或失败的消息。
- */
- public function create()
- {
- $param = $this->request->param(['brand_name' => '', 'logo_url' => '', 'is_del' => '0'], "post", "trim");
- $valid=Validate::rule([
- 'brand_name|品牌名称'=>'require|max:255|unique:\app\admin\model\Brand,brand_name^is_del',
- 'logo_url|品牌logo'=>'url'
- ]);
- if(!$valid->check($param)){
- return error($valid->getError());
- }
- $data=[
- 'brand_name'=>$param['brand_name'],
- 'logo_url'=>$param['logo_url'],
- 'createrid'=>$this->uid,
- 'creater'=>$this->uname,
- ];
- $save=$this->model->save($data);
- if($save){
- return success("添加成功");
- }else{
- return error("添加失败");
- }
- }
- /**
- * 修改品牌
- * @param string $param 修改品牌信息的请求参数,包括品牌ID、品牌名称、logo URL、状态等。
- * @return \think\Response|\think\response\Json|void
- */
- public function save()
- {
- $param = $this->request->param(['id' => '', 'brand_name' => '', 'logo_url' => '', 'is_del' => '0'], "post", "trim");
- $valid=Validate::rule([
- 'id|品牌id'=>'require|integer',
- 'brand_name|品牌名称'=>'require|max:255|unique:\app\admin\model\Brand,brand_name^is_del',
- 'logo_url|品牌logo'=>'url'
- ]);
- if(!$valid->check($param)){
- return error($valid->getError());
- }
- $info = $this->model->findOrEmpty($param['id']);
- if ($info->isEmpty()) {
- return error("未找到该品牌");
- }
- $info->brand_name = $param['brand_name'];
- $info->logo_url = $param['logo_url'];
- $save = $info->save();
- if ($save) {
- return success("修改成功");
- } else {
- return error("修改失败");
- }
- }
- /** 删除品牌
- * @param string $param 删除品牌信息的请求参数,包括品牌ID。
- * @return \think\Response|\think\response\Json|void
- */
- public function delete()
- {
- $param = $this->request->param(['id' => '', 'is_del' => '0'], "post", "trim");
- $valid=Validate::rule([
- 'id|品牌id'=>'require|integer',
- ]);
- if(!$valid->check($param)){
- return error($valid->getError());
- }
- $info = $this->model->findOrEmpty($param['id']);
- if ($info->isEmpty()) {
- return error("未找到该品牌");
- }
- if($info->is_del==1){
- return error("该品牌已被删除");
- }
- if($info->status==1){
- return error("该品牌已被使用,无法删除");
- }
- $info->is_del = 1;
- $save = $info->save();
- if ($save) {
- return success("删除成功");
- } else {
- return error("删除失败");
- }
- }
- /** 品牌状态
- * @param string $param 品牌状态的请求参数,包括品牌ID、状态。
- */
- public function status()
- {
- $param = $this->request->param(['id' => '', 'status' => '0'], "post", "trim");
- $valid=Validate::rule([
- 'id|品牌id'=>'require|number|gt:0',
- 'status|状态'=>'require|in:0,1'
- ]);
- if(!$valid->check($param)){
- return error($valid->getError());
- }
- $info = $this->model->findOrEmpty($param['id']);
- if ($info->isEmpty()) {
- return error("未找到该品牌");
- }
- if($info->status==$param['status']){
- return error("该品牌状态已为".\app\admin\model\Brand::$statusCn[$param['status']]);
- }
- $info->status = $param['status'];
- $save = $info->save();
- if($save==false)return error(\app\admin\model\Brand::$statusCn[$param['status']]."失败");
- return success(\app\admin\model\Brand::$statusCn[$param['status']]."成功");
- }
- /** 获取品牌列表
- * @param string $param 获取品牌列表的请求参数,包括每页条数、品牌名称。
- * @return \think\Response|\think\response\Json|void
- */
- public function getBrandList()
- {
- $param = $this->request->param(['size' => 100,"brand_name"=>""], "post", "trim");
- $where=[["is_del","=",0]];
- if($param['brand_name']!=''){
- $where[]=["brand_name","like","%".$param['brand_name']."%"];
- }
- $list = $this->model->where($where)->field("id,brand_name,status,length(brand_name) len_name")
- ->order("len_name asc")->limit($param['size'])->select();
- return success("获取成功", $list);
- }
- }
|