UserClueServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package com.goafanti.customer.service.impl;
  2. import com.goafanti.common.constant.AFTConstants;
  3. import com.goafanti.common.dao.*;
  4. import com.goafanti.common.enums.IdentityProcess;
  5. import com.goafanti.common.enums.UserLevel;
  6. import com.goafanti.common.error.BusinessException;
  7. import com.goafanti.common.model.*;
  8. import com.goafanti.common.utils.PasswordUtil;
  9. import com.goafanti.common.utils.StringUtils;
  10. import com.goafanti.core.mybatis.BaseMybatisDao;
  11. import com.goafanti.core.mybatis.page.Pagination;
  12. import com.goafanti.core.shiro.token.TokenManager;
  13. import com.goafanti.customer.bo.*;
  14. import com.goafanti.customer.service.UserClueService;
  15. import org.springframework.stereotype.Service;
  16. import javax.annotation.Resource;
  17. import java.time.LocalDateTime;
  18. import java.time.ZoneId;
  19. import java.time.temporal.ChronoUnit;
  20. import java.util.*;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. @Service
  24. public class UserClueServiceImpl extends BaseMybatisDao<UserMapper> implements UserClueService {
  25. @Resource
  26. private UserMapper userMapper;
  27. @Resource
  28. private UserMidMapper userMidMapper;
  29. @Resource
  30. private UserTransferLogMapper userTransferLogMapper;
  31. @Resource
  32. private AdminMapper adminMapper;
  33. @Resource
  34. private UserLockReleaseMapper userLockReleaseMapper;
  35. @Resource
  36. private UserNamesMapper userNamesMapper;
  37. @Resource
  38. private OrganizationContactBookMapper organizationContactBookMapper;
  39. @Resource
  40. private OrganizationIdentityMapper organizationIdentityMapper;
  41. @Resource(name = "passwordUtil")
  42. private PasswordUtil passwordUtil;
  43. @Override
  44. public Pagination<OutUserClueList> list(InputUserClueList in) {
  45. if (TokenManager.hasRole(AFTConstants.SUPERADMIN)){
  46. in.setShiroType(0);
  47. }
  48. if (in.getShiroType()==null){
  49. in.setShiroType(2);
  50. }
  51. in.setAid(TokenManager.getAdminId());
  52. if(in.getEndTime()!=null)in.setEndTime(in.getEndTime()+ " 23:59:59");
  53. return pushReleaseUser((Pagination<OutUserClueList>) findPage("selectUserClueList", "selectUserClueCount", in));
  54. }
  55. private Pagination<OutUserClueList> pushReleaseUser(Pagination<OutUserClueList> page) {
  56. List<OutUserClueList> list = (List<OutUserClueList>) page.getList();
  57. LocalDateTime now = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
  58. LocalDateTime clueTime = null;
  59. if (!list.isEmpty()){
  60. for (OutUserClueList e : list) {
  61. clueTime=e.getClueTransferTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  62. clueTime = clueTime.withHour(0).withMinute(0).withSecond(0).plusDays(AFTConstants.CLUE_USER_MAX);
  63. long between = ChronoUnit.DAYS.between(now,clueTime);
  64. e.setDays((int) between);
  65. }
  66. }
  67. return page;
  68. }
  69. @Override
  70. public Object add(InputUserClueBo in) throws BusinessException{
  71. Admin admin = adminMapper.queryById(TokenManager.getAdminId());
  72. return add(in,admin);
  73. }
  74. public Object add(InputUserClueBo in,Admin admin) throws BusinessException{
  75. User user = new User();
  76. Date now = new Date();
  77. String aid= TokenManager.getAdminId();
  78. String uid = UUID.randomUUID().toString();
  79. String identifyId = UUID.randomUUID().toString();
  80. user.setId(uid);
  81. user.setSource(1);// 客户来源为录入
  82. user.setIdentifyName(in.getUserName());
  83. user.setNickname(in.getUserName());
  84. user.setUsername(in.getUserName());
  85. user.setStatus(AFTConstants.USER_STATUS_NORMAL);
  86. user.setShareType(5);
  87. user.setAid(TokenManager.getAdminId());
  88. user.setClueAid(TokenManager.getAdminId());
  89. // user.setAid("1");
  90. user.setType(1);
  91. user.setLvl(UserLevel.CERTIFIED.getCode());
  92. user.setCreateTime(now);
  93. user.setTransferTime(now);
  94. user.setUpdateTime(now);
  95. user.setSignBills(0);
  96. user.setNewChannel(0);
  97. user.setMobile(in.getContactMobile());
  98. user.setPassword(AFTConstants.INITIALPASSWORD);
  99. user.setInformationMaintainer(TokenManager.getAdminId());
  100. double f = 0;
  101. user.setIntegrity(f);
  102. // user.setInformationMaintainer("1");
  103. user.setChannel(0);
  104. user.setClueProcess(0);
  105. user.setClueStatus(1);
  106. user.setClueTime(now);
  107. user.setClueTransferTime(now);
  108. passwordUtil.encryptPassword(user);
  109. UserMid um=new UserMid();
  110. um.setAid(user.getAid());
  111. um.setUid(uid);
  112. userMidMapper.insertSelective(um);
  113. // 创建企业认证信息
  114. OrganizationIdentity oi = new OrganizationIdentity();
  115. oi.setUnitName(in.getUserName());
  116. oi.setId(identifyId);
  117. oi.setUid(uid);
  118. oi.setContacts(in.getContacts());
  119. oi.setContactMobile(in.getContactMobile());
  120. oi.setHighTechZone(AFTConstants.NO);
  121. oi.setInternational(AFTConstants.NO);
  122. oi.setListed(AFTConstants.NO);
  123. oi.setAuditStatus(IdentityProcess.SUCCESS.getCode()); // 实名企业
  124. organizationIdentityMapper.insert(oi);
  125. userMapper.insertSelective(user);
  126. UserNames un=new UserNames();
  127. un.setUid(uid);
  128. un.setAid(aid);
  129. un.setName(user.getNickname());
  130. userNamesMapper.insertSelective(un);
  131. // 新增企业联系人
  132. iterContactBook(in,admin.getName(),uid);
  133. addUserTransferLog(user,22,null);
  134. return 1;
  135. }
  136. private void iterContactBook(InputUserClueBo in, String aname, String uid) {
  137. OrganizationContactBook cob = new OrganizationContactBook();
  138. cob.setAid(TokenManager.getAdminId());
  139. cob.setAname(aname);
  140. cob.setId(UUID.randomUUID().toString());
  141. cob.setUid(uid);
  142. cob.setName(in.getContacts());
  143. cob.setMobile(in.getContactMobile());
  144. cob.setMajor(AFTConstants.YES);
  145. organizationContactBookMapper.updateSubContact(uid);
  146. organizationContactBookMapper.insert(cob);
  147. if (StringUtils.isNotBlank(in.getContactMobileMore())){
  148. String[] split = null;
  149. if (in.getContactMobileMore().contains(";")){
  150. split = in.getContactMobileMore().split(";");
  151. } else if (in.getContactMobileMore().contains(";")) {
  152. split = in.getContactMobileMore().split(";");
  153. }
  154. for (String s : split) {
  155. if (StringUtils.isNotBlank(s)) {
  156. OrganizationContactBook cob2 = new OrganizationContactBook();
  157. cob2.setAid(TokenManager.getAdminId());
  158. cob2.setAname(aname);
  159. cob2.setId(UUID.randomUUID().toString());
  160. cob2.setUid(uid);
  161. cob2.setName(in.getContacts());
  162. cob2.setMobile(s);
  163. cob2.setMajor(AFTConstants.NO);
  164. organizationContactBookMapper.insert(cob2);
  165. }
  166. }
  167. }
  168. }
  169. @Override
  170. public Map<String, Object> pushImportUserClue(List<OutUserClueExcel> list) {
  171. StringBuilder msg = new StringBuilder();
  172. int size=0;
  173. for (OutUserClueExcel e : list) {
  174. try {
  175. checkMobile(e.getContactMobile());
  176. checkContacts(e.getContacts());
  177. }catch (BusinessException ex){
  178. size++;
  179. msg.append("<br/>客户[").append(e.getNickname()).append("]导入失败,原因:").append(ex.getMessage()).append(" ");
  180. continue;
  181. }
  182. //判定客户名称
  183. User user = checkUserName(e.getNickname());
  184. InputUserClueBo inUserClueBo = new InputUserClueBo();
  185. inUserClueBo.setUserName(e.getNickname());
  186. inUserClueBo.setContactMobile(e.getContactMobile());
  187. inUserClueBo.setContacts(e.getContacts());
  188. inUserClueBo.setContactMobileMore(e.getContactMobileMore());
  189. try {
  190. Admin admin = adminMapper.queryById(TokenManager.getAdminId());
  191. if (user==null){
  192. add(inUserClueBo,admin);
  193. }else {
  194. update(inUserClueBo,admin,user);
  195. }
  196. }catch (Exception ex){
  197. size++;
  198. msg.append("<br/>客户[").append(e.getNickname()).append("]新增异常。 ");
  199. }
  200. }
  201. Map<String,Object> map = new HashMap();
  202. map.put("errorCount",size);
  203. if (size==0){
  204. msg.append("客户导入成功.");
  205. }
  206. map.put("msg",msg);
  207. return map;
  208. }
  209. private void update(InputUserClueBo inUserClueBo,Admin admin,User user) {
  210. Date now = new Date();
  211. User newUser =new User();
  212. newUser.setId(user.getId());
  213. newUser.setClueProcess(0);
  214. newUser.setClueStatus(1);
  215. newUser.setClueTime(now);
  216. newUser.setClueTransferTime(now);
  217. userMapper.update(newUser);
  218. // 新增企业联系人
  219. iterContactBook(inUserClueBo,admin.getName(),user.getId());
  220. }
  221. private void checkContacts(String contacts) throws BusinessException {
  222. String nameReg="[\\u4e00-\\u9fa5]+.*";
  223. Pattern pattern =Pattern.compile(nameReg);
  224. Matcher matcher = pattern.matcher(contacts);
  225. if (!matcher.matches()){
  226. throw new BusinessException("联系人名称名称格式不正确");
  227. }
  228. }
  229. private void checkMobile(String contactMobile) throws BusinessException{
  230. String mobileReg="[1-9]\\d{10}";
  231. String mobileReg2="^0\\d{2,3}-?\\d{7,8}$";
  232. Pattern pattern =Pattern.compile(mobileReg);
  233. Matcher matcher = pattern.matcher(contactMobile);
  234. if (!matcher.matches()){
  235. Pattern pattern2 =Pattern.compile(mobileReg2);
  236. Matcher matcher2 = pattern2.matcher(contactMobile);
  237. if (!matcher2.matches()){
  238. throw new BusinessException("手机号格式不正确");
  239. }
  240. }
  241. }
  242. @Override
  243. public Object updateTransfer(InputUserClueTransferBo in) {
  244. String[] split = in.getUid().split(",");
  245. for (String uid : split) {
  246. userTransfer(uid,in);
  247. }
  248. return 1;
  249. }
  250. private void userTransfer(String uid,InputUserClueTransferBo in) {
  251. User user = userMapper.queryById(uid);
  252. Date date = new Date();
  253. if (!user.getAid().equals(TokenManager.getAdminId())){
  254. throw new BusinessException("只能操作所属客户");
  255. }
  256. String aid=in.getTransferId();
  257. int type = 0;
  258. if (in.getType()==0){
  259. user.setAid(in.getTransferId());
  260. user.setClueProcess(1);
  261. type=23;
  262. }else if (in.getType()==1){
  263. user.setClueProcess(2);
  264. user.setAid(user.getClueAid());
  265. type=24;
  266. aid=user.getClueAid();
  267. }else if (in.getType()==2){
  268. user.setClueProcess(3);
  269. userMapper.update(user);
  270. type=25;
  271. }else if (in.getType()==3){
  272. user.setClueProcess(4);
  273. user.setShareType(0);
  274. type=26;
  275. if (userLockReleaseMapper.selectByUidGetCount(aid,user.getId())==0){
  276. UserLockRelease ulr=new UserLockRelease();
  277. ulr.setId(UUID.randomUUID().toString());
  278. ulr.setAid(TokenManager.getAdminId());
  279. ulr.setUid(user.getId());
  280. ulr.setLockTime(date);
  281. ulr.setStatus(0);
  282. ulr.setType(0);
  283. userLockReleaseMapper.insert(ulr);
  284. }
  285. organizationContactBookMapper.updateAdmin(user.getId(),TokenManager.getAdminId());
  286. }
  287. user.setTransferTime(date);
  288. userMapper.update(user);
  289. addUserTransferLog(user,type,aid);
  290. }
  291. /**
  292. *
  293. * @param user 客户信息
  294. * @param type 22=导入线索客户,23=转交线索客户,24=退回线索客户,25=移除线索客户,26=领取线索客户
  295. */
  296. private void addUserTransferLog(User user,Integer type,String transferId) {
  297. UserTransferLog u = new UserTransferLog();
  298. u.setUid(user.getId());
  299. u.setAid(TokenManager.getAdminId());
  300. if (type==23||type==24){
  301. u.setTakeAid(transferId);
  302. }
  303. u.setType(type);
  304. userTransferLogMapper.insertSelective(u);
  305. }
  306. private User checkUserName(String name) throws BusinessException{
  307. // if (!userMapper.checkUser("", name, "", 1, null, null).isEmpty())
  308. // throw new BusinessException("客户已经存在");
  309. String name2=null;
  310. if (name.contains("(")){
  311. name2=name.replace("(","(").replace(")",")");
  312. }else if (name.contains("(")){
  313. name2=name.replace("(","(").replace(")",")");
  314. }
  315. // if (name2!=null){
  316. // if (!userMapper.checkUser("", name2, "", 1, null, null).isEmpty())
  317. // throw new BusinessException("客户已经存在");
  318. // }
  319. User user = userMapper.selectByName(name);
  320. if (user==null){
  321. user = userMapper.selectByName(name2);
  322. }
  323. if (user!=null){
  324. if (user.getShareType()==0){
  325. throw new BusinessException("客户已经存在私有");
  326. }else if (user.getShareType()==2){
  327. throw new BusinessException("客户已经存在签单");
  328. }
  329. return user;
  330. }
  331. return null;
  332. }
  333. }