EducationModel.php 5.9 KB

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