CompanyRelationController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. Helper::ok($data);
  16. }
  17. public function actionList()
  18. {
  19. $filter = [
  20. 'r.is_del' => 0,
  21. 'r.phone' => Helper::getPostString('phone'),
  22. 'r.company_id' => Helper::getPostInt('company_id') ?: null,
  23. ];
  24. if ($name = Helper::getPostString('name')) {
  25. $filter['r.name'] = '%'.$name;
  26. }
  27. $cri = DbCriteria::simpleCompareWithPage($filter)
  28. ->setAlias('r')
  29. ->setSelect('r.*, s.name as company_name')
  30. ->setJoin('LEFT JOIN wx_company s ON s.id=r.company_id')
  31. ->setOrder('r.id desc');
  32. if ($date = Helper::getPostDate('date')) {
  33. $cri->addBetweenCondition('r.create_date', $date, $date . ' 23:59:59');
  34. }
  35. $data = DB::getListWithCriteria($this->table, $cri);
  36. if (!empty($data['records'])) {
  37. $users = Helper::arrayColumn(
  38. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
  39. 'username',
  40. 'id'
  41. );
  42. $data['records'] = array_map(function ($item) use ($users) {
  43. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  44. return $item;
  45. }, $data['records']);
  46. }
  47. Helper::ok($data);
  48. }
  49. /**
  50. * 下拉列表获取
  51. * @return void
  52. */
  53. public function actionGetSelectList()
  54. {
  55. $cri = DbCriteria::simpleCompare(['is_del' => 0])->setSelect('id, name');
  56. $companys = Helper::arrayColumn(DB::getListWithCriteria('company', $cri), null, 'id');
  57. if (empty($companys)) {
  58. Helper::ok();
  59. }
  60. $cri1 = DbCriteria::simpleCompare(['is_del' => 0])->setSelect('id, name, company_id');
  61. $relations = DB::getListWithCriteria($this->table, $cri1);
  62. foreach ($relations['records'] as $relation) {
  63. $sid = $relation['company_id'];
  64. if (!isset($companys[$sid])) {
  65. continue;
  66. }
  67. if (!isset($companys[$sid]['children'])) {
  68. $companys[$sid]['children'] = [];
  69. }
  70. $companys[$sid]['children'][] = [
  71. 'id' => $relation['id'],
  72. 'name' => $relation['name'],
  73. ];
  74. }
  75. Helper::ok(array_values($companys));
  76. }
  77. public function actionDelete()
  78. {
  79. $id = Helper::getPostInt('id');
  80. if ($id < 1) {
  81. Helper::error('参数错误');
  82. }
  83. Db::updateById($this->table, ['is_del' => 1], $id);
  84. Helper::ok();
  85. }
  86. public function actionAdd()
  87. {
  88. $this->_save();
  89. }
  90. public function actionEdit()
  91. {
  92. $id = Helper::getPostInt('id');
  93. if (!$id) {
  94. Helper::error('参数错误');
  95. }
  96. $this->_save($id);
  97. }
  98. private function _save($id = 0)
  99. {
  100. $data = [
  101. 'name' => Helper::getPostString('name'),
  102. 'company_id' => Helper::getPostInt('company_id'),
  103. 'phone' => Helper::getPostString('phone'),
  104. 'weixin' => Helper::getPostString('weixin'),
  105. 'position' => Helper::getPostString('position'),
  106. 'memo' => Helper::getPostString('memo'),
  107. ];
  108. $notNullField = ["name", "company_id", "phone", "weixin", "position"];
  109. $allowEmptyField = [];
  110. // 空字段检测
  111. if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) {
  112. Helper::error('参数错误');
  113. }
  114. if ($id) {
  115. DB::updateById($this->table, $data, $id);
  116. } else {
  117. DB::addData($this->table, $data);
  118. }
  119. Helper::ok();
  120. }
  121. public function actionUpdateAttr()
  122. {
  123. $id = Helper::getPostInt('id');
  124. $attr = Helper::getPostString('attr');
  125. $value = Helper::getPostString('value');
  126. if ($id <= 0 || !$attr) {
  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. }