QwUserController.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. package com.fs.qw;
  2. import com.alibaba.fastjson.JSON;
  3. import com.fs.common.annotation.Log;
  4. import com.fs.common.annotation.RepeatSubmit;
  5. import com.fs.common.constant.Constants;
  6. import com.fs.common.core.controller.BaseController;
  7. import com.fs.common.core.domain.AjaxResult;
  8. import com.fs.common.core.domain.R;
  9. import com.fs.common.core.page.TableDataInfo;
  10. import com.fs.common.enums.BusinessType;
  11. import com.fs.common.exception.ServiceException;
  12. import com.fs.common.exception.user.UserPasswordNotMatchException;
  13. import com.fs.common.utils.MessageUtils;
  14. import com.fs.common.utils.ServletUtils;
  15. import com.fs.common.utils.poi.ExcelUtil;
  16. import com.fs.company.domain.Company;
  17. import com.fs.company.domain.CompanyUser;
  18. import com.fs.company.mapper.CompanyUserMapper;
  19. import com.fs.company.service.ICompanyUserService;
  20. import com.fs.core.manager.AsyncManager;
  21. import com.fs.core.manager.factory.AsyncFactory;
  22. import com.fs.fastGpt.domain.FastGptRole;
  23. import com.fs.fastGpt.mapper.FastGptRoleMapper;
  24. import com.fs.core.security.LoginUser;
  25. import com.fs.core.web.service.TokenService;
  26. import com.fs.qw.domain.QwExternalContact;
  27. import com.fs.qw.domain.QwUser;
  28. import com.fs.qw.mapper.QwCompanyMapper;
  29. import com.fs.qw.mapper.QwExternalContactMapper;
  30. import com.fs.qw.param.*;
  31. import com.fs.qw.service.IQwDeptService;
  32. import com.fs.qw.service.IQwExternalContactService;
  33. import com.fs.qw.service.IQwUserService;
  34. import com.fs.qw.vo.QwOptionsVO;
  35. import com.fs.qw.vo.QwUserVO;
  36. import com.fs.qwApi.domain.QwExternalContactAllListResult;
  37. import com.fs.qwApi.domain.inner.ExternalContact;
  38. import com.fs.qwApi.domain.inner.ExternalContactInfo;
  39. import com.fs.qwApi.domain.inner.FollowInfo;
  40. import com.fs.qwApi.param.QwExternalListParam;
  41. import com.fs.qwApi.service.QwApiService;
  42. import com.fs.voice.utils.StringUtil;
  43. import org.slf4j.LoggerFactory;
  44. import org.springframework.beans.factory.annotation.Autowired;
  45. import org.springframework.security.access.prepost.PreAuthorize;
  46. import org.springframework.security.authentication.AuthenticationManager;
  47. import org.springframework.security.authentication.BadCredentialsException;
  48. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  49. import org.springframework.security.core.Authentication;
  50. import org.springframework.web.bind.annotation.*;
  51. import javax.annotation.Resource;
  52. import java.util.Arrays;
  53. import java.util.HashSet;
  54. import java.util.List;
  55. import java.util.Set;
  56. import java.util.stream.Collectors;
  57. /**
  58. * 企微用户Controller
  59. *
  60. * @author fs
  61. * @date 2024-06-20
  62. */
  63. @RestController
  64. @RequestMapping("/qw/user")
  65. public class QwUserController extends BaseController
  66. {
  67. private static final org.slf4j.Logger logger = LoggerFactory.getLogger(QwUserController.class);
  68. @Autowired
  69. private IQwUserService qwUserService;
  70. @Autowired
  71. private TokenService tokenService;
  72. @Autowired
  73. private ICompanyUserService companyUserService;
  74. @Autowired
  75. private IQwExternalContactService qwExternalContactService;
  76. @Autowired
  77. private QwCompanyMapper qwCompanyMapper;
  78. @Autowired
  79. private QwExternalContactMapper qwExternalContactMapper;
  80. @Autowired
  81. private FastGptRoleMapper fastGptRoleMapper;
  82. @Autowired
  83. private IQwDeptService qwDeptService;
  84. @Autowired
  85. private CompanyUserMapper companyUserMapper;
  86. @Autowired
  87. private QwApiService qwApiService;
  88. @Resource
  89. private AuthenticationManager authenticationManager;
  90. /**
  91. * 查询企微员工列表
  92. */
  93. @PreAuthorize("@ss.hasPermi('qw:user:staffList')")
  94. @GetMapping("/staffList")
  95. public TableDataInfo staffList(QwUserListParam qwUser)
  96. {
  97. startPage();
  98. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  99. qwUser.setCompanyId(loginUser.getCompany().getCompanyId());
  100. List<QwUserVO> list = qwUserService.selectQwUserListStaffVO(qwUser);
  101. return getDataTable(list);
  102. }
  103. /**
  104. * 直接授权key
  105. */
  106. @PreAuthorize("@ss.hasPermi('qw:user:authAppKey')")
  107. @PostMapping("/authAppKey")
  108. public R authAppKey(@RequestBody QwUser param){
  109. return qwUserService.authAppKey(param);
  110. }
  111. /**
  112. * 输入授权key
  113. */
  114. @PreAuthorize("@ss.hasPermi('qw:user:authAppKey')")
  115. @PostMapping("/handleInputAuthAppKey")
  116. public R handleInputAuthAppKey(@RequestBody QwUser param){
  117. return qwUserService.handleInputAuthAppKey(param);
  118. }
  119. /**
  120. * 登录企业微信(发起登录)
  121. */
  122. @PreAuthorize("@ss.hasPermi('qw:user:login')")
  123. @PostMapping("/loginQwCode")
  124. public R loginQwCode(@RequestBody QwLoginParam loginParam){
  125. return qwUserService.loginQwCode(loginParam);
  126. }
  127. /**
  128. * 登录请求-刷新获取二维码
  129. */
  130. @PreAuthorize("@ss.hasPermi('qw:user:login')")
  131. @PostMapping("/loginQwCodeUrl")
  132. public R loginQwCodeUrl(@RequestBody QwLoginParam loginParam){
  133. return qwUserService.loginQwCodeUrl(loginParam);
  134. }
  135. /**
  136. * 取redis里的登录二维码
  137. */
  138. @PreAuthorize("@ss.hasPermi('qw:user:login')")
  139. @PostMapping("/getQwCodeUrl")
  140. public R getQwCodeUrl(@RequestBody QwLoginParam loginParam) throws InterruptedException {
  141. return qwUserService.getQwCodeUrl(loginParam);
  142. }
  143. /**
  144. * 登录企业微信(传输验证信息)
  145. */
  146. @PreAuthorize("@ss.hasPermi('qw:user:login')")
  147. @PostMapping("/loginQwCodeMsg")
  148. public R loginQwCodeMsg(@RequestBody QwLoginParam loginParam){
  149. return qwUserService.loginQwCodeMsg(loginParam);
  150. }
  151. /**
  152. * 退出企业微信(退出插件)
  153. */
  154. @PreAuthorize("@ss.hasPermi('qw:user:login')")
  155. @PostMapping("/logoutQwLogout")
  156. public R logoutQwLogout(@RequestBody QwLoginParam loginParam){
  157. return qwUserService.logoutQwLogout(loginParam);
  158. }
  159. // /**
  160. // * 企业微信(修改登录状态)
  161. // */
  162. // @PreAuthorize("@ss.hasPermi('qw:user:login')")
  163. // @PostMapping("/modifyLoginQwStatus")
  164. // public R modifyLoginQwStatus(@RequestBody QwLoginParam loginParam){
  165. // return qwUserService.modifyLoginQwStatus(loginParam);
  166. // }
  167. //
  168. /**
  169. * 查询企业微信登录状态
  170. */
  171. @PreAuthorize("@ss.hasPermi('qw:user:login')")
  172. @PostMapping("/getLoginQwStatus")
  173. public R getLoginQwStatus(@RequestBody QwLoginParam loginParam){
  174. return qwUserService.getLoginQwStatus(loginParam);
  175. }
  176. @PutMapping
  177. public AjaxResult updateUser(@RequestBody QwUser qwUser){
  178. return toAjax(qwUserService.updateQwUser(qwUser));
  179. }
  180. /**
  181. * 企业微信员工账号 绑定 云主机
  182. */
  183. @PreAuthorize("@ss.hasPermi('qw:user:loginIp')")
  184. @Log(title = "绑定 云主机", businessType = BusinessType.INSERT)
  185. @GetMapping("/qwBindCloudHost/{appKey}")
  186. public R qwBindCloudHost(@PathVariable("appKey") String appKey){
  187. return qwUserService.qwBindCloudHost(appKey);
  188. }
  189. /**
  190. * 获取云主机的账密
  191. *
  192. */
  193. @PreAuthorize("@ss.hasPermi('qw:user:cloudAP')")
  194. @PostMapping("/selectCloudAP")
  195. public R selectCloudAP(@RequestBody QwCloudAPParam param) throws Exception {
  196. return qwUserService.selectCloudAP(param);
  197. }
  198. /**
  199. * 根据销售账号密码 获取 他的所有企业微信账号以及云主机和账号密码
  200. */
  201. @PostMapping("/selectCloudByCompany")
  202. public R selectCloudByCompany(@RequestBody QwCloudIPByCompanyParam param) throws Exception {
  203. // 用户验证
  204. Authentication authentication = null;
  205. try
  206. {
  207. // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
  208. authentication = authenticationManager
  209. .authenticate(new UsernamePasswordAuthenticationToken(param.getCompanyAdmin(), param.getCompanyPassWord()));
  210. }
  211. catch (Exception e)
  212. {
  213. if (e instanceof BadCredentialsException)
  214. {
  215. AsyncManager.me().execute(AsyncFactory.recordLogininfor(0l,param.getCompanyAdmin(), Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
  216. throw new UserPasswordNotMatchException();
  217. }
  218. else
  219. {
  220. AsyncManager.me().execute(AsyncFactory.recordLogininfor(0l,param.getCompanyAdmin(), Constants.LOGIN_FAIL, e.getMessage()));
  221. throw new ServiceException(e.getMessage());
  222. }
  223. }
  224. LoginUser loginUser=(LoginUser) authentication.getPrincipal();
  225. return qwUserService.selectCloudByCompany(loginUser.getUser().getCompanyId(),loginUser.getUser().getUserId());
  226. }
  227. /**
  228. * 企业微信员工账号 绑定 云主机
  229. */
  230. @PreAuthorize("@ss.hasPermi('qw:user:bindIp')")
  231. @GetMapping("/qwBindCloudHostByIp/{appKey}/{IP}")
  232. public R qwBindCloudHostByIp(@PathVariable("appKey") String appKey,@PathVariable("IP") String IP){
  233. return qwUserService.qwBindCloudHostByIp(appKey,IP);
  234. }
  235. /**
  236. * 企业微信员工账号 解除绑定 云主机
  237. */
  238. @PreAuthorize("@ss.hasPermi('qw:user:loginIpOut')")
  239. @Log(title = "解除绑定 云主机", businessType = BusinessType.UPDATE)
  240. @GetMapping("/qwUnbindCloudHost/{appKey}")
  241. public R qwUnbindCloudHost(@PathVariable("appKey") String appKey){
  242. return qwUserService.qwUnbindCloudHost(appKey);
  243. }
  244. /**
  245. * 查询企微用户列表
  246. */
  247. // @PreAuthorize("@ss.hasPermi('qw:user:list')")
  248. @GetMapping("/list")
  249. public TableDataInfo list(QwUserListParam qwUser)
  250. {
  251. startPage();
  252. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  253. qwUser.setCompanyId(loginUser.getCompany().getCompanyId());
  254. List<QwUserVO> list = qwUserService.selectQwUserListVO(qwUser);
  255. return getDataTable(list);
  256. }
  257. /**
  258. * 查询企微用户列表
  259. */
  260. @GetMapping("/userList")
  261. public TableDataInfo userList(QwUserListParam qwUser)
  262. {
  263. startPage();
  264. // LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  265. // qwUser.setCompanyId(loginUser.getCompany().getCompanyId());
  266. List<QwUserVO> list = qwUserService.selectAllQwUserListVO(qwUser);
  267. return getDataTable(list);
  268. }
  269. // /**
  270. // * 查询我的企微用户列表
  271. // */
  272. // @PreAuthorize("@ss.hasPermi('qw:user:myList')")
  273. // @GetMapping("/myList")
  274. // public TableDataInfo myList(QwUserParam qwUser)
  275. // {
  276. // startPage();
  277. // LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  278. // qwUser.setCompanyId(loginUser.getCompany().getCompanyId());
  279. // qwUser.setCompanyUserId(loginUser.getCompany().getUserId());
  280. // List<QwUserVO> list = qwUserService.selectQwUserListVO(qwUser);
  281. // return getDataTable(list);
  282. // }
  283. /**
  284. * 导出企微用户列表
  285. */
  286. @PreAuthorize("@ss.hasPermi('qw:user:export')")
  287. @Log(title = "企微用户", businessType = BusinessType.EXPORT)
  288. @GetMapping("/export")
  289. public AjaxResult export(QwUser qwUser)
  290. {
  291. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  292. qwUser.setCompanyId(loginUser.getCompany().getCompanyId());
  293. List<QwUser> list = qwUserService.selectQwUserList(qwUser);
  294. ExcelUtil<QwUser> util = new ExcelUtil<QwUser>(QwUser.class);
  295. return util.exportExcel(list, "企微用户数据");
  296. }
  297. /**
  298. * 查询企微员工列表-用于员工管理绑定
  299. */
  300. @GetMapping("/getQwUserList")
  301. public R getQwUserList()
  302. {
  303. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  304. QwUserParam qwUser = new QwUserParam();
  305. List<String> strings = qwCompanyMapper.selectQwCompanyCorpIdListByCompanyId(loginUser.getCompany().getCompanyId());
  306. qwUser.setCorpId(strings);
  307. if (strings==null||strings.size()==0){
  308. return R.ok().put("data",null);
  309. }
  310. qwUser.setIsDel(0);
  311. List<QwUserVO> list = qwUserService.selectQwUserListBindVO(qwUser);
  312. return R.ok().put("data",list);
  313. }
  314. @GetMapping("/getMyQwUserList")
  315. public R getMyQwUserList()
  316. {
  317. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  318. List<QwOptionsVO> list = qwUserService.selectQwUserListOptionsVOByCompanyUserId(loginUser.getUser().getUserId());
  319. return R.ok().put("data",list);
  320. }
  321. @GetMapping("/getMyQwCompanyList")
  322. public R getMyQwCompanyList()
  323. {
  324. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  325. List<QwOptionsVO> list = qwUserService.selectQwCompanyListOptionsVOByCompanyId(loginUser.getCompany().getCompanyId());
  326. return R.ok().put("data",list);
  327. }
  328. /**
  329. * 获取企微用户详细信息
  330. */
  331. @GetMapping(value = "/{id}")
  332. public AjaxResult getInfo(@PathVariable("id") Long id)
  333. {
  334. return AjaxResult.success(qwUserService.selectQwUserVOById(id));
  335. }
  336. /**
  337. * 批量查询 企微用户详细信息
  338. */
  339. @GetMapping(value = "/getInfo/{ids}")
  340. public AjaxResult getInfoByIds(@PathVariable("ids") Long[] ids)
  341. {
  342. return AjaxResult.success(qwUserService.selectQwUserVOByIds(ids));
  343. }
  344. // /**
  345. // * 新增企微用户
  346. // */
  347. // @PreAuthorize("@ss.hasPermi('qw:user:add')")
  348. // @Log(title = "企微用户", businessType = BusinessType.INSERT)
  349. // @PostMapping
  350. // public R add()
  351. // {
  352. // LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  353. //
  354. // return R.ok(qwUserService.syncQwUser(loginUser.getCompany().getCompanyId()));
  355. // }
  356. /**
  357. * 同步企微用户
  358. */
  359. @RepeatSubmit
  360. @PreAuthorize("@ss.hasPermi('qw:user:sync')")
  361. @Log(title = "企微用户", businessType = BusinessType.INSERT)
  362. @PostMapping("sync/{corpId}")
  363. public R sync(@PathVariable String corpId)
  364. {
  365. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  366. Company company = loginUser.getCompany();
  367. List<String> strings = qwCompanyMapper.selectQwCompanyCorpIdListByCompanyId(company.getCompanyId());
  368. for (String string : strings) {
  369. if (string.equals(corpId)){
  370. qwUserService.syncQwUser(string);
  371. qwDeptService.insertOrUpdateQwDept(string);
  372. System.out.println("同步完成");
  373. }
  374. }
  375. return R.ok();
  376. }
  377. @RepeatSubmit
  378. @PreAuthorize("@ss.hasPermi('qw:user:sync')")
  379. @Log(title = "企微用户", businessType = BusinessType.INSERT)
  380. @PostMapping("syncName/{corpId}")
  381. public R syncName(@PathVariable String corpId)
  382. {
  383. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  384. Company company = loginUser.getCompany();
  385. List<String> strings = qwCompanyMapper.selectQwCompanyCorpIdListByCompanyId(company.getCompanyId());
  386. for (String string : strings) {
  387. if (string.equals(corpId)){
  388. qwUserService.syncQwUserName(string);
  389. }
  390. }
  391. return R.ok();
  392. }
  393. /**
  394. * 绑定AI客服
  395. */
  396. // @PreAuthorize("@ss.hasPermi('qw:user:bindAi')")
  397. @Log(title = "企微用户", businessType = BusinessType.UPDATE)
  398. @PutMapping("/bindAi")
  399. @RepeatSubmit
  400. public R edit(@RequestBody QwUserBindAi param)
  401. {
  402. QwUser qwUser=new QwUser();
  403. qwUser.setId(param.getId());
  404. qwUser.setFastGptRoleId(param.getFastGptRoleId());
  405. FastGptRole role = fastGptRoleMapper.selectFastGptRoleByRoleId(param.getFastGptRoleId());
  406. if (role.getBindCorpId()!=null){
  407. if (role.getBindCorpId().equals(param.getCorpId())){
  408. qwUserService.updateQwUser(qwUser);
  409. }else {
  410. return R.error("该角色已绑定其他企业");
  411. }
  412. }else {
  413. int i = qwUserService.updateQwUser(qwUser);
  414. FastGptRole fastGptRole=new FastGptRole();
  415. fastGptRole.setRoleId(param.getFastGptRoleId());
  416. fastGptRole.setBindCorpId(param.getCorpId());
  417. fastGptRoleMapper.updateFastGptRole(fastGptRole);
  418. if (i>0){
  419. return R.ok();
  420. }else {
  421. return R.error("绑定失败");
  422. }
  423. }
  424. return R.ok();
  425. }
  426. /**
  427. * 解除应用绑定
  428. */
  429. // @PreAuthorize("@ss.hasPermi('fastGpt:fastGptRole:relieve')")
  430. @Log(title = "解除应用", businessType = BusinessType.UPDATE)
  431. @GetMapping("/relieveFastGptRoleById/{id}")
  432. public R relieveFastGptRoleById(@PathVariable("id") Long id)
  433. {
  434. return qwUserService.relieveFastGptRoleById(id);
  435. }
  436. /**
  437. * 绑定企微用户
  438. */
  439. @PreAuthorize("@ss.hasPermi('qw:user:bind')")
  440. @Log(title = "企微用户", businessType = BusinessType.UPDATE)
  441. @PutMapping("/bindQwUser")
  442. @RepeatSubmit
  443. public R bindQwUser(@RequestBody QwUserBingParam qwUserParam)
  444. {
  445. CompanyUser companyUser = companyUserService.selectCompanyUserById(qwUserParam.getCompanyUserId());
  446. String qwUserIdCompanyStr = companyUser.getQwUserId();
  447. Set<String> qwUserIdCompanySet = new HashSet<>();
  448. if (!StringUtil.strIsNullOrEmpty(qwUserIdCompanyStr)) {
  449. String[] qwUserId = qwUserIdCompanyStr.split(",");
  450. qwUserIdCompanySet = Arrays.stream(qwUserId)
  451. .filter(id -> !id.isEmpty())
  452. .collect(Collectors.toSet());
  453. }
  454. if (companyUser!=null){
  455. //选择的企业微信账号为“”
  456. if (StringUtil.strIsNullOrEmpty(qwUserParam.getId())&&qwUserParam.getCompanyUserId()!=null){
  457. //制空企业微信的绑定
  458. qwUserService.updateUserByUserId(qwUserParam.getCompanyUserId());
  459. //制空销售的绑定
  460. companyUserMapper.updateCompanyUserByNullQwUserID(companyUser.getUserId());
  461. }else {
  462. String idParam = qwUserParam.getId();
  463. String[] splitParam = idParam.split(",");
  464. Set<String> splitParamSet = Arrays.stream(splitParam)
  465. .filter(s -> !s.isEmpty())
  466. .collect(Collectors.toSet());
  467. // 找出在 qwUserIdCompanySet 中存在但在 splitParamSet 中不存在的元素
  468. Set<String> difference = new HashSet<>(qwUserIdCompanySet);
  469. difference.removeAll(splitParamSet);
  470. //制空绑定
  471. if (!difference.isEmpty()){
  472. difference.forEach(id->{
  473. qwUserService.updateUnBindUserById(id);
  474. });
  475. }
  476. for (String paramId : splitParamSet) {
  477. QwUser qu= qwUserService.selectQwUserById(Long.parseLong(paramId));
  478. if (qu.getCompanyUserId()!=null&&!qu.getCompanyUserId().equals(qwUserParam.getCompanyUserId())){
  479. return R.error( qu.getQwUserName()+"已经被其他人绑定,请先解绑");
  480. }
  481. CompanyUser user = new CompanyUser();
  482. user.setQwUserId(qwUserParam.getId());
  483. user.setUserId(companyUser.getUserId());
  484. user.setQwStatus(1);
  485. companyUserMapper.updateCompanyUser(user);
  486. QwUser qw = new QwUser();
  487. qw.setCompanyUserId(qwUserParam.getCompanyUserId());
  488. qw.setId(Long.parseLong(paramId));
  489. qw.setStatus(1);
  490. qw.setCompanyId(companyUser.getCompanyId());
  491. qwUserService.updateQwUser(qw);
  492. }
  493. //同步最后单独跑/先不异步了pp
  494. for (String paramId : splitParamSet){
  495. //如果销售没绑定过,全刷,否则只同步新的增加的
  496. if (StringUtil.strIsNullOrEmpty(qwUserIdCompanyStr)){
  497. updateAndSyncQwUser(paramId, companyUser);
  498. }
  499. else if (!StringUtil.strIsNullOrEmpty(qwUserIdCompanyStr) && !qwUserIdCompanySet.contains(paramId)){
  500. updateAndSyncQwUser(paramId, companyUser);
  501. }
  502. }
  503. }
  504. return R.ok();
  505. }
  506. return R.error("绑定失败");
  507. }
  508. private void updateAndSyncQwUser(String paramId, CompanyUser companyUser) {
  509. QwUser qu= qwUserService.selectQwUserById(Long.parseLong(paramId));
  510. qwExternalContactMapper.updateBindUserByQwUser(qu.getCorpId(),qu.getQwUserId(),companyUser.getCompanyId(),companyUser.getUserId());
  511. //根据企微账号和公司id 同步企业微信账号(游标初始为空)
  512. syncMyQwExternalContact(qu,null);
  513. }
  514. /** 修改企微用户的欢迎语 */
  515. @PostMapping("/weclomeQwUser")
  516. public R weclomeQwUser(@RequestBody QwUser qwUser) throws Exception {
  517. return qwUserService.weclomeQwUser(qwUser);
  518. }
  519. /**
  520. * 删除企微用户
  521. */
  522. @PreAuthorize("@ss.hasPermi('qw:user:remove')")
  523. @Log(title = "企微用户", businessType = BusinessType.DELETE)
  524. @DeleteMapping("/{ids}")
  525. public AjaxResult remove(@PathVariable Long[] ids)
  526. {
  527. return toAjax(qwUserService.deleteQwUserByIds(ids));
  528. }
  529. /**
  530. * 获取企业微信用户列表
  531. */
  532. @GetMapping("/qwUserList/{corpId}")
  533. public TableDataInfo qwUserList(@PathVariable String corpId)
  534. {
  535. // LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  536. // Long companyId = loginUser.getCompany().getCompanyId();
  537. List<QwOptionsVO> list = qwUserService.selectQwUserListOptionsVO(corpId);
  538. return getDataTable(list);
  539. }
  540. @GetMapping("/getQwUserAll")
  541. public AjaxResult getQwUserAll(){
  542. return AjaxResult.success(qwUserService.getQwUserAll());
  543. }
  544. public R syncMyQwExternalContact(QwUser qwUser,String getNextCursor ) {
  545. String qwUserId = qwUser.getQwUserId();
  546. String corpId = qwUser.getCorpId();
  547. Long companyId = qwUser.getCompanyId();
  548. QwExternalListParam param = new QwExternalListParam();
  549. param.setLimit(100);
  550. param.setUserid_list(Arrays.asList(qwUserId));
  551. param.setCursor(getNextCursor);
  552. QwExternalContactAllListResult list = qwApiService.getAllExternalcontactList(param, corpId);
  553. logger.info("批量获取客户详情" + list);
  554. if (list.getErrcode() == 0) {
  555. List<ExternalContactInfo> externalContactList = list.getExternal_contact_list();
  556. for (ExternalContactInfo externalContactInfo : externalContactList) {
  557. ExternalContact externalContact = externalContactInfo.getExternal_contact();
  558. FollowInfo followInfo = externalContactInfo.getFollow_info();
  559. QwExternalContact qwExternalContact = qwExternalContactMapper.selectQwExternalContactUserIdAndExternalIdAndCompanyId(externalContact.getExternal_userid(), qwUserId, corpId);
  560. logger.info("客户详情" + qwExternalContact);
  561. if (qwExternalContact == null) {
  562. qwExternalContact = new QwExternalContact();
  563. qwExternalContact.setUserId(qwUserId); // 设置属于用户ID
  564. qwExternalContact.setExternalUserId(externalContact.getExternal_userid()); // 设置外部联系人ID
  565. qwExternalContact.setCorpId(corpId); // 设置企业ID
  566. qwExternalContact.setCompanyId(companyId); // 设置公司ID
  567. }
  568. // 设置公共属性
  569. qwExternalContact.setCompanyUserId(qwUser.getCompanyUserId());
  570. qwExternalContact.setQwUserId(qwUser.getId());
  571. qwExternalContact.setName(externalContact.getName()); // 设置名称
  572. qwExternalContact.setAvatar(externalContact.getAvatar()); // 设置头像
  573. qwExternalContact.setType(externalContact.getType()); // 设置外部联系人类型(1微信用户,2企业微信用户)
  574. qwExternalContact.setGender(externalContact.getGender()); // 设置性别 (0-未知, 1-男性, 2-女性)
  575. qwExternalContact.setDescription(followInfo.getDescription()); // 设置描述信息
  576. qwExternalContact.setRemark(followInfo.getRemark());
  577. // if (followInfo.getTag_id() != null && !followInfo.getTag_id().isEmpty()) {
  578. qwExternalContact.setTagIds(JSON.toJSONString(followInfo.getTag_id())); // 设置标签ID
  579. // }
  580. // if (followInfo.getRemark_mobiles() != null && !followInfo.getRemark_mobiles().isEmpty()) {
  581. qwExternalContact.setRemarkMobiles(JSON.toJSONString(followInfo.getRemark_mobiles())); // 设置备注电话号码
  582. // }
  583. qwExternalContact.setState(followInfo.getState());
  584. if (followInfo.getState() != null && !followInfo.getState().isEmpty()) {
  585. String s = "way:" + corpId + ":";
  586. if (followInfo.getState().contains(s)) {
  587. String wayId = followInfo.getState().substring(followInfo.getState().indexOf(s) + s.length());
  588. qwExternalContact.setWayId(Long.parseLong(wayId));
  589. }
  590. }
  591. qwExternalContact.setRemarkCorpName(followInfo.getRemark_corp_name()); // 设置备注企业名称
  592. qwExternalContact.setAddWay(followInfo.getAdd_way()); // 设置来源
  593. qwExternalContact.setOperUserid(followInfo.getOper_userid()); // 设置oper用户ID
  594. // 根据是否存在记录进行更新或插入
  595. if (qwExternalContact.getId() != null) {
  596. qwExternalContactMapper.updateQwExternalContact(qwExternalContact);
  597. } else {
  598. qwExternalContactMapper.insertQwExternalContact(qwExternalContact);
  599. }
  600. }
  601. }else {
  602. return R.error("同步失败:"+list.getErrmsg());
  603. }
  604. if (!StringUtil.strIsNullOrEmpty(list.getNext_cursor())){
  605. syncMyQwExternalContact(qwUser,list.getNext_cursor());
  606. }
  607. return R.ok();
  608. }
  609. }