UserArchivesInterviewServiceImpl.java 12 KB

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