CanteenController.php 8.4 KB

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