| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- class CompanyRelationController extends Controller
- {
- public string $table = 'company_contact';
- public function actionInfo()
- {
- $id = Helper::getPostInt('id');
- if ($id <= 0) {
- Helper::error('参数错误');
- }
- $data = DB::getInfoById($this->table, $id);
- if (!$data) {
- Helper::error('数据不存在');
- }
- if (!$this->checkCompanyId($data['company_id'])) {
- Helper::error('您没有权限操作此数据');
- }
- Helper::ok($data);
- }
- public function actionList()
- {
- $filter = [
- 'r.is_del' => 0,
- 'r.phone' => Helper::getPostString('phone'),
- ];
- $companyId = Helper::getPostString('company_id');
- if ($companyId) {
- if (!$this->checkCompanyId($companyId)) {
- $filter['r.company_id'] = -1;
- } else {
- $filter['r.company_id'] = $companyId;
- }
- } else {
- $filter['r.company_id'] = $this->getCompanyFilter();
- }
- if ($name = Helper::getPostString('name')) {
- $filter['r.name'] = '%'.$name;
- }
- $cri = DbCriteria::simpleCompareWithPage($filter)
- ->setAlias('r')
- ->setSelect('r.*, s.name as company_name')
- ->setJoin('LEFT JOIN wx_company s ON s.id=r.company_id')
- ->setOrder('r.id desc');
- if ($date = Helper::getPostDate('date')) {
- $cri->addBetweenCondition('r.create_date', $date, $date . ' 23:59:59');
- }
- $data = DB::getListWithCriteria($this->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']] ?? '-';
- return $item;
- }, $data['records']);
- }
- Helper::ok($data);
- }
- public function actionDelete()
- {
- $id = Helper::getPostInt('id');
- if ($id < 1) {
- Helper::error('参数错误');
- }
- $data = DB::getInfoById($this->table, $id);
- if (!$data || !$this->checkCompanyId($data['company_id'])) {
- Helper::error('您没有权限操作此数据');
- }
- Db::updateById($this->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($this->table, $id);
- if (!$data || !$this->checkCompanyId($data['company_id'])) {
- Helper::error('您没有权限操作此数据');
- }
- $this->_save($id);
- }
- private function _save($id = 0)
- {
- $data = [
- 'name' => Helper::getPostString('name'),
- 'company_id' => Helper::getPostInt('company_id'),
- 'phone' => Helper::getPostString('phone'),
- 'weixin' => Helper::getPostString('weixin'),
- 'position' => Helper::getPostString('position'),
- 'memo' => Helper::getPostString('memo'),
- ];
- $notNullField = ["name", "company_id", "phone", "weixin", "position"];
- $allowEmptyField = ['weixin'];
- // 空字段检测
- if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
- Helper::error('参数错误');
- }
- $this->dobuleCheck();
- if ($id) {
- DB::updateById($this->table, $data, $id);
- } else {
- DB::addData($this->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('参数错误');
- }
- $data = DB::getInfoById($this->table, $id);
- if (!$data || !$this->checkCompanyId($data['company_id'])) {
- Helper::error('您没有权限操作此数据');
- }
- if (!in_array($attr, [])) {
- Helper::error('参数错误2');
- }
- if (DB::updateById($this->table, [$attr => $value], $id) === false) {
- Helper::error('更新失败');
- }
- Helper::ok();
- }
- }
|