CompanyRelationController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if ($bind_user_id = Helper::getPostInt('bind_user_id')) {
  40. $filter['r.bind_user_id'] = $bind_user_id;
  41. }
  42. if ($last_user_id = Helper::getPostInt('last_user_id')) {
  43. $filter['r.last_user_id'] = $last_user_id;
  44. }
  45. $cri = DbCriteria::simpleCompareWithPage($filter)
  46. ->setAlias('r')
  47. ->setSelect('r.*, s.name as company_name, group_concat(sf.id) AS follow_ids')
  48. ->setJoin('LEFT JOIN wx_company s ON s.id=r.company_id')
  49. ->addJoin('LEFT JOIN wx_company_follow AS sf ON sf.contact_id = r.id')
  50. ->setGroup('r.id')
  51. ->setOrder('r.id desc');
  52. if ($date = Helper::getPostDate('date')) {
  53. $cri->addBetweenCondition('r.create_date', $date, $date . ' 23:59:59');
  54. }
  55. $last_date = $_POST['last_date'];
  56. if (is_array($last_date) && count($last_date) == 2 && $last_date[0] < $last_date[1]) {
  57. $cri->addcondition("r.last_date > '{$last_date[0]}' and r.last_date < '{$last_date[1]}'");
  58. }
  59. $data = DB::getListWithCriteria($this->table, $cri);
  60. if (!empty($data['records'])) {
  61. $users = Helper::arrayColumn(
  62. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username,avatar')),
  63. null,
  64. 'id'
  65. );
  66. $data['records'] = FollowSrv::formatWithFollowList($data['records'], 'wx_company_follow', $users);
  67. $data['records'] = array_map(function ($item) use ($users) {
  68. $item['last_user_name'] = $users[$item['last_user_id']]['username'] ?? '-';
  69. return $item;
  70. }, $data['records']);
  71. }
  72. Helper::ok($data);
  73. }
  74. public function actionDelete()
  75. {
  76. $id = Helper::getPostInt('id');
  77. if ($id < 1) {
  78. Helper::error('参数错误');
  79. }
  80. $data = DB::getInfoById($this->table, $id);
  81. if (!$data || !$this->checkCompanyId($data['company_id'])) {
  82. Helper::error('您没有权限操作此数据');
  83. }
  84. Db::updateById($this->table, ['is_del' => 1], $id);
  85. Helper::ok();
  86. }
  87. public function actionAdd()
  88. {
  89. $this->_save();
  90. }
  91. public function actionEdit()
  92. {
  93. $id = Helper::getPostInt('id');
  94. if (!$id) {
  95. Helper::error('参数错误');
  96. }
  97. $data = DB::getInfoById($this->table, $id);
  98. if (!$data || !$this->checkCompanyId($data['company_id'])) {
  99. Helper::error('您没有权限操作此数据');
  100. }
  101. $this->_save($id);
  102. }
  103. private function _save($id = 0)
  104. {
  105. $data = [
  106. 'name' => Helper::getPostString('name'),
  107. 'company_id' => Helper::getPostInt('company_id'),
  108. 'phone' => Helper::getPostString('phone'),
  109. 'weixin' => Helper::getPostString('weixin'),
  110. 'position' => Helper::getPostString('position'),
  111. 'memo' => Helper::getPostString('memo'),
  112. ];
  113. $notNullField = ["name", "company_id", "phone", "weixin", "position"];
  114. $allowEmptyField = ['weixin'];
  115. // 空字段检测
  116. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  117. Helper::error('参数错误');
  118. }
  119. $this->dobuleCheck();
  120. if ($id) {
  121. DB::updateById($this->table, $data, $id);
  122. } else {
  123. DB::addData($this->table, $data);
  124. }
  125. Helper::ok();
  126. }
  127. public function actionUpdateAttr()
  128. {
  129. $id = Helper::getPostInt('id');
  130. $attr = Helper::getPostString('attr');
  131. $value = Helper::getPostString('value');
  132. if ($id <= 0 || !$attr) {
  133. Helper::error('参数错误');
  134. }
  135. $data = DB::getInfoById($this->table, $id);
  136. if (!$data || !$this->checkCompanyId($data['company_id'])) {
  137. Helper::error('您没有权限操作此数据');
  138. }
  139. if (!in_array($attr, [])) {
  140. Helper::error('参数错误2');
  141. }
  142. if (DB::updateById($this->table, [$attr => $value], $id) === false) {
  143. Helper::error('更新失败');
  144. }
  145. Helper::ok();
  146. }
  147. }