CompanyRelationController.php 4.6 KB

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