| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- class CompanyController extends Controller
- {
- public static string $table = 'company';
- public function actionInfo()
- {
- $id = Helper::getPostInt('id');
- if ($id <= 0) {
- Helper::error('参数错误');
- }
- if (!$this->checkCompanyId($id)) {
- Helper::error('您没有权限操作此数据');
- }
- $data = DB::getInfoById(self::$table, $id);
- if (!$data) {
- Helper::error('数据不存在');
- }
- $cri = DbCriteria::simpleCompare(['id' => [$data['last_user_id'], $data['bind_user_id']]])->setSelect('id, username');
- $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), 'username', 'id');
- $data['last_user_name'] = $users[$data['last_user_id']] ?? '';
- $data['bind_user_name'] = $users[$data['bind_user_id']] ?? '';
- $data['distinct'] = [
- $data['province'],
- $data['city'],
- // $data['area'],
- ];
- // 关联食堂
- $data['canteen_names'] = [];
- $data['canteens'] = [];
- $cri = DbCriteria::simpleCompare(['t.company_id' => $id])
- ->setAlias('t')
- ->setSelect('t.school_id, t.canteen_id, s.name as school_name, c.name as canteen_name')
- ->setJoin('left join wx_school s on t.school_id = s.id')
- ->addJoin('left join wx_canteen c on t.canteen_id = c.id');
- $canteens = DB::getListWithCriteria('company_canteen_relation', $cri);
- foreach ($canteens['records'] as $item) {
- $data['canteens'][] = [(int)$item['school_id'], (int)$item['canteen_id']];
- $data['canteen_names'][] = "{$item['canteen_name']}({$item['school_name']})";
- }
- // 关系人
- $relations = DB::getListWithCriteria(
- 'company_contact',
- DbCriteria::simpleCompare(['company_id' => $id])->setSelect('id, name, phone, position, weixin'),
- );
- $data['relations'] = $relations['records'];
- Helper::ok($data);
- }
- public function actionList()
- {
- $filter = [
- 'is_del' => 0,
- 'phone' => Helper::getPostString('phone'),
- 'id' => $this->getcompanyFilter(),
- ];
- if ($name = Helper::getPostString('name')) {
- $filter['name'] = '%' . $name;
- }
- $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
- $filter['province'] = $address[0]?? null;
- $filter['city'] = $address[1]?? null;
- $filter['area'] = $address[2]?? null;
- if ($school_id = Helper::getPostInt('school_id')) {
- $cri = DbCriteria::simpleCompare(['school_id' => $school_id])->setSelect('company_id');
- $ids = Helper::arrayColumn(
- DB::getListWithCriteria('company_canteen_relation', $cri),
- 'company_id'
- );
- $filter['id'] = $ids?: -1;
- }
- $cri = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');;
- $data = DB::getListWithCriteria(self::$table, $cri);
- if (!empty($data['records'])) {
- $users = Helper::arrayColumn(
- DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
- 'username',
- 'id'
- );
- $data['records'] = array_map(function ($item) use ($users) {
- $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
- $item['bind_user_name'] = $users[$item['bind_user_id']] ?? '-';
- return $item;
- }, $data['records']);
- }
- Helper::ok($data);
- }
- public function actionDelete()
- {
- $id = Helper::getPostInt('id');
- if ($id < 1) {
- Helper::error('参数错误');
- }
- if (!$this->checkCompanyId($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('参数错误');
- }
- if (!$this->checkCompanyId($id)) {
- Helper::error('您没有权限操作此数据');
- }
- $this->_save($id);
- }
- private function _save($id = 0)
- {
- $data = [
- 'name' => Helper::getPostString('name'),
- 'address' => Helper::getPostString('address'),
- 'memo' => Helper::getPostString('memo'),
- 'bind_user_id' => Helper::getPostInt('bind_user_id'),
- ];
- // 空字段检测
- if (!Helper::checkEmptyKey($data, ['name', 'address', 'memo'], ['memo'])) {
- Helper::error('参数错误');
- }
- // 处理地区
- $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
- $district = array_filter($district);
- if (count($district) != 2) {
- Helper::error('地区参数错误');
- }
- $data['province'] = $district[0];
- $data['city'] = $district[1];
- $data['area'] = '';
- // 关联食堂
- $canteens = $_POST['canteens']?? [];
- if (!$canteens) {
- Helper::error('请选择关联的食堂');
- }
- foreach ($canteens as $k => $canteen) {
- $canteens[$k] = array_filter(explode(',', $canteen));
- if (count($canteens[$k]) != 2 || empty($canteens[$k][0]) || empty($canteens[$k][1])) {
- Helper::error('选择的食堂参数有误 ' . json_encode($canteens));
- }
- }
- $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();
- $trans = \Yii::app()->db->beginTransaction();
- try {
- if ($id) {
- DB::updateById(self::$table, $data, $id);
- DB::deleteByCondition('company_canteen_relation', ['company_id' => $id]);
- } else {
- $id = DB::addData(self::$table, $data);
- if (!$id) {
- throw new \Exception('添加失败');
- }
- // 给用户操作权限
- $user = DB::getInfoById('useradmin', $this->getUserId());
- if (!str_contains($user['company_ids'], '-1')) {
- DB::updateById(
- 'useradmin',
- ['company_ids' => trim($user['company_ids'] . ',' . $id, ',')],
- $this->getUserId()
- );
- }
- $this->clearAuth();
- }
- $batchArr = [];
- foreach ($canteens as $canteen) {
- $batchArr[] = [
- 'company_id' => $id,
- 'school_id' => $canteen[0],
- 'canteen_id' => $canteen[1],
- ];
- }
- DB::safeBatchInsert('company_canteen_relation', $batchArr);
- $trans->commit();
- } catch (\Exception $e) {
- $trans->rollback();
- Helper::error($e->getMessage());
- }
- Helper::ok();
- }
- public function actionUpdateAttr()
- {
- $id = Helper::getPostInt('id');
- $attr = Helper::getPostString('attr');
- $value = Helper::getPostString('value');
- if ($id <= 0 || !$attr) {
- Helper::error('参数错误');
- }
- if (!$this->checkCompanyId($id)) {
- Helper::error('您没有权限操作此数据');
- }
- if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
- Helper::error('参数错误2');
- }
- if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
- Helper::error('参数错误3');
- }
- if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
- Helper::error('更新失败');
- }
- Helper::ok();
- }
- }
|