CanteenController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $data['stall_imgs'] = Helper::formatImgsFiled($data['stall_imgs']);
  16. $school = DB::getInfoById('school', $data['school_id'], 'name');
  17. $company = DB::getInfoById('company', $data['company_id'], 'name');
  18. $data['company_name'] = $company['name']?? '';
  19. $data['school_name'] = $school['name']?? '';
  20. Helper::ok($data);
  21. }
  22. public function actionList()
  23. {
  24. $filter = [
  25. 'is_del' => 0,
  26. 'school_id' => Helper::getPostString('school_id') ? : null,
  27. ];
  28. if ($name = Helper::getPostString('name')) {
  29. $filter['name'] = '%' . $name;
  30. }
  31. if ($company_id = Helper::getPostInt('company_id')) {
  32. $rs = Helper::arrayColumn(
  33. DB::getListWithCriteria(
  34. 'company_canteen_relation',
  35. DbCriteria::simpleCompare(['company_id' => $company_id])
  36. ),
  37. 'canteen_id'
  38. );
  39. $filter['id'] = $rs ?: [-1];
  40. }
  41. $cri = DbCriteria::simpleCompareWithPage($filter);
  42. $data = DB::getListWithCriteria(self::$table, $cri);
  43. if (!empty($data['records'])) {
  44. $users = Helper::arrayColumn(
  45. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
  46. 'username',
  47. 'id'
  48. );
  49. $schools = Helper::arrayColumn(
  50. DB::getListWithCriteria('school', DbCriteria::simpleCompare([])->setSelect('id, name')),
  51. 'name',
  52. 'id'
  53. );
  54. $data['records'] = array_map(function ($item) use ($users, $schools) {
  55. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  56. $item['school_name'] = $schools[$item['school_id']] ?? '-';
  57. $item['stall_imgs'] = Helper::formatImgsFiled($item['stall_imgs']);
  58. return $item;
  59. }, $data['records']);
  60. }
  61. Helper::ok($data);
  62. }
  63. /**
  64. * 下拉列表获取
  65. * @return void
  66. */
  67. public function actionGetSelectList()
  68. {
  69. $cri = DbCriteria::simpleCompare(['t.is_del' => 0])->setAlias('t')
  70. ->setSelect('t.id, t.name, t.school_id, s.name as school_name')
  71. ->setJoin('LEFT JOIN wx_school s on s.id=t.school_id');
  72. $data = DB::getListWithCriteria(self::$table, $cri);
  73. if (empty($data['records'])) {
  74. return [];
  75. }
  76. $newData = [];
  77. foreach ($data['records'] as $item) {
  78. $sid = $item['school_id'];
  79. if (!isset($newData[$sid])) {
  80. $newData[$sid] = [
  81. 'id' => $sid,
  82. 'name' => $item['school_name'],
  83. 'children' => [],
  84. ];
  85. }
  86. $newData[$sid]['children'][] = ['id' => $item['id'], 'name' => $item['name']];
  87. }
  88. Helper::ok(array_values($newData));
  89. }
  90. public function actionDelete()
  91. {
  92. $id = Helper::getPostInt('id');
  93. if ($id < 1) {
  94. Helper::error('参数错误');
  95. }
  96. Db::updateById(self::$table, ['is_del' => 1], $id);
  97. Helper::ok();
  98. }
  99. public function actionAdd()
  100. {
  101. $this->_save();
  102. }
  103. public function actionEdit()
  104. {
  105. $id = Helper::getPostInt('id');
  106. if (!$id) {
  107. Helper::error('参数错误');
  108. }
  109. $this->_save($id);
  110. }
  111. private function _save($id = 0)
  112. {
  113. $data = [
  114. 'school_id' => Helper::getPostInt('school_id'),
  115. 'name' => Helper::getPostString('name'),
  116. 'stall_num' => Helper::getPostInt('stall_num'),
  117. 'is_direct' => Helper::getPostInt('is_direct'),
  118. 'stall_imgs' => Helper::getArrParam($_POST, 'stall_imgs', Helper::PARAM_KEY_TYPE['array_string']),
  119. 'username' => Helper::getPostString('username'),
  120. 'phone' => Helper::getPostString('phone'),
  121. 'weixin' => Helper::getPostString('weixin'),
  122. 'memo' => Helper::getPostString('memo'),
  123. ];
  124. $notNullField = ["school_id","name","stall_num","is_direct","username","phone","weixin"];
  125. $allowEmptyField = ["school_id","stall_num","is_direct"];
  126. // 空字段检测
  127. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  128. Helper::error('参数错误');
  129. }
  130. $name = $data['name'];
  131. // 检测名称重复
  132. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  133. if ($id > 0) {
  134. $cri->addCondition('id!=' . $id);
  135. }
  136. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  137. Helper::error('食堂名称已存在 ' . $fid);
  138. }
  139. $data['stall_imgs'] = $data['stall_imgs'] ? implode(',', $data['stall_imgs']) : '';
  140. if ($id) {
  141. DB::updateById(self::$table, $data, $id);
  142. } else {
  143. DB::addData(self::$table, $data);
  144. }
  145. Helper::ok();
  146. }
  147. public function actionUpdateAttr()
  148. {
  149. $id = Helper::getPostInt('id');
  150. $attr = Helper::getPostString('attr');
  151. $value = Helper::getPostString('value');
  152. if ($id <= 0 || !$attr) {
  153. Helper::error('参数错误');
  154. }
  155. if (!in_array($attr, ['is_direct', 'stall_num'])) {
  156. Helper::error('参数错误2');
  157. }
  158. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  159. Helper::error('更新失败');
  160. }
  161. Helper::ok();
  162. }
  163. }