WeCustomerSeasController.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package com.linkwechat.controller;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.collection.ListUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.linkwechat.common.annotation.PhoneEncryptMethod;
  6. import com.linkwechat.common.core.controller.BaseController;
  7. import com.linkwechat.common.core.domain.AjaxResult;
  8. import com.linkwechat.common.core.domain.entity.SysUser;
  9. import com.linkwechat.common.core.page.TableDataInfo;
  10. import com.linkwechat.common.exception.wecom.WeComException;
  11. import com.linkwechat.common.utils.*;
  12. import com.linkwechat.common.utils.poi.ExcelUtil;
  13. import com.linkwechat.common.utils.poi.LwExcelUtil;
  14. import com.linkwechat.domain.WeCustomerSeas;
  15. import com.linkwechat.domain.live.WeLive;
  16. import com.linkwechat.domain.wecom.vo.customer.seas.CustomerSeasRecordVo;
  17. import com.linkwechat.service.IWeCustomerSeasService;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.annotation.*;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import java.text.MessageFormat;
  22. import java.util.*;
  23. import java.util.stream.Collectors;
  24. import java.util.stream.IntStream;
  25. /**
  26. * 客户公海
  27. */
  28. @RestController
  29. @RequestMapping(value = "/seas")
  30. public class WeCustomerSeasController extends BaseController {
  31. @Autowired
  32. private IWeCustomerSeasService iWeCustomerSeasService;
  33. /**
  34. * 客户公海模版下载
  35. * @return
  36. */
  37. @GetMapping("/importTemplate")
  38. public void importTemplate()
  39. {
  40. LwExcelUtil.exprotForWeb(
  41. ServletUtils.getResponse(), WeCustomerSeas.class, ListUtil.toList(new WeCustomerSeas()),"客户公海"
  42. );
  43. }
  44. /**
  45. * 导入公海数据
  46. * @param file
  47. * @param weCustomerSea
  48. * @return
  49. */
  50. @PostMapping("/importData")
  51. public AjaxResult importData(MultipartFile file, WeCustomerSeas weCustomerSea) throws Exception {
  52. ExcelUtil<WeCustomerSeas> util = new ExcelUtil<WeCustomerSeas>(WeCustomerSeas.class);
  53. List<WeCustomerSeas> weCustomerSeas = util.importExcel(file.getInputStream());
  54. String tip=new String("成功导入{0}条,去重复{1}条");
  55. if(CollectionUtil.isNotEmpty(weCustomerSeas)){
  56. if(StringUtils.isEmpty(weCustomerSea.getAddUserName())&&StringUtils.isEmpty(weCustomerSea.getAddUserId())){
  57. return AjaxResult.error("分配人不可为空!");
  58. }
  59. //过滤不符合规范的手机号
  60. List<WeCustomerSeas> noRuleWeCustomerSeas
  61. = weCustomerSeas.stream().filter(s -> StringUtils.isMobile(s.getPhone())).collect(Collectors.toList());
  62. if(CollectionUtil.isEmpty(noRuleWeCustomerSeas)){
  63. return AjaxResult.error("请传入合法手机号!");
  64. }
  65. //过滤字段为空的数据
  66. List<WeCustomerSeas> deduplicationSeas = noRuleWeCustomerSeas.stream().filter(s -> StringUtils.isNotEmpty(s.getCustomerName())
  67. || StringUtils.isNotEmpty(s.getPhone())).collect(Collectors.toList());
  68. if(CollectionUtil.isEmpty(deduplicationSeas)){
  69. return AjaxResult.error("导入用户数据不能为空!");
  70. }
  71. //根据手机号去重(去除excel中重复的号码)
  72. List<WeCustomerSeas> deduplicationSeasNoRepeat=deduplicationSeas.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
  73. new TreeSet<>(Comparator.comparing(WeCustomerSeas :: getPhone))), ArrayList::new));
  74. //根据手机号去除数据库与excel中重复号码
  75. List<WeCustomerSeas> dbExist = iWeCustomerSeasService.list(new LambdaQueryWrapper<WeCustomerSeas>()
  76. .in(WeCustomerSeas::getPhone, deduplicationSeasNoRepeat.stream()
  77. .map(WeCustomerSeas::getPhone).collect(Collectors.toList())));
  78. if(CollectionUtil.isNotEmpty(dbExist)){
  79. List<WeCustomerSeas> noRepetWeCustomerSeas
  80. = deduplicationSeasNoRepeat.stream().filter(item ->
  81. !dbExist.stream().map(e->e.getPhone()).collect(Collectors.toList())
  82. .contains(item.getPhone())).collect(Collectors.toList());
  83. deduplicationSeasNoRepeat.clear();
  84. deduplicationSeasNoRepeat.addAll(
  85. noRepetWeCustomerSeas
  86. );
  87. }
  88. if(CollectionUtil.isNotEmpty(deduplicationSeasNoRepeat)){
  89. Long tabaleExcelId= SnowFlakeUtil.nextId();
  90. List<String> userIds = ListUtil.toList(weCustomerSea.getAddUserId().split(","));
  91. List<String> userNames = ListUtil.toList(weCustomerSea.getAddUserName().split(","));
  92. IntStream.range(0,deduplicationSeasNoRepeat.size()).forEach(i->{
  93. WeCustomerSeas k = deduplicationSeasNoRepeat.get(i);
  94. if(Objects.nonNull(k)){
  95. k.setTagIds(weCustomerSea.getTagIds());
  96. k.setTagNames(weCustomerSea.getTagNames());
  97. k.setAddUserId( userIds.get(Math.floorMod(i, userIds.size())));
  98. k.setAddUserName(userNames.get(Math.floorMod(i, userNames.size())));
  99. k.setTableExcelName(file.getOriginalFilename());
  100. k.setTableExcelId(tabaleExcelId);
  101. }
  102. });
  103. if(iWeCustomerSeasService.saveBatch(deduplicationSeasNoRepeat)){
  104. //发送提醒
  105. deduplicationSeasNoRepeat.stream().collect(Collectors.groupingBy(WeCustomerSeas::getAddUserId)).forEach((k,v)->{
  106. iWeCustomerSeasService.remidUser(ListUtil.toList(k),v.size());
  107. });
  108. }
  109. tip = MessageFormat.format(tip, new Object[]{new Integer(deduplicationSeasNoRepeat.size()).toString(),
  110. new Integer( weCustomerSeas.size()-deduplicationSeasNoRepeat.size()).toString()});
  111. }else{
  112. tip = MessageFormat.format(tip, new Object[] { "0",weCustomerSeas.size() });
  113. }
  114. ;
  115. }else{
  116. return AjaxResult.error("导入用户数据不能为空!");
  117. }
  118. return AjaxResult.success(tip);
  119. }
  120. /**
  121. * 获取公海客户列表
  122. * @param weCustomerSea
  123. * @return
  124. */
  125. @GetMapping("list")
  126. public TableDataInfo<WeCustomerSeas> list(WeCustomerSeas weCustomerSea){
  127. startPage();
  128. return getDataTable(
  129. iWeCustomerSeasService.findWeCustomerSeas(weCustomerSea)
  130. );
  131. }
  132. /**
  133. * 删除公海客户
  134. * @param ids
  135. * @return
  136. */
  137. @DeleteMapping("/{ids}")
  138. public AjaxResult remove(@PathVariable Long[] ids)
  139. {
  140. iWeCustomerSeasService.removeByIds(
  141. ListUtil.toList(ids)
  142. );
  143. return AjaxResult.success();
  144. }
  145. /**
  146. * 提醒员工
  147. * @param ids
  148. * @return
  149. */
  150. @PostMapping("/remidUser/{ids}")
  151. public AjaxResult remidUser(@PathVariable Long[] ids){
  152. List<WeCustomerSeas> weCustomerSeas
  153. = iWeCustomerSeasService.listByIds(ListUtil.toList(ids));
  154. if(CollectionUtil.isNotEmpty(weCustomerSeas)){
  155. weCustomerSeas.stream().collect(Collectors.groupingBy(WeCustomerSeas::getAddUserId)).forEach((k,v)->{
  156. iWeCustomerSeasService.remidUser(ListUtil.toList(k),v.size());
  157. });
  158. }
  159. return AjaxResult.success();
  160. }
  161. /**
  162. * 详情头部统计
  163. * @return
  164. */
  165. @GetMapping("/countCustomerSeas")
  166. public AjaxResult countCustomerSeas(){
  167. return AjaxResult.success(
  168. iWeCustomerSeasService.countCustomerSeas()
  169. );
  170. }
  171. /**
  172. * 导入记录或员工添加统计
  173. * @param groupByType 1:获取导入记录数据 2:员工添加统计
  174. * @return
  175. */
  176. @GetMapping("/findSeasRecord")
  177. public TableDataInfo findSeasRecord(@RequestParam(defaultValue = "1") Integer groupByType){
  178. startPage();
  179. List<CustomerSeasRecordVo> seasRecord = iWeCustomerSeasService.findSeasRecord(groupByType);
  180. return getDataTable(seasRecord);
  181. }
  182. /**
  183. * 设置公海客户状态
  184. * @return
  185. */
  186. @PostMapping("/setState")
  187. public AjaxResult setState(@RequestBody WeCustomerSeas weCustomerSea){
  188. if(StringUtils.isEmpty(weCustomerSea.getPhone())){
  189. return AjaxResult.error("手机号不可为空");
  190. }
  191. iWeCustomerSeasService.update(WeCustomerSeas.builder().addState(new Integer(3)).build()
  192. ,new LambdaQueryWrapper<WeCustomerSeas>().eq(WeCustomerSeas::getPhone,weCustomerSea.getPhone()));
  193. return AjaxResult.success();
  194. }
  195. /**
  196. * 获取指定员工所拥有的状态(移动端)
  197. * @param addState
  198. * @return
  199. */
  200. @GetMapping("/findEmployeeCustomer")
  201. public TableDataInfo findEmployeeCustomer(Integer addState){
  202. SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
  203. String userId = sysUser.getWeUserId();
  204. if (StringUtils.isBlank(userId)) {
  205. throw new WeComException("token中获取的we_user_id为空!");
  206. }
  207. startPage();
  208. List<WeCustomerSeas> weCustomerSeas = iWeCustomerSeasService.list(new LambdaQueryWrapper<WeCustomerSeas>()
  209. .eq(StringUtils.isNotEmpty(userId),WeCustomerSeas::getAddUserId, userId)
  210. .eq(addState != null,WeCustomerSeas::getAddState, addState));
  211. return getDataTable(weCustomerSeas);
  212. }
  213. }