You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

179 lines
5.0 KiB

  1. <?php
  2. /**
  3. * 数据库表类 zz_news
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm LoginController.php
  13. * Create By 2017/12/19 16:48 $
  14. */
  15. namespace common\models;
  16. use yii\db\ActiveRecord;
  17. use yii\db\Exception;
  18. use yii\db\Expression;
  19. /**
  20. * 数据库表类 zz_news.
  21. * @property integer $id
  22. * @property integer $class_id
  23. * @property string $news_title
  24. * @property string $navi_content
  25. * @property string $content
  26. * @property string $create_user
  27. * @property string $create_time
  28. * @property string $update_time
  29. * @property integer $delete_flag
  30. */
  31. class ZzNews extends ActiveRecord
  32. {
  33. /**
  34. * @inheritdoc
  35. */
  36. public static function tableName()
  37. {
  38. return 'zz_news';
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['class_id', 'delete_flag'], 'integer'],
  47. [['content'], 'required'],
  48. [['content'], 'string'],
  49. [['create_time', 'update_time'], 'safe'],
  50. [['news_title'], 'string', 'max' => 100],
  51. [['navi_content'], 'string', 'max' => 255],
  52. [['create_user'], 'string', 'max' => 50],
  53. ];
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'class_id' => 'Class ID',
  63. 'news_title' => 'News Title',
  64. 'navi_content' => 'Navi Content',
  65. 'content' => 'Content',
  66. 'create_user' => 'Create User',
  67. 'create_time' => 'Create Time',
  68. 'update_time' => 'Update Time',
  69. 'delete_flag' => 'Delete Flag',
  70. ];
  71. }
  72. /**
  73. * Function Description:获取新闻列表
  74. * Function Name: GetNewsList
  75. * @param $param
  76. *
  77. * @return array
  78. *
  79. * @author 娄梦宁
  80. */
  81. public function GetNewsList($param){
  82. $news_title=$param['news_title'];
  83. $limit=$param['page_size'];
  84. $offset = ($param['current_page'] - 1) * $param['page_size'];
  85. $where=['and',['=','delete_flag',0]];
  86. if($news_title!=''){
  87. $where=['like','news_title',$news_title];
  88. }
  89. $select=[
  90. 'id',
  91. 'class_id'=>new Expression('case class_id when 1 then "公司新闻"
  92. when 2 then "新闻报道"
  93. when 3 then "行业动态"
  94. else ""
  95. end'),
  96. 'news_title',
  97. 'navi_content',
  98. 'create_time',
  99. ];
  100. $result=self::find()->select($select)
  101. ->from(self::tableName())
  102. ->where($where)
  103. ->offset($offset)
  104. ->limit($limit)
  105. ->asArray()
  106. ->all();
  107. $count=self::find()->from(self::tableName())
  108. ->where($where)->count();
  109. return [
  110. 'news_list'=>$result,
  111. 'page'=>[
  112. 'current_page'=>intval($param['current_page']),
  113. 'page_size'=>intval($param['page_size']),
  114. 'total_count'=>intval($count),
  115. 'total_pages'=>intval(ceil($count/$param['page_size'])),
  116. ]
  117. ];
  118. }
  119. /**
  120. * Function Description:删除一条新闻
  121. * Function Name: DelNews
  122. * @param $news_id
  123. *
  124. *
  125. * @author 娄梦宁
  126. */
  127. public function DelNews($news_id)
  128. {
  129. self::updateAll(['delete_flag'=>1],['id'=>$news_id]);
  130. }
  131. /**
  132. * Function Description:获取新闻数据
  133. * Function Name: GetNewsInfo
  134. * @param $news_id
  135. *
  136. * @return array|null|ActiveRecord
  137. *
  138. * @author 娄梦宁
  139. */
  140. public function GetNewsInfo($news_id)
  141. {
  142. $select=[
  143. 'news_id'=>'id',
  144. 'news_title',
  145. 'navi_content',
  146. 'content',
  147. 'class_id'
  148. ];
  149. $result=self::find()->select($select)->from(self::tableName())->where(['id'=>$news_id])->asArray()->one();
  150. return $result;
  151. }
  152. /**
  153. * Function Description:保存新闻,根据id是否传判断新增或修改
  154. * Function Name: SaveNews
  155. * @param $params
  156. *
  157. *
  158. * @author 娄梦宁
  159. */
  160. public function SaveNews($params)
  161. {
  162. $model=clone $this;
  163. if(isset($params['id'])){
  164. $model=self::findOne($params['id']);
  165. }
  166. $model->attributes=$params;
  167. $model->save();
  168. }
  169. }