UserClueServiceImpl.java 11 KB

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