CanteenController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. $cri = DbCriteria::simpleCompare(['t.canteen_id' => $id])
  21. ->setSelect('c.name,c.id')
  22. ->setAlias('t')
  23. ->setJoin('left join wx_company c on c.id=t.company_id');
  24. $companyInfo = DB::getInfoWithCriteria('company_canteen_relation', $cri);
  25. $data['company_name'] = $companyInfo['name']?? '';
  26. $data['company_id'] = $companyInfo['id']?? '';
  27. $data['school_name'] = $school['name']?? '';
  28. Helper::ok($data);
  29. }
  30. public function actionList()
  31. {
  32. $filter = ['is_del' => 0];
  33. $schoolId = Helper::getPostString('school_id');
  34. if ($schoolId) {
  35. if (!$this->checkSchoolId($schoolId)) {
  36. $filter['school_id'] = -1;
  37. } else {
  38. $filter['school_id'] = $schoolId;
  39. }
  40. } else {
  41. $filter['school_id'] = $this->getSchoolFilter();
  42. }
  43. if ($name = Helper::getPostString('name')) {
  44. $filter['name'] = '%' . $name;
  45. }
  46. if ($company_id = Helper::getPostInt('company_id')) {
  47. $rs = Helper::arrayColumn(
  48. DB::getListWithCriteria(
  49. 'company_canteen_relation',
  50. DbCriteria::simpleCompare(['company_id' => $company_id])
  51. ),
  52. 'canteen_id'
  53. );
  54. $filter['id'] = $rs ?: [-1];
  55. }
  56. $cri = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
  57. if ($date = Helper::getPostDate('date')) {
  58. $cri->addBetweenCondition('create_date', $date, $date . ' 23:59:59');
  59. }
  60. $data = DB::getListWithCriteria(self::$table, $cri);
  61. if (!empty($data['records'])) {
  62. $users = Helper::arrayColumn(
  63. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare(['id' => array_column($data['records'], 'last_user_id')])->setSelect('id, username')),
  64. 'username',
  65. 'id'
  66. );
  67. $schools = Helper::arrayColumn(
  68. DB::getListWithCriteria('school', DbCriteria::simpleCompare(['id' => array_column($data['records'], 'school_id')])->setSelect('id, name')),
  69. 'name',
  70. 'id'
  71. );
  72. $cri = DbCriteria::simpleCompare([['t.canteen_id' => array_column($data['records'], 'id')]])
  73. ->setSelect('c.name,t.canteen_id')
  74. ->setAlias('t')
  75. ->setJoin('left join wx_company c on c.id=t.company_id');
  76. $relations = Helper::arrayColumn(
  77. DB::getListWithCriteria('company_canteen_relation', $cri),
  78. 'name',
  79. 'canteen_id'
  80. );
  81. $data['records'] = array_map(function ($item) use ($users, $schools, $relations) {
  82. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  83. $item['school_name'] = $schools[$item['school_id']] ?? '-';
  84. $item['company_name'] = $relations[$item['id']] ?? '-';
  85. $item['stall_imgs'] = Helper::formatImgsFiled($item['stall_imgs']);
  86. return $item;
  87. }, $data['records']);
  88. }
  89. Helper::ok($data);
  90. }
  91. public function actionDelete()
  92. {
  93. $id = Helper::getPostInt('id');
  94. if ($id < 1) {
  95. Helper::error('参数错误');
  96. }
  97. $data = DB::getInfoById(self::$table, $id);
  98. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  99. Helper::error('您没有权限操作此数据');
  100. }
  101. Db::updateById(self::$table, ['is_del' => 1], $id);
  102. Helper::ok();
  103. }
  104. public function actionAdd()
  105. {
  106. $this->_save();
  107. }
  108. public function actionEdit()
  109. {
  110. $id = Helper::getPostInt('id');
  111. if (!$id) {
  112. Helper::error('参数错误');
  113. }
  114. $data = DB::getInfoById(self::$table, $id);
  115. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  116. Helper::error('您没有权限操作此数据');
  117. }
  118. $this->_save($id);
  119. }
  120. private function _save($id = 0)
  121. {
  122. $data = [
  123. 'school_id' => Helper::getPostInt('school_id'),
  124. 'name' => Helper::getPostString('name'),
  125. 'stall_num' => Helper::getPostInt('stall_num'),
  126. 'is_direct' => Helper::getPostInt('is_direct'),
  127. 'stall_imgs' => Helper::getArrParam($_POST, 'stall_imgs', Helper::PARAM_KEY_TYPE['array_string']),
  128. 'username' => Helper::getPostString('username'),
  129. 'phone' => Helper::getPostString('phone'),
  130. 'weixin' => Helper::getPostString('weixin'),
  131. 'memo' => Helper::getPostString('memo'),
  132. ];
  133. $company_id = Helper::getPostInt('company_id');
  134. $notNullField = ["school_id","name","stall_num","is_direct","username","phone","weixin"];
  135. $allowEmptyField = ["stall_num","is_direct",'weixin'];
  136. // 空字段检测
  137. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  138. Helper::error('参数错误');
  139. }
  140. $name = $data['name'];
  141. // 检测名称重复
  142. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  143. if ($id > 0) {
  144. $cri->addCondition('id!=' . $id);
  145. }
  146. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  147. Helper::error('食堂名称已存在 ' . $fid);
  148. }
  149. $this->dobuleCheck();
  150. $data['stall_imgs'] = $data['stall_imgs'] ? implode(',', $data['stall_imgs']) : '';
  151. $trans = \Yii::app()->db->beginTransaction();
  152. try {
  153. if ($id) {
  154. DB::updateById(self::$table, $data, $id);
  155. } else {
  156. DB::addData(self::$table, $data);
  157. }
  158. Db::deleteByCondition('company_canteen_relation', ['canteen_id' => $id]);
  159. if ($company_id) {
  160. Db::addData('company_canteen_relation', ['canteen_id' => $id, 'company_id' => $company_id, 'school_id' => $data['school_id']]);
  161. }
  162. $trans->commit();
  163. } catch (\Exception $e) {
  164. $trans->rollback();
  165. Helper::error($e->getMessage());
  166. }
  167. Helper::ok();
  168. }
  169. public function actionUpdateAttr()
  170. {
  171. $id = Helper::getPostInt('id');
  172. $attr = Helper::getPostString('attr');
  173. $value = Helper::getPostString('value');
  174. if ($id <= 0 || !$attr) {
  175. Helper::error('参数错误');
  176. }
  177. $data = DB::getInfoById(self::$table, $id);
  178. if (!$data || !$this->checkSchoolId($data['school_id'])) {
  179. Helper::error('您没有权限操作此数据');
  180. }
  181. if (!in_array($attr, ['is_direct', 'stall_num'])) {
  182. Helper::error('参数错误2');
  183. }
  184. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  185. Helper::error('更新失败');
  186. }
  187. Helper::ok();
  188. }
  189. }