CanteenController.php 7.8 KB

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