CanteenController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class CanteenController extends Controller
  3. {
  4. public static string $table = 'canteen';
  5. public function actionInfo()
  6. {
  7. $id = Helper::getPostInt('id');
  8. if ($id <= 0) {
  9. Helper::error('参数错误');
  10. }
  11. $data = DB::getInfoById(self::$table, $id);
  12. if (!$data) {
  13. Helper::error('数据不存在');
  14. }
  15. if (!$this->checkSchoolId($data['school_id'])) {
  16. Helper::error('您没有权限操作此数据');
  17. }
  18. $data['stall_imgs'] = Helper::formatImgsFiled($data['stall_imgs']);
  19. $school = DB::getInfoById('school', $data['school_id'], 'name');
  20. $company = DB::getInfoById('company', $data['company_id'], 'name');
  21. $data['company_name'] = $company['name']?? '';
  22. $data['school_name'] = $school['name']?? '';
  23. Helper::ok($data);
  24. }
  25. public function actionList()
  26. {
  27. $filter = ['is_del' => 0];
  28. $schoolId = Helper::getPostString('school_id');
  29. if ($schoolId) {
  30. if (!$this->checkSchoolId($schoolId)) {
  31. $filter['school_id'] = -1;
  32. } else {
  33. $filter['school_id'] = $schoolId;
  34. }
  35. } else {
  36. $filter['school_id'] = $this->getSchoolFilter();
  37. }
  38. if ($name = Helper::getPostString('name')) {
  39. $filter['name'] = '%' . $name;
  40. }
  41. if ($company_id = Helper::getPostInt('company_id')) {
  42. $rs = Helper::arrayColumn(
  43. DB::getListWithCriteria(
  44. 'company_canteen_relation',
  45. DbCriteria::simpleCompare(['company_id' => $company_id])
  46. ),
  47. 'canteen_id'
  48. );
  49. $filter['id'] = $rs ?: [-1];
  50. }
  51. $cri = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
  52. if ($date = Helper::getPostDate('date')) {
  53. $cri->addBetweenCondition('create_date', $date, $date . ' 23:59:59');
  54. }
  55. $data = DB::getListWithCriteria(self::$table, $cri);
  56. if (!empty($data['records'])) {
  57. $users = Helper::arrayColumn(
  58. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
  59. 'username',
  60. 'id'
  61. );
  62. $schools = Helper::arrayColumn(
  63. DB::getListWithCriteria('school', DbCriteria::simpleCompare([])->setSelect('id, name')),
  64. 'name',
  65. 'id'
  66. );
  67. $data['records'] = array_map(function ($item) use ($users, $schools) {
  68. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  69. $item['school_name'] = $schools[$item['school_id']] ?? '-';
  70. $item['stall_imgs'] = Helper::formatImgsFiled($item['stall_imgs']);
  71. return $item;
  72. }, $data['records']);
  73. }
  74. Helper::ok($data);
  75. }
  76. public function actionDelete()
  77. {
  78. $id = Helper::getPostInt('id');
  79. if ($id < 1) {
  80. Helper::error('参数错误');
  81. }
  82. $data = DB::getInfoById(self::$table, $id);
  83. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  84. Helper::error('您没有权限操作此数据');
  85. }
  86. Db::updateById(self::$table, ['is_del' => 1], $id);
  87. Helper::ok();
  88. }
  89. public function actionAdd()
  90. {
  91. $this->_save();
  92. }
  93. public function actionEdit()
  94. {
  95. $id = Helper::getPostInt('id');
  96. if (!$id) {
  97. Helper::error('参数错误');
  98. }
  99. $data = DB::getInfoById(self::$table, $id);
  100. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  101. Helper::error('您没有权限操作此数据');
  102. }
  103. $this->_save($id);
  104. }
  105. private function _save($id = 0)
  106. {
  107. $data = [
  108. 'school_id' => Helper::getPostInt('school_id'),
  109. 'name' => Helper::getPostString('name'),
  110. 'stall_num' => Helper::getPostInt('stall_num'),
  111. 'is_direct' => Helper::getPostInt('is_direct'),
  112. 'stall_imgs' => Helper::getArrParam($_POST, 'stall_imgs', Helper::PARAM_KEY_TYPE['array_string']),
  113. 'username' => Helper::getPostString('username'),
  114. 'phone' => Helper::getPostString('phone'),
  115. 'weixin' => Helper::getPostString('weixin'),
  116. 'memo' => Helper::getPostString('memo'),
  117. ];
  118. $notNullField = ["school_id","name","stall_num","is_direct","username","phone","weixin"];
  119. $allowEmptyField = ["school_id","stall_num","is_direct"];
  120. // 空字段检测
  121. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  122. Helper::error('参数错误');
  123. }
  124. $name = $data['name'];
  125. // 检测名称重复
  126. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  127. if ($id > 0) {
  128. $cri->addCondition('id!=' . $id);
  129. }
  130. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  131. Helper::error('食堂名称已存在 ' . $fid);
  132. }
  133. $data['stall_imgs'] = $data['stall_imgs'] ? implode(',', $data['stall_imgs']) : '';
  134. if ($id) {
  135. DB::updateById(self::$table, $data, $id);
  136. } else {
  137. DB::addData(self::$table, $data);
  138. }
  139. Helper::ok();
  140. }
  141. public function actionUpdateAttr()
  142. {
  143. $id = Helper::getPostInt('id');
  144. $attr = Helper::getPostString('attr');
  145. $value = Helper::getPostString('value');
  146. if ($id <= 0 || !$attr) {
  147. Helper::error('参数错误');
  148. }
  149. $data = DB::getInfoById(self::$table, $id);
  150. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  151. Helper::error('您没有权限操作此数据');
  152. }
  153. if (!in_array($attr, ['is_direct', 'stall_num'])) {
  154. Helper::error('参数错误2');
  155. }
  156. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  157. Helper::error('更新失败');
  158. }
  159. Helper::ok();
  160. }
  161. }