EducationModel.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\model\api;
  3. use think\Model;
  4. /**
  5. * 教育模型
  6. */
  7. class EducationModel extends Model
  8. {
  9. protected $table = 'table_education_cate';
  10. public static function getInstance()
  11. {
  12. return new self;
  13. }
  14. /**
  15. * 获取所有分类
  16. *
  17. * @return mixed
  18. */
  19. public function getCate()
  20. {
  21. $items = $this->table(self::CATE_TABLE_NAME)
  22. ->where(['status' => 'Y'])
  23. ->order('sort', 'asc')
  24. ->column('id,pid,name,sort', 'id');
  25. $tree = array();
  26. foreach ($items as $key => $item) {
  27. $items[$key]['oriName'] = $item['name'];
  28. if (isset ($items [$item ['pid']])) {
  29. $items [$item ['pid']] ['son'] [$item ['id']] = &$items [$item ['id']];
  30. } else {
  31. $tree [$item ['id']] = &$items [$item ['id']];
  32. }
  33. }
  34. return $tree;
  35. }
  36. // public function getGrList($userId)
  37. // {
  38. // return $this->where('user', $userId)->order('create_time', 'desc')->select()->toArray();
  39. // }
  40. /**
  41. * 获取所有文章列表
  42. *
  43. * @param int|null $excludeCourseId 需要排除的课程 ID
  44. * @return array
  45. */
  46. public function getCourseList($excludeCourseId = null)
  47. {
  48. $query = $this->where(['status' => 'Y']);
  49. if ($excludeCourseId !== null) {
  50. $query->where('id', '<>', $excludeCourseId);
  51. }
  52. $query->order('sort', 'asc');
  53. return $query->select()->toArray();
  54. }
  55. /**
  56. * 获取顶级类
  57. */
  58. public function getTopCate()
  59. {
  60. $items = $this->where(['status' => 'Y', 'pid' => 0])
  61. ->order('sort', 'asc')
  62. ->field('id,pid,name,sort')
  63. ->select()
  64. ->toArray();
  65. return $items;
  66. }
  67. /**
  68. *获取指定ID列表
  69. */
  70. public function getSubcatesByTopCate($cates, &$subcates = array())
  71. {
  72. foreach ($cates as $cate) {
  73. $subcates[] = $cate['id'];
  74. if (isset($cate['son'])) {
  75. $this->getSubcatesByTopCate($cate['son'], $subcates);
  76. }
  77. }
  78. return $subcates;
  79. }
  80. // /**
  81. // * 去除分类名称中的前缀
  82. // *
  83. // * @param string $dirtyName 脏分类名称
  84. // * @return string
  85. // */
  86. // public static function getPureCateName($dirtyName)
  87. // {
  88. // return strtr($dirtyName, [self::CATE_TREE_PREFIX => '']);
  89. // }
  90. /**
  91. * 修改分类
  92. *
  93. * @param array $data 修改的信息
  94. * @param $id 类目ID
  95. * @return bool
  96. */
  97. public function editCate(array $data, $id)
  98. {
  99. if ($this->allowField(true)->save($data, ['id' => $id])) {
  100. return true;
  101. }
  102. return false;
  103. }
  104. /**
  105. * 增加一个分类
  106. *
  107. * @param array $data
  108. * @return bool|mixed
  109. */
  110. public function addCate(array $data)
  111. {
  112. $data['status'] = 'Y';
  113. if ($this->save($data)) {
  114. return $this->id;
  115. } else {
  116. return false;
  117. }
  118. }
  119. /**
  120. * 删除一个分类
  121. *
  122. * @param array $data
  123. * @return bool|mixed
  124. */
  125. public function delCate($id)
  126. {
  127. return $this->editCate(['id' => $id, 'status' => 'N'], $id);
  128. }
  129. public function getCourseListByCates($cates = array(), $pageSize = 3, $page = 1, $userInfo=null)
  130. {
  131. $firstRow = (($page < 1) ? 0 : ($page - 1)) * $pageSize;
  132. $where = array(
  133. 'course_audit' => 1,
  134. 'course_status' => 1,
  135. 'course_only_for_employee' => 0,
  136. 'course_cate_id' => array('in', $cates)
  137. );
  138. $fields = 'course_id, course_cate_id, course_topic, course_intro, course_cover, course_speaker_intro, course_play_count, ';
  139. if ($userInfo){
  140. // 假如当前用户是从业人员,那就删除条件
  141. if ($userInfo["is_employee"]){
  142. unset($where["course_only_for_employee"]);
  143. }
  144. // 根据用户类型选择不同字段
  145. switch ($userInfo["user_type"]){
  146. case "1":
  147. $fields .= "course_price_2 as course_price";
  148. break;
  149. case "2":
  150. $fields .= "course_price_3 as course_price";
  151. break;
  152. default :
  153. $fields .= "course_price";
  154. }
  155. }else{
  156. $fields .= "course_price";
  157. }
  158. $result = array();
  159. if ($cates) {
  160. $result = $this->where( $where )
  161. ->order('course_id desc')
  162. ->limit($firstRow, $pageSize)
  163. ->column($fields);
  164. }
  165. return (array)$result;
  166. }
  167. public function getCourseListLikeTopic($topic, $pageSize = 50, $page = 1)
  168. {
  169. $firstRow = (($page < 1) ? 0 : ($page - 1)) * $pageSize;
  170. $result = array();
  171. if ($topic) {
  172. $result = $this->where(
  173. array(
  174. 'course_audit' => 1,
  175. 'course_status' => 1,
  176. 'course_topic' => array('like', '%' . $topic . '%')
  177. )
  178. )
  179. ->order('course_id desc')
  180. ->limit($firstRow, $pageSize)
  181. ->column(
  182. 'course_id,
  183. course_cate_id,
  184. course_topic,
  185. course_price,
  186. course_intro,
  187. course_cover,
  188. course_speaker_intro,
  189. course_play_count,
  190. gr_id'
  191. );
  192. }
  193. return (array)$result;
  194. }
  195. }