UserArchivesInterviewServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package com.goafanti.Interview.service.impl;
  2. import com.goafanti.Interview.bo.OutPublicAidUid;
  3. import com.goafanti.Interview.bo.UpdateUserBo;
  4. import com.goafanti.Interview.service.UserArchivesInterviewService;
  5. import com.goafanti.common.dao.*;
  6. import com.goafanti.common.model.*;
  7. import com.goafanti.core.mybatis.BaseMybatisDao;
  8. import com.goafanti.core.mybatis.page.Pagination;
  9. import com.goafanti.core.shiro.token.TokenManager;
  10. import com.goafanti.customer.bo.MyUserDetailsBo;
  11. import org.springframework.stereotype.Service;
  12. import javax.annotation.Resource;
  13. import java.math.BigDecimal;
  14. import java.util.*;
  15. import java.util.stream.Collectors;
  16. /**
  17. * 客户档案面谈表(UserArchivesInterview)表服务实现类
  18. *
  19. * @author makejava
  20. * @since 2025-04-10 17:09:23
  21. */
  22. @Service("userArchivesInterviewService")
  23. public class UserArchivesInterviewServiceImpl extends BaseMybatisDao<UserArchivesInterviewMapper> implements UserArchivesInterviewService {
  24. @Resource
  25. private UserArchivesInterviewMapper userArchivesInterviewMapper;
  26. @Resource
  27. private UserArchivesMapper userArchivesMapper;
  28. @Resource
  29. private PublicReleaseMapper publicReleaseMapper;
  30. @Resource
  31. private UserFirstInterviewMapper userFirstInterviewMapper;
  32. @Resource
  33. private UserDataLogMapper userDataLogMapper;
  34. @Override
  35. public Pagination<UserArchivesInterview> list(UserArchivesInterview userArchivesInterview, Integer pageNo, Integer pageSize) {
  36. Map<String, Object> params = new HashMap<>();
  37. userArchivesInterview.setAid(TokenManager.getAdminId());
  38. params.put("in", userArchivesInterview);
  39. return (Pagination<UserArchivesInterview>) findPage("findUserArchivesInterviewList",
  40. "findUserArchivesInterviewCount", params, pageNo, pageSize);
  41. }
  42. /**
  43. * 通过ID查询单条数据
  44. *
  45. * @param id 主键
  46. * @return 实例对象
  47. */
  48. @Override
  49. public UserArchivesInterview queryById(Integer id) {
  50. return this.userArchivesInterviewMapper.selectById(id);
  51. }
  52. /**
  53. * 新增数据
  54. *
  55. * @param userArchivesInterview 实例对象
  56. * @return 实例对象
  57. */
  58. @Override
  59. public UserArchivesInterview insert(UserArchivesInterview userArchivesInterview) {
  60. this.userArchivesInterviewMapper.insert(userArchivesInterview);
  61. return userArchivesInterview;
  62. }
  63. /**
  64. * 修改数据
  65. *
  66. * @param userArchivesInterview 实例对象
  67. * @return 实例对象
  68. */
  69. @Override
  70. public UserArchivesInterview update(UserArchivesInterview userArchivesInterview) {
  71. this.userArchivesInterviewMapper.update(userArchivesInterview);
  72. return this.queryById(userArchivesInterview.getId());
  73. }
  74. /**
  75. * 通过主键删除数据
  76. *
  77. * @param id 主键
  78. * @return 是否成功
  79. */
  80. @Override
  81. public boolean deleteById(Integer id) {
  82. return this.userArchivesInterviewMapper.deleteById(id) > 0;
  83. }
  84. @Override
  85. public Object updateUser(UpdateUserBo in) {
  86. //第一次面谈时间,暂时设定所有的
  87. List<MyUserDetailsBo> myUserDetailsBos =publicReleaseMapper.selectByUid(in.getUid());
  88. if (!myUserDetailsBos.isEmpty()){
  89. MyUserDetailsBo myUserDetailsBo = myUserDetailsBos.get(0);
  90. in.setFirstInterviewDate(myUserDetailsBo.getReleaseStart());
  91. }
  92. //添加客户档案面谈记录表
  93. if (in.getType()==1){
  94. addUserArchivesInterview(in);
  95. }
  96. //读取是否有档案存在
  97. UserArchives userArchives = userArchivesMapper.queryByUid(in.getUid());
  98. if (userArchives !=null){
  99. in.setId(userArchives.getId());
  100. }
  101. if (in.getId()!=null){
  102. addUserDataLog(userArchives,in);
  103. in.setUpdateTime(new Date());
  104. userArchivesMapper.update(in);
  105. }else {
  106. in.setEarlyCommunication(null);
  107. in.setInterviewIdeas(null);
  108. in.setCreateTime(new Date());
  109. userArchivesMapper.insert(in);
  110. }
  111. return in;
  112. }
  113. private void addUserDataLog(UserArchives use, UpdateUserBo in) {
  114. Admin adminToken = TokenManager.getAdminToken();
  115. String name= adminToken.getName();
  116. StringBuilder str=new StringBuilder(name).append("修改了");
  117. int length = str.length();
  118. addPramUserDataLog(use, in, str);
  119. if ((str.length()-length)>4){
  120. userDataLogMapper.insert(new UserDataLog(use.getUid(),str.toString()));
  121. }
  122. }
  123. private void addPramUserDataLog(UserArchives use, UpdateUserBo in, StringBuilder sb) {
  124. JudgePram(sb,use.getCompanyCount(),in.getCompanyCount(),"母/子公司数");
  125. JudgePram(sb,use.getSocialSecurityCount(),in.getSocialSecurityCount(),"社保人数");
  126. JudgePram(sb,use.getExternalInvestCount(),in.getExternalInvestCount(),"对外投资控股的企业数");
  127. JudgePram(sb,use.getExternalInvestIndustryName(),in.getExternalInvestIndustryName(),"对外投资控股的行业");
  128. JudgePram(sb,use.getExternalInvestName(),in.getExternalInvestName(),"对外投资控股的企业");
  129. JudgePram(sb,use.getFinancialRevenue(),in.getFinancialRevenue(),"财务数据-营收(万元)");
  130. JudgePram(sb,use.getFinancialTax(),in.getFinancialTax(),"财务数据-税收(万元)");
  131. JudgePram(sb,use.getFinancialProperty(),in.getFinancialProperty(),"财务数据-资产(万元)");
  132. JudgePram(sb,use.getFinancialRd(),in.getFinancialRd(),"财务数据-研发费用(万元)");
  133. }
  134. private void JudgePram(StringBuilder sb,Object use, Object in, String name) {
  135. if (use instanceof String){
  136. if (!Objects.equals(use,in)) {
  137. sb.append(name).append("[").append(use).append(" => ").append(in).append("];");
  138. }
  139. }
  140. if (use instanceof BigDecimal){
  141. BigDecimal inDb = BigDecimal.valueOf(Double.parseDouble(in.toString()));
  142. if (inDb.compareTo((BigDecimal) use)!=0) {
  143. sb.append(name).append("[").append(use).append(" => ").append(inDb).append("];");
  144. }
  145. }
  146. }
  147. private void addUserArchivesInterview(UpdateUserBo in) {
  148. List<UserArchivesInterview> list = userArchivesInterviewMapper.selectByUidAid(in.getUid(), in.getAid());
  149. UserArchivesInterview userArchivesInterview = new UserArchivesInterview();
  150. if (list.isEmpty()){
  151. userArchivesInterview.setCounts(1);
  152. }else {
  153. userArchivesInterview = list.get(0);
  154. userArchivesInterview.setCounts(userArchivesInterview.getCounts() + 1);
  155. }
  156. userArchivesInterview.setUid(in.getUid());
  157. userArchivesInterview.setAid(in.getAid());
  158. userArchivesInterview.setEarlyCommunication(in.getEarlyCommunication());
  159. userArchivesInterview.setCustomerDemand(in.getCustomerDemand());
  160. userArchivesInterview.setInterviewIdeas(in.getInterviewIdeas());
  161. userArchivesInterview.setInterviewPurpose(in.getInterviewPurpose());
  162. userArchivesInterview.setInterviewRecommend(in.getInterviewRecommend());
  163. userArchivesInterview.setInterviewFeedback(in.getInterviewFeedback());
  164. userArchivesInterview.setFollowUpPlan(in.getFollowUpPlan());
  165. if(in.getUaiId()!=null){
  166. userArchivesInterviewMapper.update(userArchivesInterview);
  167. }else {
  168. userArchivesInterview.setCreateTime(new Date());
  169. userArchivesInterviewMapper.insert(userArchivesInterview);
  170. in.setUaiId(userArchivesInterview.getId());
  171. }
  172. }
  173. @Override
  174. public Object pushUserFirstInterview(){
  175. try {
  176. // 获取所有公共发布记录
  177. List<OutPublicAidUid> publicReleases = publicReleaseMapper.selectAllAidUid();
  178. if (publicReleases == null || publicReleases.isEmpty()) {
  179. return 1; // 如果没有数据,直接返回
  180. }
  181. // 按 aid 分组
  182. Map<String, List<OutPublicAidUid>> groupedByAid = groupByAid(publicReleases);
  183. // 按 uid 进一步分组,并计算每个用户的首次发布时间
  184. List<UserFirstInterview> res = calculateUserFirstInterviews(groupedByAid);
  185. // 批量插入数据库
  186. insertInBatches(res, 100);
  187. return 1;
  188. } catch (Exception e) {
  189. // 异常处理
  190. e.printStackTrace();
  191. throw new RuntimeException("Error during processing user first interview", e);
  192. }
  193. }
  194. @Override
  195. public Object selectByPrdid(Integer prdid) {
  196. Map<String ,Object> res = new HashMap<>();
  197. UserArchivesInterview uai = userArchivesInterviewMapper.selectByPrdid(prdid);
  198. if (uai!=null){
  199. res.put("data",uai);
  200. List<UserArchivesInterview> list = userArchivesInterviewMapper.selectByUidAid(uai.getUid(), uai.getAid());
  201. res.put("list",list);
  202. }
  203. return res;
  204. }
  205. private Map<String, List<OutPublicAidUid>> groupByAid(List<OutPublicAidUid> publicReleases) {
  206. return publicReleases.stream()
  207. .filter(Objects::nonNull) // 过滤掉空对象
  208. .collect(Collectors.groupingBy(OutPublicAidUid::getAid));
  209. }
  210. private List<UserFirstInterview> calculateUserFirstInterviews(Map<String, List<OutPublicAidUid>> groupedByAid) {
  211. long currentTime = System.currentTimeMillis(); // 提前获取当前时间戳
  212. List<UserFirstInterview> res = new ArrayList<>();
  213. for (Map.Entry<String, List<OutPublicAidUid>> entry : groupedByAid.entrySet()) {
  214. String aid = entry.getKey();
  215. List<OutPublicAidUid> aids = entry.getValue();
  216. Map<String, List<OutPublicAidUid>> groupedByUid = aids.stream()
  217. .filter(Objects::nonNull) // 过滤掉空对象
  218. .collect(Collectors.groupingBy(OutPublicAidUid::getUid));
  219. for (Map.Entry<String, List<OutPublicAidUid>> uidEntry : groupedByUid.entrySet()) {
  220. String uid = uidEntry.getKey();
  221. List<OutPublicAidUid> list = uidEntry.getValue();
  222. UserFirstInterview in = findEarliestRelease(list, currentTime, aid, uid);
  223. if (in != null) {
  224. res.add(in);
  225. }
  226. }
  227. }
  228. return res;
  229. }
  230. private UserFirstInterview findEarliestRelease(List<OutPublicAidUid> list, long currentTime, String aid, String uid) {
  231. UserFirstInterview in = null;
  232. long earliestTime = Long.MAX_VALUE;
  233. for (OutPublicAidUid e : list) {
  234. if (e == null || e.getReleaseStart() == null) {
  235. continue; // 跳过空对象或无效的时间
  236. }
  237. long releaseStartTime = e.getReleaseStart().getTime();
  238. if (releaseStartTime < earliestTime && releaseStartTime < currentTime) {
  239. earliestTime = releaseStartTime;
  240. in = new UserFirstInterview();
  241. in.setFirstTime(e.getReleaseStart());
  242. in.setUid(uid);
  243. in.setAid(aid);
  244. }
  245. }
  246. return in;
  247. }
  248. private void insertInBatches(List<UserFirstInterview> res, int batchSize) {
  249. int total = res.size();
  250. for (int i = 0; i < total; i += batchSize) {
  251. int end = Math.min(i + batchSize, total);
  252. List<UserFirstInterview> batch = res.subList(i, end);
  253. try {
  254. userFirstInterviewMapper.insertBatch(batch);
  255. } catch (Exception e) {
  256. // 批量插入失败时的处理
  257. e.printStackTrace();
  258. throw new RuntimeException("Error inserting batch into database", e);
  259. }
  260. }
  261. }
  262. }