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();
- }
-
- public function list()
- {
-
- $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'])];
-
- $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()]);
- }
-
- 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("添加失败");
- }
- }
-
- 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("修改失败");
- }
- }
-
- 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("删除失败");
- }
- }
-
- 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']]."成功");
- }
-
- 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);
- }
- }
|