Cases.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\admin\model\cases;
  3. use app\admin\model\CategoryLangs;
  4. use app\common\model\Category;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\ModelNotFoundException;
  7. use think\Exception;
  8. use think\exception\DbException;
  9. use think\Model;
  10. use traits\model\SoftDelete;
  11. class Cases extends Model
  12. {
  13. use SoftDelete;
  14. // 表名
  15. protected $name = 'cases';
  16. // 自动写入时间戳字段
  17. protected $autoWriteTimestamp = 'int';
  18. // 定义时间戳字段名
  19. protected $createTime = 'createtime';
  20. protected $updateTime = 'updatetime';
  21. protected $deleteTime = 'deletetime';
  22. // 追加属性
  23. protected $append = [
  24. 'status_text'
  25. ];
  26. protected static function init()
  27. {
  28. self::afterInsert(function ($row) {
  29. $pk = $row->getPk();
  30. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  31. });
  32. }
  33. public function getStatusList()
  34. {
  35. return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
  36. }
  37. public function getStatusTextAttr($value, $data)
  38. {
  39. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  40. $list = $this->getStatusList();
  41. return isset($list[$value]) ? $list[$value] : '';
  42. }
  43. public static function validWhere($alias = '')
  44. {
  45. $str = '';
  46. if ($alias) {
  47. $str = $alias . '.';
  48. }
  49. return self::where($str . 'status', '1')->where($str . 'showswitch', 1);
  50. }
  51. /**
  52. * @param $where
  53. * @return array
  54. * @throws Exception
  55. * @throws DataNotFoundException
  56. * @throws ModelNotFoundException
  57. * @throws DbException
  58. */
  59. public static function getValidArticleList($where)
  60. {
  61. $model = self::validWhere();
  62. if (isset($where['title']) && $where['title'] != '') {
  63. $ids = CasesLangs::where('title|synopsis', 'like', "%{$where['title']}%")->column('cases_id');
  64. $ids2 = self::where('title|synopsis', 'like', "%{$where['title']}%")->column('id');
  65. $all_ids = array_merge($ids, $ids2);
  66. $model->where('id', 'in', $all_ids);
  67. }
  68. if (isset($where['category_id']) && $where['category_id'] != '') {
  69. $model->where('find_in_set(' . $where['category_id'] . ',category_ids)');
  70. }
  71. $count = $model->count();
  72. $model = self::validWhere();
  73. if (isset($where['title']) && $where['title'] != '') {
  74. $ids = CasesLangs::where('title|synopsis', 'like', "%{$where['title']}%")->column('cases_id');
  75. $ids2 = self::where('title|synopsis', 'like', "%{$where['title']}%")->column('id');
  76. $all_ids = array_merge($ids, $ids2);
  77. $model->where('id', 'in', $all_ids);
  78. }
  79. if (isset($where['category_id']) && $where['category_id'] != '') {
  80. $model->where('find_in_set(' . $where['category_id'] . ',category_ids)');
  81. }
  82. $model = $model->order('weigh', 'desc')
  83. ->field('id,title,coverimage,category_ids,visit,synopsis,author,createtime,updatetime');
  84. $list = $model->page((int)($where['page'] ?? 1), (int)($where['limit'] ?? 10))
  85. ->select();
  86. //var_dump(self::getLastSql());
  87. foreach ($list as &$item) {
  88. $cate = function ($item) use ($where) {
  89. $cates = Category::where('id', 'in', $item['category_ids'])->column('id');
  90. $cate_item = [];
  91. foreach ($cates as $v) {
  92. $cate = Category::where('id', $v)->value('name');
  93. if (isset($where['lang']) && $where['lang']) {
  94. $info = CategoryLangs::where('category_id', $v)->where('langlist', $where['lang'])->find();
  95. if ($info) {
  96. $cate = $info['name'];
  97. }
  98. }
  99. $cate_item[] = $cate;
  100. }
  101. $item['cate'] = $cate_item;
  102. if (isset($where['lang']) && $where['lang']) {
  103. $info = CasesLangs::where('cases_id', $item['id'])->where('langlist', $where['lang'])->find();
  104. if ($info) {
  105. $item['title'] = $info['title'];
  106. $item['synopsis'] = $info['synopsis'];
  107. $item['content'] = $info['content'];
  108. }
  109. }
  110. };
  111. $cate($item);
  112. }
  113. return compact('list', 'count');
  114. }
  115. /**
  116. * @param $id
  117. * @param string $lang
  118. * @param bool $add_visit
  119. * @return array|false|\PDOStatement|string|Model
  120. * @throws DataNotFoundException
  121. * @throws DbException
  122. * @throws ModelNotFoundException
  123. */
  124. public static function getValidArticle($id, $lang = '', $add_visit = false)
  125. {
  126. $model = self::validWhere();
  127. $info = $model->where('id', $id)->find();
  128. if ($info && $add_visit) {
  129. $info->hidden(['showswitch', 'weigh', 'status', 'admin_id', 'deletetime']);
  130. $info['visit'] = $info['visit'] + 1;
  131. $info->save();
  132. $cates = Category::where('id', 'in', $info['category_ids'])->column('id');
  133. $info_cate = [];
  134. foreach ($cates as $v) {
  135. $cate = Category::where('id', $v)->value('name');
  136. if ($lang) {
  137. $lang_info = CategoryLangs::where('category_id', $v)->where('langlist', $lang)->find();
  138. if ($lang_info) {
  139. $cate = $lang_info['name'];
  140. }
  141. }
  142. $info_cate[] = $cate;
  143. }
  144. $info['cate'] = $info_cate;
  145. if ($lang) {
  146. $lang_info = CasesLangs::where('cases_id', $info['id'])->where('langlist', $lang)->find();
  147. if ($lang_info) {
  148. $info['title'] = $lang_info['title'];
  149. $info['synopsis'] = $lang_info['synopsis'];
  150. $info['content'] = $lang_info['content'];
  151. }
  152. }
  153. }
  154. return $info;
  155. }
  156. }