You've already forked qlg.tsgz.moe
							
							
		
			
				
	
	
		
			407 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			407 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
| <?php
 | |
| 
 | |
| namespace wstmart\admin\model;
 | |
| 
 | |
| use think\Db;
 | |
| 
 | |
| /**
 | |
| 
 | |
|  * ============================================================================
 | |
| 
 | |
|  * 品牌业务处理
 | |
| 
 | |
|  */
 | |
| 
 | |
| class Brands extends Base{
 | |
| 
 | |
| 	/**
 | |
| 
 | |
| 	 * 分页
 | |
| 
 | |
| 	 */
 | |
| 
 | |
| 	public function pageQuery(){
 | |
| 
 | |
| 		$key = input('key');
 | |
| 
 | |
| 		$id = input('id/d');
 | |
| 
 | |
| 		$where = [];
 | |
| 
 | |
| 		$where['b.dataFlag'] = 1;
 | |
| 
 | |
| 		if($key!='')$where['b.brandName'] = ['like','%'.$key.'%'];
 | |
| 
 | |
| 		if($id>0)$where['gcb.catId'] = $id;
 | |
| 
 | |
| 		$total = Db::name('brands')->alias('b');
 | |
| 
 | |
| 		if($id>0){ 
 | |
| 
 | |
| 		    $total->join('__CAT_BRANDS__ gcb','b.brandId = gcb.brandId','left');
 | |
| 
 | |
| 		}
 | |
| 
 | |
| 		$page = $total->where($where)
 | |
| 
 | |
| 		->join('shops s','s.shopId=b.shopId','left')
 | |
| 
 | |
| 		->join('cat_brands cb','cb.brandId=b.brandId','left')
 | |
| 
 | |
| 		->join('goods_cats gc','gc.catId=cb.catId','left')
 | |
| 
 | |
| 		->where('b.dataFlag=1 and s.dataFlag=1 and s.shopStatus=1')
 | |
| 
 | |
| 		->field('b.brandId,b.brandName,b.brandImg,b.brandDesc,b.sortNo,s.shopName,gc.catName')
 | |
| 
 | |
| 		->order('b.brandId', 'desc')
 | |
| 
 | |
| 		->group('b.brandId')
 | |
| 
 | |
| 		->paginate(input('post.limit/d'))->toArray();
 | |
| 
 | |
| 		if(count($page['Rows'])>0){
 | |
| 
 | |
| 			foreach ($page['Rows'] as $key => $v){
 | |
| 
 | |
| 				$page['Rows'][$key]['brandDesc'] = strip_tags(htmlspecialchars_decode($v['brandDesc']));
 | |
| 
 | |
| 			}
 | |
| 
 | |
| 		}
 | |
| 
 | |
| 		return $page;
 | |
| 
 | |
| 	}	
 | |
| 
 | |
| 	
 | |
| 
 | |
| 	/**
 | |
| 
 | |
| 	 * 获取指定对象
 | |
| 
 | |
| 	 */
 | |
| 
 | |
| 	public function getById($id){
 | |
| 
 | |
| 		$result = $this->where(['brandId'=>$id])->find();
 | |
| 
 | |
| 		//获取关联的分类
 | |
| 
 | |
| 		$result['catIds'] = Db::name('cat_brands')->where(['brandId'=>$id])->column('catId');
 | |
| 
 | |
| 		return $result;
 | |
| 
 | |
| 	}
 | |
| 
 | |
| 	
 | |
| 
 | |
| 	/**
 | |
| 
 | |
| 	 * 新增
 | |
| 
 | |
| 	 */
 | |
| 
 | |
| 	public function add(){
 | |
| 
 | |
| 		$data = input('post.');
 | |
| 
 | |
| 		$shop=db('shops')->where(['shopId'=>$data['shopId'],'dataFlag'=>1,'shopStatus'=>1])->find();
 | |
| 
 | |
| 		if(!$shop) return WSTReturn('无效的店铺,请重新输入');
 | |
| 
 | |
| 		WSTUnset($data,'brandId,dataFlag');
 | |
| 
 | |
| 		$data['createTime'] = date('Y-m-d H:i:s');
 | |
| 
 | |
| 		$idsStr = explode(',',$data['catId']);
 | |
| 
 | |
| 		if($idsStr!=''){
 | |
| 
 | |
| 			foreach ($idsStr as $v){
 | |
| 
 | |
| 				if((int)$v>0)$ids[] = (int)$v;
 | |
| 
 | |
| 			}
 | |
| 
 | |
| 		}
 | |
| 
 | |
| 		Db::startTrans();
 | |
| 
 | |
|         try{
 | |
| 
 | |
| 			$result = $this->validate('Brands.add')->allowField(true)->save($data);
 | |
| 
 | |
| 			if(false !== $result){
 | |
| 
 | |
| 				WSTClearAllCache();
 | |
| 
 | |
| 				//启用上传图片
 | |
| 
 | |
| 			    WSTUseImages(1, $this->brandId, $data['brandImg']);
 | |
| 
 | |
| 				//商品描述图片
 | |
| 
 | |
| 				WSTEditorImageRocord(1, $this->brandId, '',$data['brandDesc']);
 | |
| 
 | |
| 				foreach ($ids as $key =>$v){
 | |
| 
 | |
| 					$d = array();
 | |
| 
 | |
| 					$d['catId'] = $v;
 | |
| 
 | |
| 					$d['brandId'] = $this->brandId;
 | |
| 
 | |
| 					Db::name('cat_brands')->insert($d);
 | |
| 
 | |
| 				}
 | |
| 
 | |
| 				Db::commit();
 | |
| 
 | |
| 				return WSTReturn("新增成功", 1);
 | |
| 
 | |
| 			}
 | |
| 
 | |
|         }catch (\Exception $e) {
 | |
| 
 | |
|             Db::rollback();errLog($e);
 | |
| 
 | |
|         }
 | |
| 
 | |
|         return WSTReturn('新增失败',-1);
 | |
| 
 | |
| 	}
 | |
| 
 | |
| 	
 | |
| 
 | |
| 	/**
 | |
| 
 | |
| 	 * 编辑
 | |
| 
 | |
| 	 */
 | |
| 
 | |
| 	public function edit(){
 | |
| 
 | |
| 		$brandId = input('post.id/d');
 | |
| 
 | |
| 		$data = input('post.');
 | |
| 
 | |
| 		$idsStr = explode(',',$data['catId']);
 | |
| 
 | |
| 		if($idsStr!=''){
 | |
| 
 | |
| 			foreach ($idsStr as $v){
 | |
| 
 | |
| 				if((int)$v>0)$ids[] = (int)$v;
 | |
| 
 | |
| 			}
 | |
| 
 | |
| 		}
 | |
| 
 | |
| 		$filter = array();
 | |
| 
 | |
| 		//获取品牌的关联分类
 | |
| 
 | |
| 		$catBrands = Db::name('cat_brands')->where(['brandId'=>$brandId])->select();
 | |
| 
 | |
| 		foreach ($catBrands as $key =>$v){
 | |
| 
 | |
| 			if(!in_array($v['catId'],$ids))$filter[] = $v['catId'];
 | |
| 
 | |
| 		}
 | |
| 
 | |
| 		Db::startTrans();
 | |
| 
 | |
|         try{
 | |
| 
 | |
| 			WSTUseImages(1, $brandId, $data['brandImg'], 'brands', 'brandImg');
 | |
| 
 | |
| 			// 品牌描述图片
 | |
| 
 | |
| 			$desc = $this->where('brandId',$brandId)->value('brandDesc');
 | |
| 
 | |
| 			WSTEditorImageRocord(1, $brandId, $desc, $data['brandDesc']);			
 | |
| 
 | |
| 			$result = $this->validate('Brands.edit')->allowField(['brandName','brandImg','brandDesc','sortNo','shopId'])->save(input('post.'),['brandId'=>$brandId]);//添加shopId mark cheng 20180207
 | |
| 
 | |
| 			if(false !== $result){
 | |
| 
 | |
| 				WSTClearAllCache();
 | |
| 
 | |
| 				foreach ($catBrands as $key =>$v){
 | |
| 
 | |
| 					Db::name('cat_brands')->where('brandId',$brandId)->delete();
 | |
| 
 | |
| 				}
 | |
| 
 | |
| 				foreach ($ids as $key =>$v){
 | |
| 
 | |
| 					$d = array();
 | |
| 
 | |
| 					$d['catId'] = $v;
 | |
| 
 | |
| 					$d['brandId'] = $brandId;
 | |
| 
 | |
| 					Db::name('cat_brands')->insert($d);
 | |
| 
 | |
| 				}
 | |
| 
 | |
| 				Db::commit();
 | |
| 
 | |
| 				return WSTReturn("修改成功", 1);
 | |
| 
 | |
| 			}
 | |
| 
 | |
|         }catch (\Exception $e) {
 | |
| 
 | |
|             Db::rollback();errLog($e);
 | |
| 
 | |
|         }
 | |
| 
 | |
|         return WSTReturn('修改失败',-1);
 | |
| 
 | |
| 	}
 | |
| 
 | |
| 	
 | |
| 
 | |
| 	/**
 | |
| 
 | |
| 	 * 删除
 | |
| 
 | |
| 	 */
 | |
| 
 | |
| 	public function del(){
 | |
| 
 | |
| 		$id = input('post.id/d');
 | |
| 
 | |
| 		$data = [];
 | |
| 
 | |
| 		$data['dataFlag'] = -1;
 | |
| 
 | |
| 		Db::startTrans();
 | |
| 
 | |
|         try{
 | |
| 
 | |
| 			$result = $this->where(['brandId'=>$id])->update($data);
 | |
| 
 | |
| 		    WSTUnuseImage('brands','brandImg',$id);
 | |
| 
 | |
| 			// 品牌描述图片
 | |
| 
 | |
| 			$desc = $this->where('brandId',$id)->value('brandDesc');
 | |
| 
 | |
| 			WSTEditorImageRocord(1, $id, $desc,'');
 | |
| 
 | |
| 			if(false !== $result){
 | |
| 
 | |
| 				WSTClearAllCache();
 | |
| 
 | |
| 				//删除推荐品牌
 | |
| 
 | |
| 				Db::name('recommends')->where(['dataSrc'=>2,'dataId'=>$id])->delete();
 | |
| 
 | |
| 				//删除品牌和分类的关系
 | |
| 
 | |
| 				Db::name('cat_brands')->where(['brandId'=>$id])->delete();
 | |
| 
 | |
| 				Db::commit();
 | |
| 
 | |
| 				return WSTReturn("删除成功", 1);
 | |
| 
 | |
| 			}
 | |
| 
 | |
|         }catch (\Exception $e) {
 | |
| 
 | |
|             Db::rollback();errLog($e);
 | |
| 
 | |
|         }
 | |
| 
 | |
|         return WSTReturn('删除失败',-1);
 | |
| 
 | |
| 	}
 | |
| 
 | |
| 	
 | |
| 
 | |
| 	/**
 | |
| 
 | |
| 	 * 获取品牌
 | |
| 
 | |
| 	 */
 | |
| 
 | |
| 	public function searchBrands(){
 | |
| 
 | |
| 		$goodsCatatId = (int)input('post.goodsCatId');
 | |
| 
 | |
| 		if($goodsCatatId<=0)return [];
 | |
| 
 | |
| 		$key = input('post.key');
 | |
| 
 | |
| 		$where = [];
 | |
| 
 | |
| 		$where['dataFlag'] = 1;
 | |
| 
 | |
| 		$where['catId'] = $goodsCatatId;
 | |
| 
 | |
| 		if($key!='')$where['brandName'] = ['like','%'.$key.'%'];
 | |
| 
 | |
| 		return $this->alias('s')->join('__CAT_BRANDS__ cb','s.brandId=cb.brandId','inner')
 | |
| 
 | |
| 		            ->where($where)->field('brandName,s.brandId')->select();
 | |
| 
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 
 | |
| 	 * 排序字母
 | |
| 
 | |
| 	 */
 | |
| 
 | |
| 	public function letterObtain(){
 | |
| 
 | |
| 		$areaName =  input('code');
 | |
| 
 | |
| 		if($areaName =='')return WSTReturn("", 1);
 | |
| 
 | |
| 		$areaName = WSTGetFirstCharter($areaName);
 | |
| 
 | |
| 		if($areaName){
 | |
| 
 | |
| 			return WSTReturn($areaName, 1);
 | |
| 
 | |
| 		}else{
 | |
| 
 | |
| 			return WSTReturn("", 1);
 | |
| 
 | |
| 		}
 | |
| 
 | |
| 	}
 | |
| 
 | |
| 
 | |
| 
 | |
| 	/**
 | |
| 
 | |
| 	* 修改品牌排序
 | |
| 
 | |
| 	*/
 | |
| 
 | |
| 	public function changeSort(){
 | |
| 
 | |
| 		$id = (int)input('id');
 | |
| 
 | |
| 		$sortNo = (int)input('sortNo');
 | |
| 
 | |
| 		$result = $this->setField(['brandId'=>$id,'sortNo'=>$sortNo]);
 | |
| 
 | |
| 		if(false !== $result){
 | |
| 
 | |
|         	return WSTReturn("操作成功", 1);
 | |
| 
 | |
|         }else{
 | |
| 
 | |
|         	return WSTReturn($this->getError(),-1);
 | |
| 
 | |
|         }
 | |
| 
 | |
| 	}
 | |
| 
 | |
| } |