Category.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace app\backend\modules\goods\models;
  3. /**
  4. * Created by PhpStorm.
  5. * Author: 芸众商城 www.yunzshop.com
  6. * Date: 2017/2/22
  7. * Time: 下午2:24
  8. */
  9. use app\backend\modules\goods\models\Category as CategoryModel;
  10. use app\common\modules\category\CategoryGroup;
  11. use Illuminate\Validation\Rule;
  12. use Illuminate\Support\Facades\DB;
  13. class Category extends \app\common\models\Category
  14. {
  15. static protected $needLog = true;
  16. /**
  17. * @return mixed
  18. */
  19. public static function getAllCategory()
  20. {
  21. return self::uniacid()->select(['id','name','parent_id','level'])
  22. ->orderBy('id', 'asc')
  23. ->where('plugin_id',0)
  24. ->get();
  25. }
  26. public static function getAllCategorys()
  27. {
  28. return self::uniacid()
  29. ->orderBy('id', 'asc')
  30. ->where('plugin_id',0);
  31. }
  32. /**
  33. * @return mixed
  34. */
  35. public static function getAllCategoryGroup()
  36. {
  37. $categorys = self::getAllCategory();
  38. $categoryMenus['parent'] = $categoryMenus['children'] = [];
  39. foreach ($categorys as $category) {
  40. !empty($category['parent_id']) ?
  41. $categoryMenus['children'][$category['parent_id']][] = $category :
  42. $categoryMenus['parent'][$category['id']] = $category;
  43. }
  44. return $categoryMenus;
  45. }
  46. public static function getAllCategoryGroupArray($pluginId = 0)
  47. {
  48. $models = CategoryModel::uniacid()->getQuery()->select(['id', 'name', 'enabled', 'parent_id'])->where(['plugin_id' => $pluginId ,'deleted_at' => null])->orderBy('id', 'asc')->get();
  49. $categoryGroup = new CategoryGroup($models);
  50. $categorys = (new \app\common\modules\category\Category(['id' => 0],$categoryGroup))->getChildrenTree();
  51. return $categorys;
  52. }
  53. /**
  54. * @param $id
  55. * @return \Illuminate\Database\Eloquent\Model|null|static
  56. */
  57. public static function getCategory($id)
  58. {
  59. return self::find($id);
  60. }
  61. /**
  62. * @param $id
  63. * @return mixed
  64. */
  65. public static function daletedCategory($id)
  66. {
  67. return self::where('id', $id)
  68. ->orWhere('parent_id', $id)
  69. ->delete();
  70. }
  71. public static function deletedAllCategory($id)
  72. {
  73. $res = self::with([
  74. 'hasManyChildren' => function ($query) use ($id) {
  75. return $query->select(['*'])
  76. ->with(['hasManyChildren' => function ($query) use ($id) {
  77. return $query->select(['*']);
  78. }]);
  79. }])
  80. ->where('id', $id)
  81. ->first();
  82. if (!is_null($res)) {
  83. if (!$res->hasManyChildren->isEmpty()) {
  84. foreach ($res->hasManyChildren as $coll) {
  85. if (!$coll->hasManyChildren->isEmpty()) {
  86. foreach ($coll->hasManyChildren as $rows) {
  87. $rows->delete();
  88. }
  89. }
  90. $coll->delete();
  91. }
  92. }
  93. return $res->delete();
  94. }
  95. }
  96. /**
  97. * 定义字段名
  98. * 可使
  99. * @return array
  100. */
  101. public function atributeNames()
  102. {
  103. return [
  104. 'name' => '分类名称',
  105. 'display_order' => '排序',
  106. ];
  107. }
  108. /**
  109. * 字段规则
  110. * @return array
  111. */
  112. public function rules()
  113. {
  114. $rule = Rule::unique($this->table);
  115. return [
  116. 'name' => ['required', $rule->ignore($this->id)
  117. ->where('uniacid', \YunShop::app()->uniacid)
  118. ->where('parent_id', $this->parent_id)
  119. ->where('plugin_id', 0)
  120. ->where('deleted_at', null)],
  121. 'display_order' => ['required','integer'],
  122. ];
  123. }
  124. /**
  125. * @param $keyword
  126. * @return mixed
  127. */
  128. public static function getCategorysByName($keyword)
  129. {
  130. return static::uniacid()->select('id', 'name', 'thumb')
  131. ->where('name', 'like', '%' . $keyword . '%')
  132. ->get();
  133. }
  134. /**
  135. * @param $keyword
  136. * @return mixed
  137. */
  138. public static function getNotOneCategorysByName($keyword)
  139. {
  140. return static::uniacid()->select('id', 'name', 'thumb')
  141. ->where('parent_id', '<>', 0)
  142. ->where('name', 'like', '%' . $keyword . '%')
  143. ->get();
  144. }
  145. public static function getMallCategorysByName($keyword)
  146. {
  147. return static::uniacid()->select('id', 'name', 'thumb')
  148. ->where('parent_id', '<>', 0)
  149. ->where('plugin_id',0)
  150. ->where('name', 'like', '%' . $keyword . '%')
  151. ->get();
  152. }
  153. //根据商品分类ID获取分类名称
  154. public static function getCategoryNameByIds($categoryIds){
  155. if(empty($categoryIds))
  156. {
  157. return '';
  158. }
  159. if(is_array($categoryIds)){
  160. $res = static::uniacid()
  161. ->select('name')
  162. ->whereIn('id', $categoryIds)
  163. ->orderByRaw(DB::raw("FIELD(id, ".implode(',', $categoryIds).')')) //必须按照categoryIds的顺序输出分类名称
  164. ->get()
  165. ->map(function($goodname){ //遍历
  166. return $goodname['name'];
  167. })
  168. ->toArray();
  169. } else{
  170. $res = static::uniacid()
  171. ->select('name')
  172. ->where('id', '=', $categoryIds)
  173. ->first();
  174. }
  175. return $res;
  176. }
  177. /**
  178. * 一级菜单
  179. *
  180. * @return mixed
  181. */
  182. public function getCategoryFirstLevel()
  183. {
  184. return self::uniacid()
  185. ->where('level', 1)
  186. ->orderBy('id', 'asc')
  187. ->get();
  188. }
  189. /**
  190. * 二级菜单
  191. *
  192. * @return mixed
  193. */
  194. public function getCategorySecondLevel()
  195. {
  196. return self::uniacid()
  197. ->where('level', 2)
  198. ->orderBy('id', 'asc')
  199. ->get();
  200. }
  201. /**
  202. * 三级菜单
  203. *
  204. * @return mixed
  205. */
  206. public function getCategoryThirdLevel()
  207. {
  208. return self::uniacid()
  209. ->where('level', 3)
  210. ->orderBy('id', 'asc')
  211. ->get();
  212. }
  213. public function getCategoryData()
  214. {
  215. $level = \Setting::get('shop.category.cat_level') == 2 ? 3 : null;
  216. $data = self::uniacid()
  217. ->select('id', 'name', 'parent_id')
  218. ->where('level' ,'!=' , $level) //根据商城后台设置判断要不要显示三级分类
  219. ->get();
  220. $data->map(function ($item){
  221. if($item['parent_id'] == 0){
  222. unset($item['parent_id']);
  223. }
  224. $item ['href'] = yzAppFullUrl('catelist/' . $item['id']);
  225. });
  226. return $data;
  227. }
  228. public function getCategorySmallData()
  229. {
  230. $level = \Setting::get('shop.category.cat_level') == 2 ? 3 : null;
  231. $data = self::uniacid()
  232. ->select('id', 'name', 'parent_id')
  233. ->where('level' ,'!=' , $level) //根据商城后台设置判断要不要显示三级分类
  234. ->get();
  235. $data->map(function ($item){
  236. if($item['parent_id'] == 0){
  237. unset($item['parent_id']);
  238. }
  239. $item ['href'] = '/packageB/member/category/catelist/catelist?id='. $item['id'];
  240. });
  241. return $data;
  242. }
  243. public function parentIdGetCategorys($level,$parent_id,$pluginId=0)
  244. {
  245. return CategoryModel::uniacid()
  246. ->getQuery()
  247. ->select(['id', 'name', 'enabled', 'parent_id'])
  248. ->where('plugin_id',$pluginId)
  249. ->where('deleted_at', null)
  250. ->where('level', $level)
  251. ->where('parent_id', $parent_id)
  252. ->orderBy('id', 'asc');
  253. }
  254. public function parentGetCategorys($pluginId=0)
  255. {
  256. return CategoryModel::uniacid()
  257. ->getQuery()
  258. ->select(['id', 'name', 'enabled', 'parent_id'])
  259. ->where('plugin_id',$pluginId)
  260. ->where('deleted_at', null)
  261. ->where('level', 1)
  262. ->orderBy('id', 'asc');
  263. }
  264. public function getCateOrderByLevel($ids){
  265. $list = $this->orderExportCacheAllCategory()->whereIn('id', explode(',',$ids))->sortBy('level')->pluck('name')->all();
  266. $list = array_pad($list,3,'');
  267. return $list;
  268. // $list=self::uniacid()->withTrashed()->whereIn('id',explode(',',$ids))->orderBy('level','asc')->pluck('name')->toArray();
  269. // if(count($list)==2){
  270. // array_push($list,'');
  271. // }else if(count($list)==1){
  272. // array_push($list,'','');
  273. // }
  274. //
  275. // if (empty($list)) {
  276. // array_push($list,'','','');
  277. // }
  278. return $list;
  279. }
  280. protected $cacheAllCategory;
  281. /**
  282. * 缓存商品分类减少订单导出时sql查询次数
  283. * @return \app\framework\Database\Eloquent\Collection
  284. */
  285. public function orderExportCacheAllCategory()
  286. {
  287. if (!isset($this->cacheAllCategory)) {
  288. $this->cacheAllCategory = self::select('id', 'name', 'level')->uniacid()->withTrashed()->get();
  289. }
  290. return $this->cacheAllCategory;
  291. }
  292. }