EducationModel.php 5.7 KB

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