UserArchivesInterviewServiceImpl.java 11 KB

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