EducationModel.php 5.7 KB

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