CompanyController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. class CompanyController extends Controller
  3. {
  4. public static string $table = 'company';
  5. public function actionInfo()
  6. {
  7. $id = Helper::getPostInt('id');
  8. if ($id <= 0) {
  9. Helper::error('参数错误');
  10. }
  11. if (!$this->checkCompanyId($id)) {
  12. Helper::error('您没有权限操作此数据');
  13. }
  14. $data = DB::getInfoById(self::$table, $id);
  15. if (!$data) {
  16. Helper::error('数据不存在');
  17. }
  18. $cri = DbCriteria::simpleCompare(['id' => [$data['last_user_id'], $data['bind_user_id']]])->setSelect('id, username');
  19. $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), 'username', 'id');
  20. $data['last_user_name'] = $users[$data['last_user_id']] ?? '';
  21. $data['bind_user_name'] = $users[$data['bind_user_id']] ?? '';
  22. $data['distinct'] = [
  23. $data['province'],
  24. $data['city'],
  25. // $data['area'],
  26. ];
  27. // 关联食堂
  28. $data['canteen_names'] = [];
  29. $data['canteens'] = [];
  30. $cri = DbCriteria::simpleCompare(['t.company_id' => $id])
  31. ->setAlias('t')
  32. ->setSelect('t.school_id, t.canteen_id, s.name as school_name, c.name as canteen_name')
  33. ->setJoin('left join wx_school s on t.school_id = s.id')
  34. ->addJoin('left join wx_canteen c on t.canteen_id = c.id');
  35. $canteens = DB::getListWithCriteria('company_canteen_relation', $cri);
  36. foreach ($canteens['records'] as $item) {
  37. $data['canteens'][] = [(int)$item['school_id'], (int)$item['canteen_id']];
  38. $data['canteen_names'][] = "{$item['canteen_name']}({$item['school_name']})";
  39. }
  40. // 关系人
  41. $relations = DB::getListWithCriteria(
  42. 'company_contact',
  43. DbCriteria::simpleCompare(['company_id' => $id])->setSelect('id, name, phone, position, weixin'),
  44. );
  45. $data['relations'] = $relations['records'];
  46. Helper::ok($data);
  47. }
  48. public function actionList()
  49. {
  50. $filter = [
  51. 'is_del' => 0,
  52. 'phone' => Helper::getPostString('phone'),
  53. 'id' => $this->getcompanyFilter(),
  54. ];
  55. if ($name = Helper::getPostString('name')) {
  56. $filter['name'] = '%' . $name;
  57. }
  58. $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
  59. $filter['province'] = $address[0]?? null;
  60. $filter['city'] = $address[1]?? null;
  61. $filter['area'] = $address[2]?? null;
  62. if ($school_id = Helper::getPostInt('school_id')) {
  63. $cri = DbCriteria::simpleCompare(['school_id' => $school_id])->setSelect('company_id');
  64. $ids = Helper::arrayColumn(
  65. DB::getListWithCriteria('company_canteen_relation', $cri),
  66. 'company_id'
  67. );
  68. $filter['id'] = $ids?: -1;
  69. }
  70. // 被删除的关系要排除
  71. $delContactIds = Helper::arrayColumn(DB::getListWithCriteria('wx_company_contact', DbCriteria::simpleCompare(['is_del' => 1])->setSelect('id')), 'id');
  72. $followWhere = '';
  73. if ($delContactIds) {
  74. $followWhere = ' AND sf.contact_id NOT IN (' . implode(',', $delContactIds) . ')';
  75. }
  76. $cri = DbCriteria::simpleCompareWithPage($filter)
  77. ->setAlias('t')
  78. ->setSelect('t.*, group_concat(sf.id) AS follow_ids')
  79. ->setJoin('LEFT JOIN wx_company_follow AS sf ON sf.company_id = t.id ' . $followWhere)
  80. ->setGroup('t.id')
  81. ->setOrder('t.id desc');
  82. $data = DB::getListWithCriteria(self::$table, $cri);
  83. if (!empty($data['records'])) {
  84. $users = Helper::arrayColumn(
  85. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username,avatar')),
  86. null,
  87. 'id'
  88. );
  89. $data['records'] = FollowSrv::formatWithFollowList($data['records'], 'wx_school_follow', $users);
  90. $data['records'] = array_map(function ($item) use ($users) {
  91. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  92. $item['bind_user_name'] = $users[$item['bind_user_id']] ?? '-';
  93. return $item;
  94. }, $data['records']);
  95. }
  96. Helper::ok($data);
  97. }
  98. public function actionDelete()
  99. {
  100. $id = Helper::getPostInt('id');
  101. if ($id < 1) {
  102. Helper::error('参数错误');
  103. }
  104. if (!$this->checkCompanyId($id)) {
  105. Helper::error('您没有权限操作此数据');
  106. }
  107. Db::updateById(self::$table, ['is_del' => 1], $id);
  108. Helper::ok();
  109. }
  110. public function actionAdd()
  111. {
  112. $this->_save();
  113. }
  114. public function actionEdit()
  115. {
  116. $id = Helper::getPostInt('id');
  117. if (!$id) {
  118. Helper::error('参数错误');
  119. }
  120. if (!$this->checkCompanyId($id)) {
  121. Helper::error('您没有权限操作此数据');
  122. }
  123. $this->_save($id);
  124. }
  125. private function _save($id = 0)
  126. {
  127. $data = [
  128. 'name' => Helper::getPostString('name'),
  129. 'address' => Helper::getPostString('address'),
  130. 'memo' => Helper::getPostString('memo'),
  131. 'bind_user_id' => Helper::getPostInt('bind_user_id'),
  132. ];
  133. // 空字段检测
  134. if (!Helper::checkEmptyKey($data, ['name', 'address', 'memo'], ['memo'])) {
  135. Helper::error('参数错误');
  136. }
  137. // 处理地区
  138. $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
  139. $district = array_filter($district);
  140. if (count($district) != 2) {
  141. Helper::error('地区参数错误');
  142. }
  143. $data['province'] = $district[0];
  144. $data['city'] = $district[1];
  145. $data['area'] = '';
  146. // 关联食堂
  147. $canteens = $_POST['canteens']?? [];
  148. if (!$canteens) {
  149. Helper::error('请选择关联的食堂');
  150. }
  151. foreach ($canteens as $k => $canteen) {
  152. $canteens[$k] = array_filter(explode(',', $canteen));
  153. if (count($canteens[$k]) != 2 || empty($canteens[$k][0]) || empty($canteens[$k][1])) {
  154. Helper::error('选择的食堂参数有误 ' . json_encode($canteens));
  155. }
  156. }
  157. $name = $data['name'];
  158. // 检测名称重复
  159. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  160. if ($id > 0) {
  161. $cri->addCondition('id!=' . $id);
  162. }
  163. $isEdit = $id > 0;
  164. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  165. Helper::error('公司名称已存在 ' . $fid);
  166. }
  167. $this->dobuleCheck();
  168. $trans = \Yii::app()->db->beginTransaction();
  169. try {
  170. if ($isEdit) {
  171. DB::updateById(self::$table, $data, $id);
  172. DB::deleteByCondition('company_canteen_relation', ['company_id' => $id]);
  173. } else {
  174. $id = DB::addData(self::$table, $data);
  175. if (!$id) {
  176. throw new \Exception('添加失败');
  177. }
  178. // 给用户操作权限
  179. $user = DB::getInfoById('useradmin', $this->getUserId());
  180. if (!str_contains($user['company_ids'], '-1')) {
  181. DB::updateById(
  182. 'useradmin',
  183. ['company_ids' => trim($user['company_ids'] . ',' . $id, ',')],
  184. $this->getUserId()
  185. );
  186. }
  187. }
  188. $batchArr = [];
  189. foreach ($canteens as $canteen) {
  190. $batchArr[] = [
  191. 'company_id' => $id,
  192. 'school_id' => $canteen[0],
  193. 'canteen_id' => $canteen[1],
  194. ];
  195. }
  196. DB::safeBatchInsert('company_canteen_relation', $batchArr);
  197. $trans->commit();
  198. } catch (\Exception $e) {
  199. $trans->rollback();
  200. Helper::error($e->getMessage());
  201. }
  202. if (!$isEdit && $data['city']) {
  203. $this->clearAuthByCity($data['city']);
  204. }
  205. Helper::ok();
  206. }
  207. public function actionUpdateAttr()
  208. {
  209. $id = Helper::getPostInt('id');
  210. $attr = Helper::getPostString('attr');
  211. $value = Helper::getPostString('value');
  212. if ($id <= 0 || !$attr) {
  213. Helper::error('参数错误');
  214. }
  215. if (!$this->checkCompanyId($id)) {
  216. Helper::error('您没有权限操作此数据');
  217. }
  218. if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
  219. Helper::error('参数错误2');
  220. }
  221. if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
  222. Helper::error('参数错误3');
  223. }
  224. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  225. Helper::error('更新失败');
  226. }
  227. Helper::ok();
  228. }
  229. }