EducationModel.php 6.6 KB

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