Industry.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace common\models;
  3. use common\behaviors\PositionBehavior;
  4. use common\behaviors\CacheInvalidateBehavior;
  5. use common\behaviors\MetaBehavior;
  6. use common\behaviors\SoftDeleteBehavior;
  7. use common\enums\StatusEnum;
  8. use common\helpers\Tree;
  9. use common\models\behaviors\CategoryBehavior;
  10. use common\models\query\CategoryQuery;
  11. use Yii;
  12. use yii\behaviors\TimestampBehavior;
  13. use yii\caching\TagDependency;
  14. use yii\db\Expression;
  15. /**
  16. * This is the model class for table "{{%category}}".
  17. *
  18. * @property int $id
  19. * @property int $pid
  20. * @property string $title
  21. * @property string $slug
  22. * @property string $description
  23. * @property integer $status
  24. * @property int $created_at
  25. * @property int $updated_at
  26. * @property string $module
  27. * @property string $cover
  28. * @property int $allow_publish
  29. * @property int $type
  30. * @property int $is_del
  31. */
  32. class Industry extends \yii\db\ActiveRecord
  33. {
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%industry}}';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['title'], 'required'],
  48. //中文没法自动生成slug,又没必要必填
  49. [
  50. 'slug', 'default', 'value' => function ($model) {
  51. return $model->title;
  52. }
  53. ],
  54. ['module', 'safe'],
  55. [['pid', 'sort', 'allow_publish', 'type', 'status'], 'integer'],
  56. ['pid', 'default', 'value' => 0],
  57. [['sort', 'is_del'], 'default', 'value' => 0],
  58. ['status', 'default', 'value' => StatusEnum::STATUS_ON],
  59. ];
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'ID',
  68. 'title' => '名称',
  69. 'slug' => '标识',
  70. 'pid' => '上级分类',
  71. 'ptitle' => '上级分类', // 非表字段,方便后台显示
  72. 'description' => '分类介绍',
  73. 'article' => '文章数', //冗余字段,方便查询
  74. 'sort' => '排序',
  75. 'module' => '支持的人才模型',
  76. 'allow_publish' => '是否允许发布内容',
  77. 'type' => '类型',
  78. 'created_at' => '创建时间',
  79. 'updated_at' => '更新时间',
  80. 'status' => Yii::t('common', '状态'),
  81. ];
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function behaviors()
  87. {
  88. return [
  89. [
  90. 'class' => SoftDeleteBehavior::className(),
  91. 'softDeleteAttributeValues' => [
  92. 'updated_at' => time(),
  93. 'is_del' => StatusEnum::STATUS_ON,
  94. 'status' => StatusEnum::STATUS_OFF,
  95. ],
  96. 'invokeDeleteEvents' => false // 不触发删除相关事件
  97. ],
  98. TimestampBehavior::className(),
  99. [
  100. 'class' => MetaBehavior::className(),
  101. ],
  102. CategoryBehavior::className(),
  103. 'positionBehavior' => [
  104. 'class' => PositionBehavior::className(),
  105. 'positionAttribute' => 'sort',
  106. 'groupAttributes' => [
  107. 'pid'
  108. ],
  109. ],
  110. [
  111. 'class' => CacheInvalidateBehavior::className(),
  112. 'tags' => [
  113. 'categoryList'
  114. ]
  115. ]
  116. ];
  117. }
  118. public function getMetaData()
  119. {
  120. $model = $this->getMetaModel();
  121. $title = $model->title ?: $this->title;
  122. $keywords = $model->keywords;
  123. $description = $model->description ?: $this->description;
  124. return [$title, $keywords, $description];
  125. }
  126. /**
  127. * 获取分类名
  128. */
  129. public function getPtitle()
  130. {
  131. return static::find()->select('title')->where(['id' => $this->pid])->scalar();
  132. }
  133. public static function lists($module = null)
  134. {
  135. $list = Yii::$app->cache->get(['categoryList', $module]);
  136. if ($list === false) {
  137. $query = static::find()->active();
  138. if ($module) {
  139. $query->andWhere(new Expression("FIND_IN_SET('{$module}', module) "));
  140. }
  141. $list = $query->asArray()->all();
  142. Yii::$app->cache->set(['categoryList', $module], $list, 0, new TagDependency(['tags' => ['categoryList']]));
  143. }
  144. return $list;
  145. }
  146. public static function listsByType($type = null)
  147. {
  148. $list = Yii::$app->cache->get(['categoryListByType', $type]);
  149. if ($list === false) {
  150. $query = static::find()->active();
  151. if (isset($type)) {
  152. $query->andWhere(['type' => $type]);
  153. }
  154. $list = $query->asArray()->all();
  155. Yii::$app->cache->set(['categoryListByType', $type], $list, 0, new TagDependency(['tags' => ['categoryListByType']]));
  156. }
  157. return $list;
  158. }
  159. public static function getDropDownList($tree = [], &$result = [], $deep = 0, $separator = '--')
  160. {
  161. $deep++;
  162. foreach ($tree as $list) {
  163. $result[$list['id']] = str_repeat($separator, $deep - 1) . $list['title'];
  164. if (isset($list['children'])) {
  165. self::getDropDownList($list['children'], $result, $deep);
  166. }
  167. }
  168. return $result;
  169. }
  170. public function getCategoryNameById($id)
  171. {
  172. $list = $this->lists();
  173. return isset($list[$id]) ? $list[$id] : null;
  174. }
  175. public static function getIdByName($name)
  176. {
  177. $list = self::lists();
  178. return array_search($name, $list);
  179. }
  180. public static function findByIdOrSlug($id)
  181. {
  182. if (intval($id) == 0) {
  183. $condition = ["slug" => $id];
  184. } else {
  185. $condition = [
  186. $id
  187. ];
  188. }
  189. return static::findOne($condition);
  190. }
  191. public static function getAllowPublishEnum()
  192. {
  193. return [
  194. '不允许',
  195. '只允许后台',
  196. '允许前后台'
  197. ];
  198. }
  199. public static function getTypeEnum()
  200. {
  201. return [
  202. '分类',
  203. '产业链',
  204. ];
  205. }
  206. public function beforeSave($insert)
  207. {
  208. if (parent::beforeSave($insert)) {
  209. if (!empty($this->module)) {
  210. $this->module = implode(',', $this->module);
  211. }
  212. return true;
  213. } else {
  214. return false;
  215. }
  216. }
  217. public function afterSave($insert, $changedAttributes)
  218. {
  219. parent::afterSave($insert, $changedAttributes);
  220. // Yii::$app->getCache()->delete('categoryList');
  221. // Yii::$app->getCache()->delete('categoryListByType');
  222. Yii::$app->cache->flush();
  223. return true;
  224. }
  225. public function afterFind()
  226. {
  227. parent::afterFind();
  228. $this->module = explode(',', $this->module);
  229. }
  230. public function renderModule($separator = ',')
  231. {
  232. return join($separator, array_map(function ($val) {
  233. return array_get(ArticleModule::getTypeEnum(), $val);
  234. }, $this->module));
  235. }
  236. /**
  237. * @inheritdoc
  238. * @return CategoryQuery the newly created [[ActiveQuery]] instance.
  239. */
  240. public static function find()
  241. {
  242. return Yii::createObject(CategoryQuery::className(), [get_called_class()]);
  243. }
  244. }