CanteenController.php 6.0 KB

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