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