EducationModel.php 5.8 KB

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