CompanyController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. 't.is_del' => 0,
  52. 't.phone' => Helper::getPostString('phone'),
  53. 't.id' => $this->getcompanyFilter(),
  54. ];
  55. if ($name = Helper::getPostString('name')) {
  56. $filter['t.name'] = '%' . $name;
  57. }
  58. $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
  59. $filter['t.province'] = $address[0]?? null;
  60. $filter['t.city'] = $address[1]?? null;
  61. $filter['t.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['t.id'] = $ids?: -1;
  69. }
  70. if ($last_user_id = Helper::getPostInt('last_user_id')) {
  71. $filter['t.last_user_id'] = $last_user_id;
  72. }
  73. if ($bind_user_id = Helper::getPostInt('bind_user_id')) {
  74. $filter['t.bind_user_id'] = $bind_user_id;
  75. }
  76. // 被删除的关系要排除
  77. $delContactIds = Helper::arrayColumn(DB::getListWithCriteria('wx_company_contact', DbCriteria::simpleCompare(['is_del' => 1])->setSelect('id')), 'id');
  78. $followWhere = '';
  79. if ($delContactIds) {
  80. $followWhere = ' AND sf.contact_id NOT IN (' . implode(',', $delContactIds) . ')';
  81. }
  82. $cri = DbCriteria::simpleCompareWithPage($filter)
  83. ->setAlias('t')
  84. ->setSelect('t.*, group_concat(sf.id) AS follow_ids')
  85. ->setJoin('LEFT JOIN wx_company_follow AS sf ON sf.company_id = t.id ' . $followWhere)
  86. ->setGroup('t.id')
  87. ->setOrder('t.id desc');
  88. $last_date = $_POST['last_date'];
  89. if (is_array($last_date) && count($last_date) == 2 && $last_date[0] < $last_date[1]) {
  90. $cri->addcondition("t.last_date > '{$last_date[0]}' and t.last_date < '{$last_date[1]}'");
  91. }
  92. $data = DB::getListWithCriteria(self::$table, $cri);
  93. if (!empty($data['records'])) {
  94. $users = Helper::arrayColumn(
  95. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username,avatar')),
  96. null,
  97. 'id'
  98. );
  99. $data['records'] = FollowSrv::formatWithFollowList($data['records'], 'wx_company_follow', $users);
  100. $data['records'] = array_map(function ($item) use ($users) {
  101. $item['last_user_name'] = $users[$item['last_user_id']]['username'] ?? '-';
  102. $item['bind_user_name'] = $users[$item['bind_user_id']]['username'] ?? '-';
  103. return $item;
  104. }, $data['records']);
  105. }
  106. Helper::ok($data);
  107. }
  108. public function actionDelete()
  109. {
  110. $id = Helper::getPostInt('id');
  111. if ($id < 1) {
  112. Helper::error('参数错误');
  113. }
  114. if (!$this->checkCompanyId($id)) {
  115. Helper::error('您没有权限操作此数据');
  116. }
  117. Db::updateById(self::$table, ['is_del' => 1], $id);
  118. Helper::ok();
  119. }
  120. public function actionAdd()
  121. {
  122. $this->_save();
  123. }
  124. public function actionEdit()
  125. {
  126. $id = Helper::getPostInt('id');
  127. if (!$id) {
  128. Helper::error('参数错误');
  129. }
  130. if (!$this->checkCompanyId($id)) {
  131. Helper::error('您没有权限操作此数据');
  132. }
  133. $this->_save($id);
  134. }
  135. private function _save($id = 0)
  136. {
  137. $data = [
  138. 'name' => Helper::getPostString('name'),
  139. 'address' => Helper::getPostString('address'),
  140. 'memo' => Helper::getPostString('memo'),
  141. 'bind_user_id' => Helper::getPostInt('bind_user_id'),
  142. ];
  143. // 空字段检测
  144. if (!Helper::checkEmptyKey($data, ['name', 'address', 'memo'], ['memo'])) {
  145. Helper::error('参数错误');
  146. }
  147. // 处理地区
  148. $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
  149. $district = array_filter($district);
  150. if (count($district) != 2) {
  151. Helper::error('地区参数错误');
  152. }
  153. $data['province'] = $district[0];
  154. $data['city'] = $district[1];
  155. $data['area'] = '';
  156. // 关联食堂
  157. $canteens = $_POST['canteens']?? [];
  158. if (!$canteens) {
  159. Helper::error('请选择关联的食堂');
  160. }
  161. foreach ($canteens as $k => $canteen) {
  162. $canteens[$k] = array_filter(explode(',', $canteen));
  163. if (count($canteens[$k]) != 2 || empty($canteens[$k][0]) || empty($canteens[$k][1])) {
  164. Helper::error('选择的食堂参数有误 ' . json_encode($canteens));
  165. }
  166. }
  167. $name = $data['name'];
  168. // 检测名称重复
  169. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  170. if ($id > 0) {
  171. $cri->addCondition('id!=' . $id);
  172. }
  173. $isEdit = $id > 0;
  174. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  175. Helper::error('公司名称已存在 ' . $fid);
  176. }
  177. $this->dobuleCheck();
  178. $trans = \Yii::app()->db->beginTransaction();
  179. try {
  180. if ($isEdit) {
  181. DB::updateById(self::$table, $data, $id);
  182. DB::deleteByCondition('company_canteen_relation', ['company_id' => $id]);
  183. } else {
  184. $id = DB::addData(self::$table, $data);
  185. if (!$id) {
  186. throw new \Exception('添加失败');
  187. }
  188. // 给用户操作权限
  189. $user = DB::getInfoById('useradmin', $this->getUserId());
  190. if (!str_contains($user['company_ids'], '-1')) {
  191. DB::updateById(
  192. 'useradmin',
  193. ['company_ids' => trim($user['company_ids'] . ',' . $id, ',')],
  194. $this->getUserId()
  195. );
  196. }
  197. }
  198. $batchArr = [];
  199. foreach ($canteens as $canteen) {
  200. $batchArr[] = [
  201. 'company_id' => $id,
  202. 'school_id' => $canteen[0],
  203. 'canteen_id' => $canteen[1],
  204. ];
  205. }
  206. DB::safeBatchInsert('company_canteen_relation', $batchArr);
  207. $trans->commit();
  208. } catch (\Exception $e) {
  209. $trans->rollback();
  210. Helper::error($e->getMessage());
  211. }
  212. $this->clearAuth();
  213. $this->clearAuthByCity($data['city']);
  214. Helper::ok();
  215. }
  216. public function actionUpdateAttr()
  217. {
  218. $id = Helper::getPostInt('id');
  219. $attr = Helper::getPostString('attr');
  220. $value = Helper::getPostString('value');
  221. if ($id <= 0 || !$attr) {
  222. Helper::error('参数错误');
  223. }
  224. if (!$this->checkCompanyId($id)) {
  225. Helper::error('您没有权限操作此数据');
  226. }
  227. if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
  228. Helper::error('参数错误2');
  229. }
  230. if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
  231. Helper::error('参数错误3');
  232. }
  233. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  234. Helper::error('更新失败');
  235. }
  236. Helper::ok();
  237. }
  238. }