School.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\common\controller\Backend;
  4. use app\admin\model\unishop\Area;
  5. use think\Db;
  6. /**
  7. * 学校管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class School extends Backend
  12. {
  13. /**
  14. * School模型对象
  15. * @var \app\admin\model\unishop\School
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\unishop\School;
  22. $this->areamodel = new \app\admin\model\unishop\Area;
  23. $this->grademodel = new \app\admin\model\unishop\Grade;
  24. $this->classmodel = new \app\admin\model\unishop\Classes;
  25. $this->ordermodel = new \app\admin\model\unishop\Order;
  26. $this->studentmodel = new \app\admin\model\unishop\Student;
  27. }
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //设置过滤方法
  34. $this->request->filter(['strip_tags']);
  35. if ($this->request->isAjax()) {
  36. //如果发送的来源是Selectpage,则转发到Selectpage
  37. if ($this->request->request('keyField')) {
  38. return $this->selectpage();
  39. }
  40. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  41. $total = $this->model
  42. ->where($where)
  43. ->with("Area")
  44. ->order($sort, $order)
  45. ->count();
  46. $list = $this->model
  47. ->where($where)
  48. ->with("Area")
  49. ->order($sort, $order)
  50. ->limit($offset, $limit)
  51. ->select();
  52. $list = collection($list)->toArray();
  53. $result = array("total" => $total, "rows" => $list);
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 添加
  60. */
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $params = $this->request->post("row/a");
  65. if ($params) {
  66. $params = $this->preExcludeFields($params);
  67. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  68. $params[$this->dataLimitField] = $this->auth->id;
  69. }
  70. $result = false;
  71. Db::startTrans();
  72. try {
  73. //是否采用模型验证
  74. if ($this->modelValidate) {
  75. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  76. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  77. $this->model->validateFailException(true)->validate($validate);
  78. }
  79. $result = $this->model->allowField(true)->save($params);
  80. Db::commit();
  81. } catch (ValidateException $e) {
  82. Db::rollback();
  83. $this->error($e->getMessage());
  84. } catch (PDOException $e) {
  85. Db::rollback();
  86. $this->error($e->getMessage());
  87. } catch (Exception $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. }
  91. if ($result !== false) {
  92. $this->success();
  93. } else {
  94. $this->error(__('No rows were inserted'));
  95. }
  96. }
  97. $this->error(__('Parameter %s can not be empty', ''));
  98. }
  99. return $this->view->fetch();
  100. }
  101. /**
  102. * 编辑
  103. */
  104. public function edit($ids = null)
  105. {
  106. $row = $this->model->get($ids);
  107. if (!$row) {
  108. $this->error(__('No Results were found'));
  109. }
  110. $adminIds = $this->getDataLimitAdminIds();
  111. if (is_array($adminIds)) {
  112. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  113. $this->error(__('You have no permission'));
  114. }
  115. }
  116. $areainfo = $this->areamodel->get($row["area_id"]);
  117. if ($this->request->isPost()) {
  118. $params = $this->request->post("row/a");
  119. if ($params) {
  120. $params = $this->preExcludeFields($params);
  121. if(intval($params["area_id"]) == 0) {
  122. unset($params["area_id"]);
  123. }
  124. $result = false;
  125. Db::startTrans();
  126. try {
  127. //是否采用模型验证
  128. if ($this->modelValidate) {
  129. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  131. $row->validateFailException(true)->validate($validate);
  132. }
  133. $result = $row->allowField(true)->save($params);
  134. Db::commit();
  135. } catch (ValidateException $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. } catch (PDOException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. } catch (Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. if ($result !== false) {
  146. $this->success();
  147. } else {
  148. $this->error(__('No rows were updated'));
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', ''));
  152. }
  153. $row["area_name"] = "山东省/临沂市/".$areainfo->name;
  154. $this->view->assign("row", $row);
  155. return $this->view->fetch();
  156. }
  157. /**
  158. * 获取订购数据
  159. */
  160. public function data($ids = null){
  161. $school_id = $ids?$ids:0;
  162. // 获取学校名称
  163. $school_name = $this->model->where(["id"=>$school_id])->value("name");
  164. // 根据学校ID获取年级班级数据
  165. $gradeinfo = $this->grademodel->where(["school_id"=>$school_id])->select();
  166. // $orderinfo = $this->ordermodel->where(["status"=>1,"have_paid"=>1])->select();
  167. // print_r($orderinfo);exit;
  168. // 获取所有成交订单信息
  169. $orderinfo = $this->ordermodel->alias("o")
  170. ->join("fa_student s","s.user_id = o.user_id")
  171. ->where(["o.status"=>1,"o.have_paid"=>1,"s.is_main"=>1])->select();
  172. $userids = [];
  173. foreach($orderinfo as $k => $v) {
  174. $num = isset($userids[$v["grade_id"]][$v["class_id"]])?$userids[$v["grade_id"]][$v["class_id"]]+1:1;
  175. $userids[$v["grade_id"]][$v["class_id"]] = $num;
  176. }
  177. $data = [];
  178. if($gradeinfo) {
  179. foreach($gradeinfo as $k => $v) {
  180. $data[$k]["grade"] = $v["name"];
  181. $classinfo = $this->classmodel->where(["grade_id"=>$v["id"]])->select();
  182. $data[$k]["class"] = [];
  183. $data[$k]["num"] = 0;
  184. if($classinfo) {
  185. foreach($classinfo as $ke => $va) {
  186. $data[$k]["class"][$ke]["name"] = $va["name"];
  187. $data[$k]["class"][$ke]["num"] = isset($userids[$v["id"]][$va["id"]])?$userids[$v["id"]][$va["id"]]:0;
  188. $data[$k]["num"] = $data[$k]["num"]+$data[$k]["class"][$ke]["num"];
  189. }
  190. }
  191. }
  192. }
  193. // print_r($ids);exit;
  194. $this->view->assign("school_name", $school_name);
  195. $this->view->assign("data", $data);
  196. // print_r($data);exit;
  197. return $this->view->fetch();
  198. }
  199. }