| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- class CanteenController extends Controller
- {
- public static string $table = 'canteen';
- public function actionInfo()
- {
- $id = Helper::getPostInt('id');
- if ($id <= 0) {
- Helper::error('参数错误');
- }
- $data = DB::getInfoById(self::$table, $id);
- if (!$data) {
- Helper::error('数据不存在');
- }
- $data['stall_imgs'] = Helper::formatImgsFiled($data['stall_imgs']);
- $school = DB::getInfoById('school', $data['school_id'], 'name');
- $company = DB::getInfoById('company', $data['company_id'], 'name');
- $data['company_name'] = $company['name']?? '';
- $data['school_name'] = $school['name']?? '';
- Helper::ok($data);
- }
- public function actionList()
- {
- $filter = [
- 'is_del' => 0,
- 'school_id' => Helper::getPostString('school_id') ? : null,
- ];
- if ($name = Helper::getPostString('name')) {
- $filter['name'] = '%' . $name;
- }
- if ($company_id = Helper::getPostInt('company_id')) {
- $rs = Helper::arrayColumn(
- DB::getListWithCriteria(
- 'company_canteen_relation',
- DbCriteria::simpleCompare(['company_id' => $company_id])
- ),
- 'canteen_id'
- );
- $filter['id'] = $rs ?: [-1];
- }
- $cri = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');
- if ($date = Helper::getPostDate('date')) {
- $cri->addBetweenCondition('create_date', $date, $date . ' 23:59:59');
- }
- $data = DB::getListWithCriteria(self::$table, $cri);
- if (!empty($data['records'])) {
- $users = Helper::arrayColumn(
- DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
- 'username',
- 'id'
- );
- $schools = Helper::arrayColumn(
- DB::getListWithCriteria('school', DbCriteria::simpleCompare([])->setSelect('id, name')),
- 'name',
- 'id'
- );
- $data['records'] = array_map(function ($item) use ($users, $schools) {
- $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
- $item['school_name'] = $schools[$item['school_id']] ?? '-';
- $item['stall_imgs'] = Helper::formatImgsFiled($item['stall_imgs']);
- return $item;
- }, $data['records']);
- }
- Helper::ok($data);
- }
- /**
- * 下拉列表获取
- * @return void
- */
- public function actionGetSelectList()
- {
- $cri = DbCriteria::simpleCompare(['t.is_del' => 0])->setAlias('t')
- ->setSelect('t.id, t.name, t.school_id, s.name as school_name')
- ->setJoin('LEFT JOIN wx_school s on s.id=t.school_id');
- $data = DB::getListWithCriteria(self::$table, $cri);
- if (empty($data['records'])) {
- return [];
- }
- $newData = [];
- foreach ($data['records'] as $item) {
- $sid = $item['school_id'];
- if (!isset($newData[$sid])) {
- $newData[$sid] = [
- 'id' => $sid,
- 'name' => $item['school_name'],
- 'children' => [],
- ];
- }
- $newData[$sid]['children'][] = ['id' => $item['id'], 'name' => $item['name']];
- }
- Helper::ok(array_values($newData));
- }
- public function actionDelete()
- {
- $id = Helper::getPostInt('id');
- if ($id < 1) {
- Helper::error('参数错误');
- }
- Db::updateById(self::$table, ['is_del' => 1], $id);
- Helper::ok();
- }
- public function actionAdd()
- {
- $this->_save();
- }
- public function actionEdit()
- {
- $id = Helper::getPostInt('id');
- if (!$id) {
- Helper::error('参数错误');
- }
- $this->_save($id);
- }
- private function _save($id = 0)
- {
- $data = [
- 'school_id' => Helper::getPostInt('school_id'),
- 'name' => Helper::getPostString('name'),
- 'stall_num' => Helper::getPostInt('stall_num'),
- 'is_direct' => Helper::getPostInt('is_direct'),
- 'stall_imgs' => Helper::getArrParam($_POST, 'stall_imgs', Helper::PARAM_KEY_TYPE['array_string']),
- 'username' => Helper::getPostString('username'),
- 'phone' => Helper::getPostString('phone'),
- 'weixin' => Helper::getPostString('weixin'),
- 'memo' => Helper::getPostString('memo'),
- ];
- $notNullField = ["school_id","name","stall_num","is_direct","username","phone","weixin"];
- $allowEmptyField = ["school_id","stall_num","is_direct"];
- // 空字段检测
- if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
- Helper::error('参数错误');
- }
- $name = $data['name'];
- // 检测名称重复
- $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
- if ($id > 0) {
- $cri->addCondition('id!=' . $id);
- }
- if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
- Helper::error('食堂名称已存在 ' . $fid);
- }
- $data['stall_imgs'] = $data['stall_imgs'] ? implode(',', $data['stall_imgs']) : '';
- if ($id) {
- DB::updateById(self::$table, $data, $id);
- } else {
- DB::addData(self::$table, $data);
- }
- Helper::ok();
- }
- public function actionUpdateAttr()
- {
- $id = Helper::getPostInt('id');
- $attr = Helper::getPostString('attr');
- $value = Helper::getPostString('value');
- if ($id <= 0 || !$attr) {
- Helper::error('参数错误');
- }
- if (!in_array($attr, ['is_direct', 'stall_num'])) {
- Helper::error('参数错误2');
- }
- if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
- Helper::error('更新失败');
- }
- Helper::ok();
- }
- }
|