CompanyRelationController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. class CompanyRelationController extends Controller
  3. {
  4. public string $table = 'company_contact';
  5. public function actionInfo()
  6. {
  7. $id = Helper::getPostInt('id');
  8. if ($id <= 0) {
  9. Helper::error('参数错误');
  10. }
  11. $data = DB::getInfoById($this->table, $id);
  12. if (!$data) {
  13. Helper::error('数据不存在');
  14. }
  15. if (!$this->checkCompanyId($data['company_id'])) {
  16. Helper::error('您没有权限操作此数据');
  17. }
  18. Helper::ok($data);
  19. }
  20. public function actionList()
  21. {
  22. $filter = [
  23. 'r.is_del' => 0,
  24. 'r.phone' => Helper::getPostString('phone'),
  25. ];
  26. $companyId = Helper::getPostString('company_id');
  27. if ($companyId) {
  28. if (!$this->checkCompanyId($companyId)) {
  29. $filter['r.company_id'] = -1;
  30. } else {
  31. $filter['r.company_id'] = $companyId;
  32. }
  33. } else {
  34. $filter['r.company_id'] = $this->getCompanyFilter();
  35. }
  36. if ($name = Helper::getPostString('name')) {
  37. $filter['r.name'] = '%'.$name;
  38. }
  39. $cri = DbCriteria::simpleCompareWithPage($filter)
  40. ->setAlias('r')
  41. ->setSelect('r.*, s.name as company_name, group_concat(sf.id) AS follow_ids')
  42. ->setJoin('LEFT JOIN wx_company s ON s.id=r.company_id')
  43. ->addJoin('LEFT JOIN wx_school_follow AS sf ON sf.school_id = r.id')
  44. ->setGroup('r.id')
  45. ->setOrder('r.id desc');
  46. if ($date = Helper::getPostDate('date')) {
  47. $cri->addBetweenCondition('r.create_date', $date, $date . ' 23:59:59');
  48. }
  49. $data = DB::getListWithCriteria($this->table, $cri);
  50. if (!empty($data['records'])) {
  51. $users = Helper::arrayColumn(
  52. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username,avatar')),
  53. null,
  54. 'id'
  55. );
  56. $data['records'] = FollowSrv::formatWithFollowList($data['records'], 'wx_school_follow', $users);
  57. $data['records'] = array_map(function ($item) use ($users) {
  58. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  59. return $item;
  60. }, $data['records']);
  61. }
  62. Helper::ok($data);
  63. }
  64. public function actionDelete()
  65. {
  66. $id = Helper::getPostInt('id');
  67. if ($id < 1) {
  68. Helper::error('参数错误');
  69. }
  70. $data = DB::getInfoById($this->table, $id);
  71. if (!$data || !$this->checkCompanyId($data['company_id'])) {
  72. Helper::error('您没有权限操作此数据');
  73. }
  74. Db::updateById($this->table, ['is_del' => 1], $id);
  75. Helper::ok();
  76. }
  77. public function actionAdd()
  78. {
  79. $this->_save();
  80. }
  81. public function actionEdit()
  82. {
  83. $id = Helper::getPostInt('id');
  84. if (!$id) {
  85. Helper::error('参数错误');
  86. }
  87. $data = DB::getInfoById($this->table, $id);
  88. if (!$data || !$this->checkCompanyId($data['company_id'])) {
  89. Helper::error('您没有权限操作此数据');
  90. }
  91. $this->_save($id);
  92. }
  93. private function _save($id = 0)
  94. {
  95. $data = [
  96. 'name' => Helper::getPostString('name'),
  97. 'company_id' => Helper::getPostInt('company_id'),
  98. 'phone' => Helper::getPostString('phone'),
  99. 'weixin' => Helper::getPostString('weixin'),
  100. 'position' => Helper::getPostString('position'),
  101. 'memo' => Helper::getPostString('memo'),
  102. ];
  103. $notNullField = ["name", "company_id", "phone", "weixin", "position"];
  104. $allowEmptyField = ['weixin'];
  105. // 空字段检测
  106. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  107. Helper::error('参数错误');
  108. }
  109. $this->dobuleCheck();
  110. if ($id) {
  111. DB::updateById($this->table, $data, $id);
  112. } else {
  113. DB::addData($this->table, $data);
  114. }
  115. Helper::ok();
  116. }
  117. public function actionUpdateAttr()
  118. {
  119. $id = Helper::getPostInt('id');
  120. $attr = Helper::getPostString('attr');
  121. $value = Helper::getPostString('value');
  122. if ($id <= 0 || !$attr) {
  123. Helper::error('参数错误');
  124. }
  125. $data = DB::getInfoById($this->table, $id);
  126. if (!$data || !$this->checkCompanyId($data['company_id'])) {
  127. Helper::error('您没有权限操作此数据');
  128. }
  129. if (!in_array($attr, [])) {
  130. Helper::error('参数错误2');
  131. }
  132. if (DB::updateById($this->table, [$attr => $value], $id) === false) {
  133. Helper::error('更新失败');
  134. }
  135. Helper::ok();
  136. }
  137. }