CustomerServiceImpl.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. package com.goafanti.customer.service.impl;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import java.util.UUID;
  13. import javax.annotation.Resource;
  14. import org.apache.commons.beanutils.BeanUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import com.goafanti.common.bo.Error;
  19. import com.goafanti.common.constant.AFTConstants;
  20. import com.goafanti.common.constant.ErrorConstants;
  21. import com.goafanti.common.dao.BusinessGlossoryMapper;
  22. import com.goafanti.common.dao.OrganizationContactBookMapper;
  23. import com.goafanti.common.dao.OrganizationIdentityMapper;
  24. import com.goafanti.common.dao.UserBusinessMapper;
  25. import com.goafanti.common.dao.UserFollowBusinessMapper;
  26. import com.goafanti.common.dao.UserFollowMapper;
  27. import com.goafanti.common.dao.UserIdentityMapper;
  28. import com.goafanti.common.dao.UserMapper;
  29. import com.goafanti.common.error.BusinessException;
  30. import com.goafanti.common.model.OrganizationContactBook;
  31. import com.goafanti.common.model.OrganizationIdentity;
  32. import com.goafanti.common.model.User;
  33. import com.goafanti.common.model.UserBusiness;
  34. import com.goafanti.common.model.UserFollow;
  35. import com.goafanti.common.model.UserFollowBusiness;
  36. import com.goafanti.common.model.UserIdentity;
  37. import com.goafanti.common.utils.BeanUtilsExt;
  38. import com.goafanti.common.utils.PasswordUtil;
  39. import com.goafanti.common.utils.StringUtils;
  40. import com.goafanti.core.mybatis.BaseMybatisDao;
  41. import com.goafanti.core.mybatis.page.Pagination;
  42. import com.goafanti.core.shiro.token.TokenManager;
  43. import com.goafanti.customer.bo.CustomerExcelBo;
  44. import com.goafanti.customer.bo.CustomerListIn;
  45. import com.goafanti.customer.bo.CustomerListOut;
  46. import com.goafanti.customer.bo.CustomerOrganizationDetailBo;
  47. import com.goafanti.customer.bo.CustomerPersonalDetailBo;
  48. import com.goafanti.customer.bo.CustomerSimpleBo;
  49. import com.goafanti.customer.bo.FollowBusinessBo;
  50. import com.goafanti.customer.bo.FollowListBo;
  51. import com.goafanti.customer.service.CustomerService;
  52. @Service
  53. public class CustomerServiceImpl extends BaseMybatisDao<UserMapper> implements CustomerService {
  54. @Autowired
  55. private UserMapper userMapper;
  56. @Autowired
  57. private UserIdentityMapper userIdentityMapper;
  58. @Autowired
  59. private OrganizationIdentityMapper organizationIdentityMapper;
  60. @Autowired
  61. private OrganizationContactBookMapper organizationContactBookMapper;
  62. @Resource(name = "passwordUtil")
  63. private PasswordUtil passwordUtil;
  64. @Resource
  65. private UserFollowMapper userFollowMapper;
  66. @Resource
  67. private UserBusinessMapper userBusinessMapper;
  68. @Resource
  69. private UserFollowBusinessMapper userFollowBusinessMapper;
  70. @Resource
  71. private BusinessGlossoryMapper businessGlossoryMapper;
  72. @Override
  73. public Pagination<CustomerListOut> listPrivatePersonalCustomer(CustomerListIn cli, Integer pageNo,Integer pageSize) {
  74. cli.setType(AFTConstants.USER_TYPE_PERSONAL);
  75. cli.setAid(TokenManager.getAdminId());
  76. cli.setShareType(String.valueOf(AFTConstants.SHARE_TYPE_PRIVATE));
  77. Map<String,Object> params = disposeParams(cli);
  78. @SuppressWarnings("unchecked")
  79. Pagination<CustomerListOut> list = (Pagination<CustomerListOut>) findPage("selectPersonalCustomerList","selectPersonalCustomerCount",params,pageNo,pageSize);
  80. return list;
  81. }
  82. @Override
  83. public Pagination<CustomerListOut> listPublicPersonalCustomer(CustomerListIn cli, Integer pageNo,Integer pageSize) {
  84. cli.setType(AFTConstants.USER_TYPE_PERSONAL);
  85. cli.setAid(TokenManager.getAdminId());
  86. cli.setShareType(String.valueOf(AFTConstants.SHARE_TYPE_PUBLIC));
  87. Map<String,Object> params = disposeParams(cli);
  88. @SuppressWarnings("unchecked")
  89. Pagination<CustomerListOut> list = (Pagination<CustomerListOut>) findPage("selectPersonalCustomerList","selectPersonalCustomerCount",params,pageNo,pageSize);
  90. return list;
  91. }
  92. @Override
  93. public Pagination<CustomerListOut> listAllPersonalCustomer(CustomerListIn cli, Integer pageNo, Integer pageSize) {
  94. cli.setType(AFTConstants.USER_TYPE_PERSONAL);
  95. Map<String,Object> params = disposeParams(cli);
  96. @SuppressWarnings("unchecked")
  97. Pagination<CustomerListOut> list = (Pagination<CustomerListOut>) findPage("selectPersonalCustomerList","selectPersonalCustomerCount",params,pageNo,pageSize);
  98. return list;
  99. }
  100. @Override
  101. public Pagination<CustomerListOut> listPrivateOrganizationCustomer(CustomerListIn cli, Integer pageNo,Integer pageSize) {
  102. cli.setType(AFTConstants.USER_TYPE_ORGANIZATION);
  103. cli.setAid(TokenManager.getAdminId());
  104. cli.setShareType(String.valueOf(AFTConstants.SHARE_TYPE_PRIVATE));
  105. Map<String,Object> params = disposeParams(cli);
  106. @SuppressWarnings("unchecked")
  107. Pagination<CustomerListOut> list = (Pagination<CustomerListOut>) findPage("selectOrganizationCustomerList","selectOrganizationCustomerCount",params,pageNo,pageSize);
  108. return list;
  109. }
  110. @Override
  111. public Pagination<CustomerListOut> listPublicOrganizationCustomer(CustomerListIn cli, Integer pageNo,Integer pageSize) {
  112. cli.setType(AFTConstants.USER_TYPE_ORGANIZATION);
  113. cli.setAid(TokenManager.getAdminId());
  114. cli.setShareType(String.valueOf(AFTConstants.SHARE_TYPE_PUBLIC));
  115. Map<String,Object> params = disposeParams(cli);
  116. @SuppressWarnings("unchecked")
  117. Pagination<CustomerListOut> list = (Pagination<CustomerListOut>) findPage("selectOrganizationCustomerList","selectOrganizationCustomerCount",params,pageNo,pageSize);
  118. return list;
  119. }
  120. @Override
  121. public Pagination<CustomerListOut> listAllOrganizationCustomer(CustomerListIn cli, Integer pageNo,Integer pageSize) {
  122. cli.setType(AFTConstants.USER_TYPE_ORGANIZATION);
  123. Map<String,Object> params = disposeParams(cli);
  124. @SuppressWarnings("unchecked")
  125. Pagination<CustomerListOut> list = (Pagination<CustomerListOut>) findPage("selectOrganizationCustomerList","selectOrganizationCustomerCount",params,pageNo,pageSize);
  126. return list;
  127. }
  128. private Map<String,Object> disposeParams(CustomerListIn cli){
  129. Map<String,Object> params = new HashMap<String, Object>();
  130. if(StringUtils.isNotBlank(cli.getType())) params.put("type", Integer.parseInt(cli.getType()));
  131. if(StringUtils.isNotBlank(cli.getShareType())) params.put("shareType", Integer.parseInt(cli.getShareType()));
  132. if(StringUtils.isNotBlank(cli.getName())) params.put("name", cli.getName());
  133. if(StringUtils.isNotBlank(cli.getProvince())) params.put("province", Integer.parseInt(cli.getProvince()));
  134. if(StringUtils.isNotBlank(cli.getCity())) params.put("city", Integer.parseInt(cli.getCity()));
  135. if(StringUtils.isNotBlank(cli.getArea())) params.put("area", Integer.parseInt(cli.getArea()));
  136. if(StringUtils.isNotBlank(cli.getContacts())) params.put("contacts", cli.getContacts());
  137. if(StringUtils.isNotBlank(cli.getContactMobile())) params.put("contactMobile", cli.getContactMobile());
  138. if(StringUtils.isNotBlank(cli.getStartDate())) params.put("startDate", cli.getStartDate());
  139. if(StringUtils.isNotBlank(cli.getEndDate())) params.put("endDate", cli.getEndDate());
  140. if(StringUtils.isNotBlank(cli.getExpert())) params.put("expert", Integer.parseInt(cli.getExpert()));
  141. if(StringUtils.isNotBlank(cli.getIndustry())) params.put("industry", Integer.parseInt(cli.getIndustry()));
  142. if(StringUtils.isNotBlank(cli.getInternational())) params.put("international", Integer.parseInt(cli.getInternational()));
  143. if(StringUtils.isNotBlank(cli.getServiceCertification())) params.put("serviceCertification", Integer.parseInt(cli.getServiceCertification()));
  144. if(StringUtils.isNotBlank(cli.getUserCertification())) params.put("userCertification", Integer.parseInt(cli.getUserCertification()));
  145. if(StringUtils.isNotBlank(cli.getCurrentMemberStatus())) params.put("currentMemberStatus", Integer.parseInt(cli.getCurrentMemberStatus()));
  146. if(StringUtils.isNotBlank(cli.getLvl())) params.put("lvl", Integer.parseInt(cli.getLvl()));
  147. if(StringUtils.isNotBlank(cli.getListed())) params.put("listed", Integer.parseInt(cli.getListed()));
  148. if(StringUtils.isNotBlank(cli.getHighTechZone())) params.put("highTechZone", Integer.parseInt(cli.getHighTechZone()));
  149. if(StringUtils.isNotBlank(cli.getExpert())) params.put("expert", Integer.parseInt(cli.getExpert()));
  150. if(StringUtils.isNotBlank(cli.getCelebrity())) params.put("celebrity", Integer.parseInt(cli.getCelebrity()));
  151. if(StringUtils.isNotBlank(cli.getAid())) params.put("aid", cli.getAid());
  152. if(StringUtils.isNotBlank(cli.getIsMember())) params.put("isMember", Integer.parseInt(cli.getIsMember()));
  153. if(StringUtils.isNotBlank(cli.getStatus())) params.put("status", Integer.parseInt(cli.getStatus()));
  154. if(StringUtils.isNotBlank(cli.getSocietyTag())) params.put("societyTag", cli.getSocietyTag());
  155. if(StringUtils.isNotBlank(cli.getStartDate())) params.put("startDate", cli.getStartDate()+" 00:00:00");
  156. if(StringUtils.isNotBlank(cli.getEndDate())) params.put("endDate", cli.getEndDate()+" 23:59:59");
  157. return params;
  158. }
  159. @Override
  160. public List<CustomerSimpleBo> findCustomerByName(String name) {
  161. return userMapper.findCustomerByName(name);
  162. }
  163. @Override
  164. public int judgeCustomerByName(String name) {
  165. return userMapper.judgeCustomerByName(name);
  166. }
  167. @Override
  168. @Transactional
  169. public int addCustomer(String name, String contacts, String contactMobile, Integer type,String societyTag) throws BusinessException{
  170. User user = new User();
  171. Date now = new Date();
  172. String uid = UUID.randomUUID().toString();
  173. String identifyId = UUID.randomUUID().toString();
  174. user.setId(uid);
  175. user.setIdentifyName(name);
  176. user.setSource(1);
  177. user.setNickname(name);
  178. user.setStatus(0);
  179. user.setShareType(0);
  180. user.setServiceCertification(0);
  181. user.setUserCertification(0);
  182. user.setAid(TokenManager.getAdminId());
  183. user.setType(type);
  184. user.setCurrentMemberStatus(0);
  185. user.setLvl(1);
  186. user.setSocietyTag(societyTag);
  187. user.setCreateTime(now);
  188. user.setUpdateTime(now);
  189. user.setMobile(contactMobile);
  190. user.setPassword(AFTConstants.INITIALPASSWORD);
  191. user.setIsMember(0);
  192. passwordUtil.encryptPassword(user);
  193. if(type == 0){
  194. if(userMapper.checkUser("", "", contactMobile, type).size()>0) throw new BusinessException(new Error(ErrorConstants.CUSTOMER_ALREADY_EXIST, name,""));
  195. UserIdentity ui = new UserIdentity();
  196. ui.setId(identifyId);
  197. ui.setUid(uid);
  198. ui.setContacts(contacts);
  199. ui.setContactMobile(contactMobile);
  200. ui.setUsername(name);
  201. ui.setExpert(0);
  202. ui.setCelebrity(0);
  203. ui.setInternational(0);
  204. ui.setAuditStatus(0);
  205. userIdentityMapper.insert(ui);
  206. }else if(type == 1){
  207. if(userMapper.judgeCustomerByName(name)>0) throw new BusinessException(new Error(ErrorConstants.CUSTOMER_ALREADY_EXIST, name,""));
  208. // 创建企业认证信息
  209. OrganizationIdentity oi = new OrganizationIdentity();
  210. oi.setUnitName(name);
  211. oi.setId(identifyId);
  212. oi.setUid(uid);
  213. oi.setContacts(contacts);
  214. oi.setContactMobile(contactMobile);
  215. oi.setHighTechZone(0);
  216. oi.setInternational(0);
  217. oi.setListed(0);
  218. oi.setAuditStatus(5); //实名企业
  219. organizationIdentityMapper.insert(oi);
  220. }
  221. userMapper.insert(user);
  222. //新增企业联系人
  223. OrganizationContactBook cob = new OrganizationContactBook();
  224. cob.setAid(TokenManager.getAdminId());
  225. cob.setId(UUID.randomUUID().toString());
  226. cob.setUid(uid);
  227. cob.setName(contacts);
  228. cob.setMobile(contactMobile);
  229. organizationContactBookMapper.insert(cob);
  230. return 1;
  231. }
  232. @Override
  233. public CustomerPersonalDetailBo findPersonalCustomerDetail(String uid) {
  234. return userMapper.findPersonalCustomerDetail(uid);
  235. }
  236. @Override
  237. public CustomerOrganizationDetailBo findOrganizationCustomerDetail(String uid) {
  238. return userMapper.findOrganizationCustomerDetail(uid);
  239. }
  240. @Override
  241. @Transactional
  242. public int updateOrganizationCustomer(CustomerOrganizationDetailBo bo) {
  243. OrganizationIdentity oi = new OrganizationIdentity();
  244. User user = new User();
  245. try {
  246. BeanUtilsExt.copyProperties(oi, bo);
  247. user.setId(bo.getUid());
  248. user.setSocietyTag(bo.getSocietyTag());
  249. user.setCompanyLogoUrl(bo.getCompanyLogoUrl());
  250. user.setIntroduction(bo.getIntroduction());
  251. user.setUpdateTime(new Date());
  252. } catch (IllegalAccessException |InvocationTargetException e) {
  253. e.printStackTrace();
  254. }
  255. organizationIdentityMapper.updateByPrimaryKeySelective(oi);
  256. userMapper.updateByPrimaryKeySelective(user);
  257. return 1;
  258. }
  259. @Override
  260. public int updatePersonalCustomer(CustomerPersonalDetailBo bo) {
  261. UserIdentity ui = new UserIdentity();
  262. User user = new User();
  263. try {
  264. BeanUtilsExt.copyProperties(ui, bo);
  265. user.setId(bo.getUid());
  266. user.setSocietyTag(bo.getSocietyTag());
  267. user.setHeadPortraitUrl(bo.getHeadPortraitUrl());
  268. user.setIntroduction(bo.getIntroduction());
  269. user.setUpdateTime(new Date());
  270. } catch (InvocationTargetException | IllegalAccessException e) {
  271. e.printStackTrace();
  272. }
  273. userIdentityMapper.updateByPrimaryKeySelective(ui);
  274. userMapper.updateByPrimaryKeySelective(user);
  275. return 1;
  276. }
  277. @SuppressWarnings("unchecked")
  278. @Override
  279. public Pagination<FollowListBo> listFollowHistory(Integer pageNo, Integer pageSize,String uid,String businessGlossoryId) {
  280. Map<String, Object> params = new HashMap<String, Object>();
  281. if(StringUtils.isNotBlank(uid))params.put("uid", uid);
  282. if(StringUtils.isNotBlank(businessGlossoryId)) params.put("businessGlossoryId", businessGlossoryId);
  283. return (Pagination<FollowListBo>)findPage("selectFollowHistoryList","selectFollowHistoryCount",params, pageNo, pageSize);
  284. }
  285. @Override
  286. public User findUserAccountDetail(String uid) {
  287. return userMapper.findUserAccountDetail(uid);
  288. }
  289. @Override
  290. public int updateUserAccount(User user) {
  291. return userMapper.updateByPrimaryKeySelective(user);
  292. }
  293. @Override
  294. public List<OrganizationContactBook> findCustomerContacts(String uid) {
  295. return userMapper.findCustomerContacts(uid,TokenManager.getAdminId());
  296. }
  297. @Override
  298. @Transactional
  299. public int addFollow(FollowBusinessBo fbb) throws BusinessException{
  300. if(fbb.getUserBusinessList().size()<1) throw new BusinessException(new Error(ErrorConstants.AT_LEAST_A_BUSINESS,"",""));
  301. //更新跟进记录表
  302. SimpleDateFormat format = new SimpleDateFormat(AFTConstants.YYYYMMDDHHMMSS);
  303. UserFollow userFollow = new UserFollow();
  304. String followId = UUID.randomUUID().toString();
  305. userFollow.setId(followId);
  306. userFollow.setAid(TokenManager.getAdminId());
  307. userFollow.setContactType(Integer.parseInt(fbb.getContactType()));
  308. userFollow.setEffective(0);
  309. try {
  310. userFollow.setCreateTime(format.parse(fbb.getFollowTime()));
  311. } catch (ParseException e) {
  312. e.printStackTrace();
  313. }
  314. userFollow.setOcbId(fbb.getOcbId());
  315. userFollow.setResult(fbb.getResult());
  316. userFollow.setUid(fbb.getUid());
  317. int followCount = userBusinessMapper.selectFollowCountByUAid(fbb.getUid(), TokenManager.getAdminId()) + 1;
  318. userFollow.setFollowCount(followCount);
  319. userFollowMapper.insert(userFollow);
  320. String ufbId = "";
  321. UserBusiness userBusiness = null;
  322. UserFollowBusiness userFollowBusiness = null;
  323. for(UserBusiness ub: fbb.getUserBusinessList()){
  324. userBusiness = new UserBusiness();
  325. userFollowBusiness = new UserFollowBusiness();
  326. ufbId = UUID.randomUUID().toString();
  327. if(StringUtils.isNotBlank(ub.getId())){
  328. //更新业务表
  329. userBusiness.setId(ub.getId());
  330. userBusiness.setBusinessGlossoryId(ub.getBusinessGlossoryId());
  331. userBusiness.setCustomerStatus(ub.getCustomerStatus());
  332. userBusiness.setFollowSituation(ub.getFollowSituation());
  333. userBusiness.setUid(fbb.getUid());
  334. try {
  335. userBusiness.setUpdateTime(format.parse(fbb.getFollowTime()));
  336. } catch (ParseException e) {
  337. e.printStackTrace();
  338. }
  339. userBusiness.setAid(TokenManager.getAdminId());
  340. userBusinessMapper.updateByPrimaryKeySelective(userBusiness);
  341. //更新业务中间表
  342. userFollowBusiness.setBusinessId(ub.getId());
  343. userFollowBusiness.setCustomerStatus(ub.getCustomerStatus());
  344. userFollowBusiness.setFollowSituation(ub.getFollowSituation());
  345. userFollowBusiness.setId(ufbId);
  346. userFollowBusiness.setFollowId(followId);
  347. userFollowBusiness.setRemarks(ub.getRemarks());
  348. userFollowBusiness.setBusinessGlossoryId(ub.getBusinessGlossoryId());
  349. userFollowBusinessMapper.insert(userFollowBusiness);
  350. }else{
  351. //检测是否可以创建业务
  352. if(userBusinessMapper.judgeBusiness(fbb.getUid(), ub.getBusinessGlossoryId(), "")>0){
  353. String businessName = businessGlossoryMapper.selectByPrimaryKey(ub.getBusinessGlossoryId()).getName();
  354. throw new BusinessException(new Error(ErrorConstants.BUSINESS_ALREADY_EXIST,businessName,""));
  355. }
  356. String ubId = UUID.randomUUID().toString();
  357. //更新业务表
  358. userBusiness.setId(ubId);
  359. userBusiness.setBusinessGlossoryId(ub.getBusinessGlossoryId());
  360. try {
  361. userBusiness.setCreateTime(format.parse(fbb.getFollowTime()));
  362. userBusiness.setUpdateTime(format.parse(fbb.getFollowTime()));
  363. } catch (ParseException e) {
  364. e.printStackTrace();
  365. }
  366. userBusiness.setCustomerStatus(ub.getCustomerStatus());
  367. userBusiness.setFollowSituation(ub.getFollowSituation());
  368. userBusiness.setUid(fbb.getUid());
  369. userBusiness.setAid(TokenManager.getAdminId());
  370. userBusinessMapper.insert(userBusiness);
  371. //更新业务中间表
  372. userFollowBusiness.setBusinessId(ubId);
  373. userFollowBusiness.setCustomerStatus(ub.getCustomerStatus());
  374. userFollowBusiness.setFollowSituation(ub.getFollowSituation());
  375. userFollowBusiness.setId(ufbId);
  376. userFollowBusiness.setFollowId(followId);
  377. userFollowBusiness.setRemarks(ub.getRemarks());
  378. userFollowBusiness.setBusinessGlossoryId(ub.getBusinessGlossoryId());
  379. userFollowBusinessMapper.insert(userFollowBusiness);
  380. }
  381. }
  382. return 1;
  383. }
  384. @Override
  385. public FollowBusinessBo findFollowById(String followId) {
  386. return userMapper.findFollowById(followId);
  387. }
  388. @Override
  389. public List<UserBusiness> findBusinessByFollowId(String followId) {
  390. return userMapper.findBusinessByFollowId(followId);
  391. }
  392. @Override
  393. @Transactional
  394. public int updateFollow(FollowBusinessBo fbb) throws BusinessException{
  395. UserFollow userFollow = new UserFollow();
  396. SimpleDateFormat format = new SimpleDateFormat(AFTConstants.YYYYMMDDHHMMSS);
  397. userFollow.setId(fbb.getFollowId());
  398. if(StringUtils.isNotBlank(fbb.getOcbId())) userFollow.setOcbId(fbb.getOcbId());
  399. userFollow.setResult(fbb.getResult());
  400. userFollow.setContactType(Integer.parseInt(fbb.getContactType()));
  401. userFollowMapper.updateByPrimaryKeySelective(userFollow); //修改拜访记录表
  402. UserFollowBusiness userFollowBusiness = null;
  403. for(UserBusiness ub: fbb.getUserBusinessList()){
  404. userFollowBusiness = new UserFollowBusiness();
  405. if(StringUtils.isNotBlank(ub.getId())){
  406. userFollowBusiness.setId(ub.getId());
  407. userFollowBusiness.setFollowSituation(ub.getFollowSituation());
  408. userFollowBusiness.setCustomerStatus(ub.getCustomerStatus());
  409. userFollowBusiness.setRemarks(ub.getRemarks());
  410. userFollowBusinessMapper.updateByPrimaryKeySelective(userFollowBusiness); //修改拜访记录中间表
  411. }else{
  412. //检测是否可以创建业务
  413. if(userBusinessMapper.judgeBusiness(fbb.getUid(), ub.getBusinessGlossoryId(), "")>0){
  414. String businessName = businessGlossoryMapper.selectByPrimaryKey(ub.getBusinessGlossoryId()).getName();
  415. throw new BusinessException(new Error(ErrorConstants.BUSINESS_ALREADY_EXIST,businessName,""));
  416. }
  417. String ubId = UUID.randomUUID().toString();
  418. UserBusiness userBusiness = new UserBusiness();
  419. //更新业务表
  420. userBusiness.setId(ubId);
  421. userBusiness.setBusinessGlossoryId(ub.getBusinessGlossoryId());
  422. try {
  423. userBusiness.setCreateTime(format.parse(fbb.getFollowTime()));
  424. userBusiness.setUpdateTime(format.parse(fbb.getFollowTime()));
  425. } catch (ParseException e) {
  426. e.printStackTrace();
  427. }
  428. userBusiness.setCustomerStatus(ub.getCustomerStatus());
  429. userBusiness.setFollowSituation(ub.getFollowSituation());
  430. userBusiness.setUid(fbb.getUid());
  431. userBusiness.setAid(TokenManager.getAdminId());
  432. userBusinessMapper.insert(userBusiness);
  433. //更新业务中间表
  434. String ufbId = UUID.randomUUID().toString();
  435. userFollowBusiness.setId(ufbId);
  436. userFollowBusiness.setFollowId(fbb.getFollowId());
  437. userFollowBusiness.setBusinessId(ubId);
  438. userFollowBusiness.setCustomerStatus(ub.getCustomerStatus());
  439. userFollowBusiness.setFollowSituation(ub.getFollowSituation());
  440. userFollowBusiness.setRemarks(ub.getRemarks());
  441. userFollowBusiness.setBusinessGlossoryId(ub.getBusinessGlossoryId());
  442. userFollowBusinessMapper.insert(userFollowBusiness);
  443. }
  444. }
  445. return 1;
  446. }
  447. @Override
  448. public Set<OrganizationContactBook> findAllContacts(String uid) {
  449. Set<OrganizationContactBook> result = new HashSet<OrganizationContactBook>();
  450. result.addAll(userMapper.findAllContacts(uid));
  451. return result;
  452. }
  453. @Override
  454. public int updateCustomerContacts(List<OrganizationContactBook> ocbList,String uid) {
  455. if(ocbList.size()>0){
  456. OrganizationContactBook ocb = null;
  457. for(OrganizationContactBook item: ocbList){
  458. ocb = new OrganizationContactBook();
  459. try {
  460. BeanUtils.copyProperties(ocb, item);
  461. if(StringUtils.isBlank(ocb.getId())){
  462. ocb.setId(UUID.randomUUID().toString());
  463. ocb.setUid(uid);
  464. ocb.setAid(TokenManager.getAdminId());
  465. organizationContactBookMapper.insert(ocb);
  466. }else{
  467. organizationContactBookMapper.updateByPrimaryKeySelective(ocb);
  468. }
  469. } catch (IllegalAccessException | InvocationTargetException e) {
  470. e.printStackTrace();
  471. }
  472. }
  473. }
  474. return 1;
  475. }
  476. @Override
  477. public int deleteFollow(String followId) {
  478. UserFollow uf = new UserFollow();
  479. uf.setId(followId);
  480. uf.setEffective(1);
  481. return userFollowMapper.updateByPrimaryKeySelective(uf);
  482. }
  483. @Override
  484. public void checkCustomer(Set<CustomerExcelBo> boSet, Set<Integer> existRows, Set<Integer> filterRows) {
  485. List<User> list = null;
  486. for (CustomerExcelBo bo : boSet) {
  487. CustomerExcelBo ceb = null;
  488. for(Iterator<CustomerExcelBo> it = boSet.iterator();it.hasNext();){ //先自检
  489. ceb = it.next();
  490. if(bo.getCustomerType().equals(AFTConstants.USER_TYPE_PERSONAL)){ //个人
  491. if(ceb.getMobile().equals(bo.getMobile()) && ceb.getCustomerType().equals(bo.getCustomerType())){
  492. bo.setUid(ceb.getUid());
  493. bo.setNewData(false);
  494. filterRows.add(bo.getRowNumber());
  495. break;
  496. }
  497. }else if(bo.getCustomerType().equals(AFTConstants.USER_TYPE_ORGANIZATION)){ //单位
  498. if(ceb.getIdentifyName().equals(bo.getIdentifyName()) && ceb.getCustomerType().equals(bo.getCustomerType())){
  499. bo.setUid(ceb.getUid());
  500. bo.setNewData(false);
  501. filterRows.add(bo.getRowNumber());
  502. break;
  503. }
  504. }
  505. }
  506. if(StringUtils.isBlank(bo.getUid())){ //是否已经存在
  507. if(bo.getCustomerType().equals(AFTConstants.USER_TYPE_PERSONAL)){
  508. list = userMapper.checkUser("", "", bo.getMobile(), Integer.parseInt(bo.getCustomerType()));
  509. }else if(bo.getCustomerType().equals(AFTConstants.USER_TYPE_ORGANIZATION)){
  510. list = userMapper.checkUser("", bo.getIdentifyName(), "", Integer.parseInt(bo.getCustomerType()));
  511. }
  512. if (list.size() > 0) {
  513. bo.setUid(list.get(0).getId());
  514. bo.setNewData(false);
  515. if (userBusinessMapper.judgeBusiness(bo.getUid(), Integer.parseInt(bo.getBusinessGlossoryId()),"") > 0) {
  516. existRows.add(bo.getRowNumber());
  517. }
  518. }else{
  519. bo.setUid(UUID.randomUUID().toString());
  520. bo.setNewData(true);
  521. }
  522. }else{
  523. if (userBusinessMapper.judgeBusiness(bo.getUid(), Integer.parseInt(bo.getBusinessGlossoryId()),"") > 0) {
  524. existRows.add(bo.getRowNumber());
  525. }
  526. }
  527. }
  528. }
  529. @Override
  530. @Transactional
  531. public int saveUploadData(Set<CustomerExcelBo> boSet) {
  532. Date now = new Date();
  533. String uid = "";
  534. for(CustomerExcelBo bo : boSet){
  535. uid = bo.getUid();
  536. if(bo.isNewData()){ //新建客户
  537. createUser(bo, uid, now);
  538. createIdentity(bo, bo.getCustomerType(), uid);
  539. createBusiness(bo, uid, now);
  540. createContacts(bo, uid);
  541. }else{ //新建业务
  542. createBusiness(bo, uid, now);
  543. if(organizationContactBookMapper.checkContacts(uid, bo.getMobile(), TokenManager.getAdminId())<=0){
  544. createContacts(bo, uid);
  545. }
  546. }
  547. }
  548. return 1;
  549. }
  550. private void createUser(CustomerExcelBo bo,String uid,Date now){
  551. User user = new User();
  552. user.setId(uid);
  553. user.setIdentifyName(bo.getIdentifyName());
  554. user.setSource(1);
  555. user.setNickname(bo.getIdentifyName());
  556. user.setStatus(0);
  557. user.setShareType(0);
  558. user.setServiceCertification(0);
  559. user.setUserCertification(0);
  560. user.setAid(TokenManager.getAdminId());
  561. user.setType(Integer.parseInt(bo.getCustomerType()));
  562. user.setCurrentMemberStatus(0);
  563. user.setLvl(1);
  564. user.setCreateTime(now);
  565. user.setUpdateTime(now);
  566. user.setMobile(bo.getMobile());
  567. user.setPassword(AFTConstants.INITIALPASSWORD);
  568. user.setIsMember(0);
  569. passwordUtil.encryptPassword(user);
  570. userMapper.insert(user);
  571. }
  572. private void createIdentity(CustomerExcelBo bo,String type,String uid){
  573. String identifyId = UUID.randomUUID().toString();
  574. if(type.equals(AFTConstants.USER_TYPE_PERSONAL)){
  575. //新增个人认证信息
  576. UserIdentity ui = new UserIdentity();
  577. ui.setId(identifyId);
  578. ui.setUid(uid);
  579. ui.setContacts(bo.getContacts());
  580. ui.setContactMobile(bo.getMobile());
  581. ui.setUsername(bo.getIdentifyName());
  582. ui.setExpert(0);
  583. ui.setCelebrity(0);
  584. ui.setInternational(0);
  585. ui.setAuditStatus(0);
  586. userIdentityMapper.insert(ui);
  587. }else if(type.equals(AFTConstants.USER_TYPE_ORGANIZATION)){
  588. // 创建企业认证信息
  589. OrganizationIdentity oi = new OrganizationIdentity();
  590. oi.setId(identifyId);
  591. oi.setUnitName(bo.getIdentifyName());
  592. oi.setUid(uid);
  593. oi.setContacts(bo.getContacts());
  594. oi.setContactMobile(bo.getMobile());
  595. oi.setHighTechZone(0);
  596. oi.setInternational(0);
  597. oi.setListed(0);
  598. oi.setAuditStatus(5); //实名企业
  599. organizationIdentityMapper.insert(oi);
  600. }
  601. }
  602. private void createContacts(CustomerExcelBo bo,String uid){
  603. //新增企业联系人
  604. OrganizationContactBook cob = new OrganizationContactBook();
  605. cob.setAid(TokenManager.getAdminId());
  606. cob.setId(UUID.randomUUID().toString());
  607. cob.setUid(uid);
  608. cob.setName(bo.getContacts());
  609. cob.setMobile(bo.getMobile());
  610. organizationContactBookMapper.insert(cob);
  611. }
  612. private void createBusiness(CustomerExcelBo bo,String uid,Date now){
  613. //新增业务
  614. UserBusiness ub = new UserBusiness();
  615. ub.setId(UUID.randomUUID().toString());
  616. ub.setAid(TokenManager.getAdminId());
  617. ub.setBusinessGlossoryId(Integer.parseInt(bo.getBusinessGlossoryId()));
  618. ub.setUid(uid);
  619. ub.setCreateTime(now);
  620. ub.setUpdateTime(now);
  621. ub.setCustomerStatus(Integer.parseInt(bo.getCustomerStatus()));
  622. ub.setFollowSituation(Integer.parseInt(bo.getFollowSituation()));
  623. userBusinessMapper.insert(ub);
  624. }
  625. @Override
  626. public int updateByOperatorType(String uid, String operatorType) {
  627. User user = new User();
  628. user.setId(uid);
  629. if(operatorType.equals(AFTConstants.USER_TRANSFER_TO_PUBLIC)){
  630. user.setShareType(AFTConstants.SHARE_TYPE_PUBLIC);
  631. }else if(operatorType.equals(AFTConstants.USER_RECEIVE)){
  632. user.setAid(TokenManager.getAdminId());
  633. user.setShareType(AFTConstants.SHARE_TYPE_PRIVATE);
  634. }else if(operatorType.equals(AFTConstants.USER_DELETE)){
  635. user.setStatus(AFTConstants.USER_STATUS_CANCEL);
  636. }
  637. return userMapper.updateByPrimaryKeySelective(user);
  638. }
  639. }