| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?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('数据不存在');
- }
- if (!$this->checkSchoolId($data['school_id'])) {
- Helper::error('您没有权限操作此数据');
- }
- $data['stall_imgs'] = Helper::formatImgsFiled($data['stall_imgs']);
- $school = DB::getInfoById('school', $data['school_id'], 'name');
- $cri = DbCriteria::simpleCompare(['t.canteen_id' => $id])
- ->setSelect('c.name,c.id')
- ->setAlias('t')
- ->setJoin('left join wx_company c on c.id=t.company_id');
- $companyInfo = DB::getInfoWithCriteria('company_canteen_relation', $cri);
- $data['company_name'] = $companyInfo['name']?? '';
- $data['company_id'] = $companyInfo['id']?? '';
- $data['school_name'] = $school['name']?? '';
- Helper::ok($data);
- }
- public function actionList()
- {
- $filter = ['t.is_del' => 0];
- $schoolId = Helper::getPostString('school_id');
- if ($schoolId) {
- if (!$this->checkSchoolId($schoolId)) {
- $filter['t.school_id'] = -1;
- } else {
- $filter['t.school_id'] = $schoolId;
- }
- } else {
- $filter['t.school_id'] = $this->getSchoolFilter();
- }
- if ($name = Helper::getPostString('name')) {
- $filter['t.name'] = '%' . $name;
- }
- $id_direct = Helper::getPostInt('is_direct', -1);
- if ($id_direct != -1) {
- $filter['t.is_direct'] = $id_direct;
- }
- if ($last_user_id = Helper::getPostInt('last_user_id')) {
- $filter['t.last_user_id'] = $last_user_id;
- }
- if ($phone = Helper::getPostString('phone')) {
- $filter['t.phone'] = $phone;
- }
- if ($company_id = Helper::getPostInt('company_id')) {
- $rs = Helper::arrayColumn(
- DB::getListWithCriteria(
- 'company_canteen_relation',
- DbCriteria::simpleCompare(['company_id' => $company_id])
- ),
- 'canteen_id'
- );
- $filter['t.id'] = $rs ?: [-1];
- }
- $cri = DbCriteria::simpleCompareWithPage($filter)
- ->setAlias('t')
- ->setSelect('t.*, group_concat(sf.id) AS follow_ids')
- ->addJoin('LEFT JOIN wx_canteen_follow AS sf ON sf.canteen_id = t.id')
- ->setGroup('t.id')
- ->setOrder('t.id desc');
- if ($date = Helper::getPostDate('date')) {
- $cri->addBetweenCondition('create_date', $date, $date . ' 23:59:59');
- }
- $last_date = $_POST['last_date'];
- if (is_array($last_date) && count($last_date) == 2 && $last_date[0] < $last_date[1]) {
- $cri->addcondition("t.last_date > '{$last_date[0]}' and t.last_date < '{$last_date[1]}'");
- }
- $data = DB::getListWithCriteria(self::$table, $cri);
- if (!empty($data['records'])) {
- $users = Helper::arrayColumn(
- DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username,avatar')),
- null,
- 'id'
- );
- $schools = Helper::arrayColumn(
- DB::getListWithCriteria('school', DbCriteria::simpleCompare(['id' => array_column($data['records'], 'school_id')])->setSelect('id, name')),
- 'name',
- 'id'
- );
- $cri = DbCriteria::simpleCompare([['t.canteen_id' => array_column($data['records'], 'id')]])
- ->setSelect('c.name,t.canteen_id')
- ->setAlias('t')
- ->setJoin('left join wx_company c on c.id=t.company_id');
- $relations = Helper::arrayColumn(
- DB::getListWithCriteria('company_canteen_relation', $cri),
- 'name',
- 'canteen_id'
- );
- $data['records'] = FollowSrv::formatWithFollowList($data['records'], 'wx_canteen_follow', $users);
- $data['records'] = array_map(function ($item) use ($users, $schools, $relations) {
- $item['last_user_name'] = $users[$item['last_user_id']]['username'] ?? '-';
- $item['school_name'] = $schools[$item['school_id']] ?? '-';
- $item['company_name'] = $relations[$item['id']] ?? '-';
- $item['stall_imgs'] = Helper::formatImgsFiled($item['stall_imgs']);
- return $item;
- }, $data['records']);
- }
- Helper::ok($data);
- }
- public function actionDelete()
- {
- $id = Helper::getPostInt('id');
- if ($id < 1) {
- Helper::error('参数错误');
- }
- $data = DB::getInfoById(self::$table, $id);
- if (!$data || !$this->checkSchoolId($data['school_id'])) {
- 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('参数错误');
- }
- $data = DB::getInfoById(self::$table, $id);
- if (!$data || !$this->checkSchoolId($data['school_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'),
- ];
- $company_id = Helper::getPostInt('company_id');
- $notNullField = ["school_id","name","stall_num","is_direct","username","phone","weixin"];
- $allowEmptyField = ["stall_num","is_direct",'weixin'];
- // 空字段检测
- 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);
- }
- $this->dobuleCheck();
- $data['stall_imgs'] = $data['stall_imgs'] ? implode(',', $data['stall_imgs']) : '';
- $trans = \Yii::app()->db->beginTransaction();
- try {
- if ($id) {
- DB::updateById(self::$table, $data, $id);
- } else {
- DB::addData(self::$table, $data);
- }
- Db::deleteByCondition('company_canteen_relation', ['canteen_id' => $id]);
- if ($company_id) {
- Db::addData('company_canteen_relation', ['canteen_id' => $id, 'company_id' => $company_id, 'school_id' => $data['school_id']]);
- }
- $trans->commit();
- } catch (\Exception $e) {
- $trans->rollback();
- Helper::error($e->getMessage());
- }
- $this->clearAuth();
- Helper::ok();
- }
- public function actionUpdateAttr()
- {
- $id = Helper::getPostInt('id');
- $attr = Helper::getPostString('attr');
- $value = Helper::getPostString('value');
- if ($id <= 0 || !$attr) {
- Helper::error('参数错误');
- }
- $data = DB::getInfoById(self::$table, $id);
- if (!$data || !$this->checkSchoolId($data['school_id'])) {
- 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();
- }
- }
|