ArticleCategory.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace app\models\article;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\services\UtilService as Util;
  6. use app\models\article\Article as ArticleModel;
  7. use PDOStatement;
  8. use think\Collection;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. /**
  13. * TODO 文章分类Model
  14. * Class ArticleCategory
  15. * @package app\models\article
  16. */
  17. class ArticleCategory extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'article_category';
  29. use ModelTrait;
  30. /**
  31. * TODO 获取文章分类
  32. * @return false|PDOStatement|string|Collection
  33. * @throws DataNotFoundException
  34. * @throws ModelNotFoundException
  35. * @throws DbException
  36. */
  37. public static function getArticleCategory($mer_id)
  38. {
  39. return self::merSet($mer_id)->where('hidden', 0)->where('is_del', 0)->where('status', 1)->where('pid', 0)->order('sort DESC')->field('id,title')->select();
  40. }
  41. /**
  42. * TODO 获取分类字段
  43. * @param $id $id 编号
  44. * @param string $field $field 字段
  45. * @return mixed|string
  46. */
  47. public static function getArticleCategoryField($id, $field = 'title')
  48. {
  49. if (!$id) return '';
  50. return self::where('id', $id)->value($field);
  51. }
  52. /**
  53. * @param $cid
  54. * @param $first
  55. * @param $limit
  56. * @param string $field
  57. * @return Collection
  58. * @throws DataNotFoundException
  59. * @throws ModelNotFoundException
  60. * @throws \think\exception\DbException
  61. */
  62. public static function cidByArticleList($cid, $first, $limit, $field = '*')
  63. {
  64. $model = new Article();
  65. if ($cid) $model->where('cid', $cid);
  66. return $model->field($field)->where('status', 1)->where('hide', 0)->order('sort DESC,add_time DESC')->limit($first, $limit)->select();
  67. }
  68. //后台模型
  69. /**
  70. * TODO 获取文章分类
  71. * @return array
  72. * @throws DataNotFoundException
  73. * @throws ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. public static function getArticleCategoryList($mer_id = '')
  77. {
  78. $list = self::merSet($mer_id)->where('is_del', 0)->order('sort desc,id desc')->select();
  79. if ($list) return $list->toArray();
  80. return [];
  81. }
  82. /**
  83. * TODO 获取文章分类信息
  84. * @param $id
  85. * @param string $field
  86. * @return mixed
  87. */
  88. public static function getArticleCategoryInfo($id, $field = 'title')
  89. {
  90. $model = new self;
  91. if ($id) $model = $model->where('id', $id);
  92. $model = $model->where('is_del', 0);
  93. $model = $model->where('status', 1);
  94. return $model->column($field, 'id');
  95. }
  96. /**
  97. * 分级排序列表
  98. * @param null $model
  99. * @return array
  100. */
  101. public static function getTierList($model = null)
  102. {
  103. if ($model === null) $model = new self();
  104. return sort_list_tier($model->where('is_del', 0)->where('status', 1)->select()->toArray());
  105. }
  106. /**
  107. * 获取系统分页数据 分类
  108. * @param array $where
  109. * @return array
  110. */
  111. public static function systemPage($where = array())
  112. {
  113. $model = new self;
  114. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  115. if ($where['status'] !== '') $model = $model->where('status', $where['status']);
  116. if ($where['mer_id'] !== '') $model = $model->where('mer_id', $where['mer_id']);
  117. $model = $model->where('is_del', 0);
  118. $model = $model->where('hidden', 0);
  119. $model = $model->order('sort desc,id desc');
  120. // $model = $model->where('pid', 0);
  121. $count = $model->count();
  122. $list = $model->page((int)$where['page'], (int)$where['limit'])->select()->toArray();
  123. return compact('count', 'list');
  124. }
  125. /**
  126. * 格式化分类
  127. * @param $menusList
  128. * @param int $pid
  129. * @param array $navList
  130. * @return array
  131. */
  132. public static function tidyTree($menusList, $pid = 0, $navList = [])
  133. {
  134. foreach ($menusList as $k => $menu) {
  135. // $menu = $menu->getData();
  136. // $menu['title']=$menu['name'];
  137. if ($menu['pid'] == $pid) {
  138. unset($menusList[$k]);
  139. $menu['children'] = self::tidyTree($menusList, $menu['id']);
  140. if ($menu['children']) $menu['expand'] = true;
  141. $navList[] = $menu;
  142. }
  143. }
  144. return $navList;
  145. }
  146. /**
  147. * 删除分类
  148. * @param $id
  149. * @return bool
  150. */
  151. public static function delArticleCategory($id)
  152. {
  153. if (count(self::getArticle($id, '*')) > 0)
  154. return self::setErrorInfo('请先删除改分类下的文章!');
  155. return self::edit(['is_del' => 1], $id, 'id');
  156. }
  157. /**
  158. * 获取分类底下的文章
  159. * id 分类表中的分类id
  160. * return array
  161. * */
  162. public static function getArticle($id, $field)
  163. {
  164. $res = ArticleModel::where('status', 1)->where('hide', 0)->column($field, 'id');
  165. $new_res = array();
  166. foreach ($res as $k => $v) {
  167. $cid_arr = explode(',', $v['cid']);
  168. if (in_array($id, $cid_arr)) {
  169. $new_res[$k] = $res[$k];
  170. }
  171. }
  172. return $new_res;
  173. }
  174. public static function getSonCate($arr, $where)
  175. {
  176. $model = new self;
  177. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  178. if ($where['status'] !== '') $model = $model->where('status', $where['status']);
  179. $model = $model->where('is_del', 0);
  180. $model = $model->where('hidden', 0);
  181. foreach ($arr as $key => $value) {
  182. $son_class = $model->where('pid', $value['id'])->select()->toArray();
  183. if ($son_class) array_splice($arr, $key, 0, $son_class);
  184. }
  185. return $arr;
  186. }
  187. }