| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- namespace app\backend\modules\goods\models;
- /**
- * Created by PhpStorm.
- * Author: 芸众商城 www.yunzshop.com
- * Date: 2017/2/22
- * Time: 下午2:24
- */
- use app\backend\modules\goods\models\Category as CategoryModel;
- use app\common\modules\category\CategoryGroup;
- use Illuminate\Validation\Rule;
- use Illuminate\Support\Facades\DB;
- class Category extends \app\common\models\Category
- {
- static protected $needLog = true;
- /**
- * @return mixed
- */
- public static function getAllCategory()
- {
- return self::uniacid()->select(['id','name','parent_id','level'])
- ->orderBy('id', 'asc')
- ->where('plugin_id',0)
- ->get();
- }
- public static function getAllCategorys()
- {
- return self::uniacid()
- ->orderBy('id', 'asc')
- ->where('plugin_id',0);
- }
- /**
- * @return mixed
- */
- public static function getAllCategoryGroup()
- {
- $categorys = self::getAllCategory();
- $categoryMenus['parent'] = $categoryMenus['children'] = [];
- foreach ($categorys as $category) {
- !empty($category['parent_id']) ?
- $categoryMenus['children'][$category['parent_id']][] = $category :
- $categoryMenus['parent'][$category['id']] = $category;
- }
- return $categoryMenus;
- }
- public static function getAllCategoryGroupArray($pluginId = 0)
- {
- $models = CategoryModel::uniacid()->getQuery()->select(['id', 'name', 'enabled', 'parent_id'])->where(['plugin_id' => $pluginId ,'deleted_at' => null])->orderBy('id', 'asc')->get();
- $categoryGroup = new CategoryGroup($models);
- $categorys = (new \app\common\modules\category\Category(['id' => 0],$categoryGroup))->getChildrenTree();
- return $categorys;
- }
- /**
- * @param $id
- * @return \Illuminate\Database\Eloquent\Model|null|static
- */
- public static function getCategory($id)
- {
- return self::find($id);
- }
- /**
- * @param $id
- * @return mixed
- */
- public static function daletedCategory($id)
- {
- return self::where('id', $id)
- ->orWhere('parent_id', $id)
- ->delete();
- }
- public static function deletedAllCategory($id)
- {
- $res = self::with([
- 'hasManyChildren' => function ($query) use ($id) {
- return $query->select(['*'])
- ->with(['hasManyChildren' => function ($query) use ($id) {
- return $query->select(['*']);
- }]);
- }])
- ->where('id', $id)
- ->first();
- if (!is_null($res)) {
- if (!$res->hasManyChildren->isEmpty()) {
- foreach ($res->hasManyChildren as $coll) {
- if (!$coll->hasManyChildren->isEmpty()) {
- foreach ($coll->hasManyChildren as $rows) {
- $rows->delete();
- }
- }
- $coll->delete();
- }
- }
- return $res->delete();
- }
- }
- /**
- * 定义字段名
- * 可使
- * @return array
- */
- public function atributeNames()
- {
- return [
- 'name' => '分类名称',
- 'display_order' => '排序',
- ];
- }
- /**
- * 字段规则
- * @return array
- */
- public function rules()
- {
- $rule = Rule::unique($this->table);
- return [
- 'name' => ['required', $rule->ignore($this->id)
- ->where('uniacid', \YunShop::app()->uniacid)
- ->where('parent_id', $this->parent_id)
- ->where('plugin_id', 0)
- ->where('deleted_at', null)],
- 'display_order' => ['required','integer'],
- ];
- }
- /**
- * @param $keyword
- * @return mixed
- */
- public static function getCategorysByName($keyword)
- {
- return static::uniacid()->select('id', 'name', 'thumb')
- ->where('name', 'like', '%' . $keyword . '%')
- ->get();
- }
- /**
- * @param $keyword
- * @return mixed
- */
- public static function getNotOneCategorysByName($keyword)
- {
- return static::uniacid()->select('id', 'name', 'thumb')
- ->where('parent_id', '<>', 0)
- ->where('name', 'like', '%' . $keyword . '%')
- ->get();
- }
- public static function getMallCategorysByName($keyword)
- {
- return static::uniacid()->select('id', 'name', 'thumb')
- ->where('parent_id', '<>', 0)
- ->where('plugin_id',0)
- ->where('name', 'like', '%' . $keyword . '%')
- ->get();
- }
- //根据商品分类ID获取分类名称
- public static function getCategoryNameByIds($categoryIds){
- if(empty($categoryIds))
- {
- return '';
- }
- if(is_array($categoryIds)){
- $res = static::uniacid()
- ->select('name')
- ->whereIn('id', $categoryIds)
- ->orderByRaw(DB::raw("FIELD(id, ".implode(',', $categoryIds).')')) //必须按照categoryIds的顺序输出分类名称
- ->get()
- ->map(function($goodname){ //遍历
- return $goodname['name'];
- })
- ->toArray();
- } else{
- $res = static::uniacid()
- ->select('name')
- ->where('id', '=', $categoryIds)
- ->first();
- }
- return $res;
- }
- /**
- * 一级菜单
- *
- * @return mixed
- */
- public function getCategoryFirstLevel()
- {
- return self::uniacid()
- ->where('level', 1)
- ->orderBy('id', 'asc')
- ->get();
- }
- /**
- * 二级菜单
- *
- * @return mixed
- */
- public function getCategorySecondLevel()
- {
- return self::uniacid()
- ->where('level', 2)
- ->orderBy('id', 'asc')
- ->get();
- }
- /**
- * 三级菜单
- *
- * @return mixed
- */
- public function getCategoryThirdLevel()
- {
- return self::uniacid()
- ->where('level', 3)
- ->orderBy('id', 'asc')
- ->get();
- }
- public function getCategoryData()
- {
- $level = \Setting::get('shop.category.cat_level') == 2 ? 3 : null;
- $data = self::uniacid()
- ->select('id', 'name', 'parent_id')
- ->where('level' ,'!=' , $level) //根据商城后台设置判断要不要显示三级分类
- ->get();
- $data->map(function ($item){
- if($item['parent_id'] == 0){
- unset($item['parent_id']);
- }
- $item ['href'] = yzAppFullUrl('catelist/' . $item['id']);
- });
- return $data;
- }
- public function getCategorySmallData()
- {
- $level = \Setting::get('shop.category.cat_level') == 2 ? 3 : null;
- $data = self::uniacid()
- ->select('id', 'name', 'parent_id')
- ->where('level' ,'!=' , $level) //根据商城后台设置判断要不要显示三级分类
- ->get();
- $data->map(function ($item){
- if($item['parent_id'] == 0){
- unset($item['parent_id']);
- }
- $item ['href'] = '/packageB/member/category/catelist/catelist?id='. $item['id'];
- });
- return $data;
- }
- public function parentIdGetCategorys($level,$parent_id,$pluginId=0)
- {
- return CategoryModel::uniacid()
- ->getQuery()
- ->select(['id', 'name', 'enabled', 'parent_id'])
- ->where('plugin_id',$pluginId)
- ->where('deleted_at', null)
- ->where('level', $level)
- ->where('parent_id', $parent_id)
- ->orderBy('id', 'asc');
- }
- public function parentGetCategorys($pluginId=0)
- {
- return CategoryModel::uniacid()
- ->getQuery()
- ->select(['id', 'name', 'enabled', 'parent_id'])
- ->where('plugin_id',$pluginId)
- ->where('deleted_at', null)
- ->where('level', 1)
- ->orderBy('id', 'asc');
- }
- public function getCateOrderByLevel($ids){
- $list = $this->orderExportCacheAllCategory()->whereIn('id', explode(',',$ids))->sortBy('level')->pluck('name')->all();
- $list = array_pad($list,3,'');
- return $list;
- // $list=self::uniacid()->withTrashed()->whereIn('id',explode(',',$ids))->orderBy('level','asc')->pluck('name')->toArray();
- // if(count($list)==2){
- // array_push($list,'');
- // }else if(count($list)==1){
- // array_push($list,'','');
- // }
- //
- // if (empty($list)) {
- // array_push($list,'','','');
- // }
- return $list;
- }
- protected $cacheAllCategory;
- /**
- * 缓存商品分类减少订单导出时sql查询次数
- * @return \app\framework\Database\Eloquent\Collection
- */
- public function orderExportCacheAllCategory()
- {
- if (!isset($this->cacheAllCategory)) {
- $this->cacheAllCategory = self::select('id', 'name', 'level')->uniacid()->withTrashed()->get();
- }
- return $this->cacheAllCategory;
- }
- }
|