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