CustomerServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package com.goafanti.customer.service.impl;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.UUID;
  9. import org.apache.commons.beanutils.BeanUtils;
  10. import org.apache.shiro.SecurityUtils;
  11. import org.apache.shiro.subject.Subject;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import com.goafanti.core.mybatis.BaseMybatisDao;
  15. import com.goafanti.core.mybatis.page.Pagination;
  16. import com.goafanti.core.shiro.token.TokenManager;
  17. import com.goafanti.customer.bo.CustomerIn;
  18. import com.goafanti.customer.bo.CustomerOut;
  19. import com.goafanti.customer.service.CustomerService;
  20. import com.goafanti.common.constant.AFTConstants;
  21. import com.goafanti.common.dao.AdminMapper;
  22. import com.goafanti.common.dao.CustomerLogMapper;
  23. import com.goafanti.common.dao.CustomerMapper;
  24. import com.goafanti.common.dao.CustomerOrganizationInfoMapper;
  25. import com.goafanti.common.dao.CustomerUserInfoMapper;
  26. import com.goafanti.common.dao.FollowUpRecordMapper;
  27. import com.goafanti.common.enums.CustomerFollowFiled;
  28. import com.goafanti.common.enums.CustomerIntentionFiled;
  29. import com.goafanti.common.enums.CustomerStatusFiled;
  30. import com.goafanti.common.model.Admin;
  31. import com.goafanti.common.model.Customer;
  32. import com.goafanti.common.model.CustomerLog;
  33. import com.goafanti.common.model.CustomerOrganizationInfo;
  34. import com.goafanti.common.model.CustomerUserInfo;
  35. import com.goafanti.common.model.FollowUpRecord;
  36. import com.goafanti.common.utils.StringUtils;
  37. @Service
  38. public class CustomerServiceImpl extends BaseMybatisDao<CustomerMapper> implements CustomerService {
  39. @Autowired
  40. private CustomerMapper customerMapper ;
  41. @Autowired
  42. private AdminMapper adminMapper;
  43. @Autowired
  44. private CustomerLogMapper customerLogMapper;
  45. @Autowired
  46. private CustomerUserInfoMapper customerUserInfoMapper;
  47. @Autowired
  48. private CustomerOrganizationInfoMapper customerOrganizationInfoMapper;
  49. @Autowired
  50. private FollowUpRecordMapper followUpRecordMapper;
  51. @Override
  52. public int addCustomer(Customer cus,CustomerUserInfo cui,CustomerOrganizationInfo coi,FollowUpRecord fur) {
  53. //插入 customerUserInfo
  54. customerUserInfoMapper.insert(cui);
  55. //插入 customerOrganizationInfo
  56. customerOrganizationInfoMapper.insert(coi);
  57. //插入跟进表
  58. followUpRecordMapper.insert(fur);
  59. String paid = findLadderIds(TokenManager.getAdminId());//查询上级管理员ID集合
  60. if(StringUtils.isNotBlank(paid)) paid = paid.substring(0, paid.length()-1);
  61. cus.setPaid(paid);
  62. int res = customerMapper.insert(cus);
  63. return res;
  64. }
  65. @Override
  66. public void deleteCustomer(String id) {
  67. customerMapper.deleteByPrimaryKey(id);
  68. }
  69. @Override
  70. public String findLadderIds(String adminId) {
  71. String result = "";
  72. Admin admin = adminMapper.selectByPrimaryKey(adminId);
  73. if(admin != null && StringUtils.isNotBlank(admin.getSuperiorId())){
  74. result += admin.getSuperiorId() + ",";
  75. result += findLadderIds(admin.getSuperiorId());
  76. }
  77. return result;
  78. }
  79. @Override
  80. public void updateLadderIds(String ladderId) {
  81. List<Customer> customers = customerMapper.selectByLadderId(ladderId);
  82. if(customers.size()>0){
  83. String newPaid = "";
  84. for(Customer c:customers){
  85. newPaid = findLadderIds(c.getAid());
  86. if(StringUtils.isNotBlank(newPaid)) newPaid = newPaid.substring(0, newPaid.length()-1);
  87. c.setPaid(newPaid);
  88. customerMapper.updateByPrimaryKeySelective(c);
  89. }
  90. }
  91. }
  92. @SuppressWarnings("unchecked")
  93. @Override
  94. public Pagination<CustomerOut> getPrivateCustomer(CustomerIn cin, Integer pageSize, Integer pageNumber) {
  95. cin.setShareType("0");
  96. cin.setAid(TokenManager.getAdminId());
  97. Map<String,Object> params = disposeParams(cin);
  98. if(pageSize == null || pageSize < 0) pageSize = 10;
  99. if(pageNumber == null || pageNumber <0 ) pageNumber = 1;
  100. Pagination<CustomerOut> list = (Pagination<CustomerOut>)findPage("selectCustomer","selectCustomerCount",params, pageNumber, pageSize);
  101. list.setList(setResult((List<CustomerOut>)list.getList()));
  102. return list;
  103. }
  104. @SuppressWarnings("unchecked")
  105. @Override
  106. public Pagination<CustomerOut> getPublicCustomer(CustomerIn cin, Integer pageSize, Integer pageNumber) {
  107. cin.setShareType("1");
  108. Map<String,Object> params = disposeParams(cin);
  109. Pagination<CustomerOut> list = (Pagination<CustomerOut>)findPage("selectCustomer","selectCustomerCount",params, pageNumber, pageSize);
  110. list.setList(setResult((List<CustomerOut>)list.getList()));
  111. list.setList(descResult((List<CustomerOut>)list.getList()));
  112. return list;
  113. }
  114. @SuppressWarnings("unchecked")
  115. @Override
  116. public Pagination<CustomerOut> getTeamCustomer(CustomerIn cin, Integer pageSize, Integer pageNumber) {
  117. cin.setShareType("0");
  118. cin.setPaid(TokenManager.getAdminId());
  119. cin.setAid(TokenManager.getAdminId());
  120. Map<String,Object> params = disposeParams(cin);
  121. if(pageSize == null || pageSize < 0) pageSize = 10;
  122. if(pageNumber == null || pageNumber <0 ) pageNumber = 1;
  123. Pagination<CustomerOut> list = (Pagination<CustomerOut>)findPage("selectCustomer","selectCustomerCount",params, pageNumber, pageSize);
  124. list.setList(setResult((List<CustomerOut>)list.getList()));
  125. list.setList(descResult((List<CustomerOut>)list.getList()));
  126. return list;
  127. }
  128. @SuppressWarnings("unchecked")
  129. @Override
  130. public Pagination<CustomerOut> getCompanyCustomer(CustomerIn cin, Integer pageSize, Integer pageNumber) {
  131. Map<String,Object> params = disposeParams(cin);
  132. if(pageSize == null || pageSize < 0) pageSize = 10;
  133. if(pageNumber == null || pageNumber <0 ) pageNumber = 1;
  134. Pagination<CustomerOut> list = (Pagination<CustomerOut>)findPage("selectCustomer","selectCustomerCount",params, pageNumber, pageSize);
  135. Subject subject = SecurityUtils.getSubject();
  136. list.setList(setResult((List<CustomerOut>)list.getList()));
  137. if(!subject.isPermitted("customer_view_all")&&!subject.hasRole(AFTConstants.SUPERADMIN)) {
  138. list.setList(descResult((List<CustomerOut>)list.getList()));
  139. }
  140. return list;
  141. }
  142. private Map<String,Object> disposeParams(CustomerIn cin){
  143. Map<String,Object> params = new HashMap<String,Object>();
  144. if(StringUtils.isNotBlank(cin.getAdminName())) params.put("adminName", cin.getAdminName());
  145. if(StringUtils.isNotBlank(cin.getAid())) params.put("aid", cin.getAid());
  146. if(StringUtils.isNotBlank(cin.getPaid())) params.put("paid", cin.getPaid());
  147. if(StringUtils.isNotBlank(cin.getCompanyIntention())) params.put("companyIntention", Integer.parseInt(cin.getCompanyIntention()));
  148. if(StringUtils.isNotBlank(cin.getContactName())) params.put("contactName", cin.getContactName());
  149. if(StringUtils.isNotBlank(cin.getCustomerStatus())) params.put("customerStauts", Integer.parseInt(cin.getCustomerStatus()));
  150. if(StringUtils.isNotBlank(cin.getFollowSituation())) params.put("followSituation", Integer.parseInt(cin.getFollowSituation()));
  151. if(StringUtils.isNotBlank(cin.getShareType())) params.put("shareType", Integer.parseInt(cin.getShareType()));
  152. if(StringUtils.isNotBlank(cin.getCustomerType())) params.put("customerType", Integer.parseInt(cin.getCustomerType()));
  153. if(StringUtils.isNotBlank(cin.getContactTel())) params.put("contactTel", cin.getContactTel());
  154. if(StringUtils.isNotBlank(cin.getLocationProvince())) params.put("locationProvince", cin.getLocationProvince());
  155. if(StringUtils.isNotBlank(cin.getCompanyName())) params.put("companyName", cin.getCompanyName());
  156. return params;
  157. }
  158. private List<CustomerOut> setResult(List<CustomerOut> list){
  159. for(CustomerOut co :list){
  160. setFiled(co);
  161. }
  162. return list;
  163. }
  164. private CustomerOut setFiled(CustomerOut co) {
  165. switch(co.getCustomerType()){
  166. case 0 : co.set_customerType("个人客户") ;break;
  167. case 1 : co.set_customerType("机构单位") ;break;
  168. case 2 : co.set_customerType("团体单位") ;break;
  169. }
  170. switch(co.getCustomerStatus()){
  171. case 0 : co.set_customerStatus("新客户") ;break;
  172. case 1 : co.set_customerStatus("意向客户") ;break;
  173. case 2 : co.set_customerStatus("重点客户") ;break;
  174. case 3 : co.set_customerStatus("面谈客户") ;break;
  175. case 4 : co.set_customerStatus("签单客户") ;break;
  176. case 5 : co.set_customerStatus("被拒绝客户") ;break;
  177. }
  178. switch(co.getFollowSituation()){
  179. case 0 : co.set_followSituation("已发项目介绍资料") ;break;
  180. case 1 : co.set_followSituation("已约面谈") ;break;
  181. case 2 : co.set_followSituation("已发合同计划书") ;break;
  182. case 3 : co.set_followSituation("已报价") ;break;
  183. case 4 : co.set_followSituation("已发合同") ;break;
  184. case 5 : co.set_followSituation("已签合同") ;break;
  185. case 6 : co.set_followSituation("面谈中") ;break;
  186. case 7 : co.set_followSituation("已面签") ;break;
  187. case 8 : co.set_followSituation("无进度") ;break;
  188. }
  189. String _companyIntention = "";
  190. if(StringUtils.isNotBlank(co.getCompanyIntention())){
  191. String[] temp ;
  192. if(co.getCompanyIntention().contains(",")){
  193. temp = co.getCompanyIntention().split(",");
  194. }else{
  195. temp = new String[]{co.getCompanyIntention()};
  196. }
  197. for(String s:temp){
  198. if(s.equals("0")) _companyIntention += "发明专利-";
  199. if(s.equals("1")) _companyIntention += "实用型新型专利-";
  200. if(s.equals("2")) _companyIntention += "外观专利-";
  201. if(s.equals("3")) _companyIntention += "软件著作权-";
  202. if(s.equals("4")) _companyIntention += "知识产权贯标-";
  203. if(s.equals("5")) _companyIntention += "高企认定-";
  204. if(s.equals("6")) _companyIntention += "技术成果-";
  205. if(s.equals("7")) _companyIntention += "技术需求-";
  206. if(s.equals("8")) _companyIntention += "专家咨询-";
  207. if(s.equals("9")) _companyIntention += "团单合作-";
  208. if(s.equals("10")) _companyIntention += "商标-";
  209. if(s.equals("11")) _companyIntention += "系统集成-";
  210. }
  211. _companyIntention = _companyIntention.substring(0, _companyIntention.length()-1);
  212. }
  213. co.set_companyIntention(_companyIntention);
  214. if(StringUtils.isNotBlank(co.getFollowDate())) co.setFollowDate(co.getFollowDate().substring(0,19));
  215. return co;
  216. }
  217. /**
  218. * 获取客户意向
  219. * @param intentions
  220. * @return
  221. */
  222. private String getCompanyIntention(String intentions){
  223. String result = "";
  224. if(StringUtils.isNotBlank(intentions)){
  225. String[] list;
  226. if(intentions.contains(",")){
  227. list = intentions.split(",");
  228. }else{
  229. list = new String[]{intentions};
  230. }
  231. for(String s:list){
  232. result += CustomerIntentionFiled.getField(Integer.parseInt(s)).getDesc()+",";
  233. }
  234. result = result.substring(0, result.length()-1);
  235. }
  236. return result;
  237. }
  238. @Override
  239. public void saveCustomerLog(CustomerIn cus,String operatorType){
  240. /* 记录日志 */
  241. String cid = cus.getId();
  242. CustomerLog log = new CustomerLog();
  243. log.setId(UUID.randomUUID().toString());
  244. log.setCid(cid);
  245. log.setOperatorTime(new SimpleDateFormat(AFTConstants.YYYYMMDDHHMMSS).format(new Date()));
  246. log.setOperatorName(TokenManager.getAdminToken().getName());
  247. Customer c = customerMapper.selectByPrimaryKey(cid) ;
  248. if(operatorType.equals(AFTConstants.CUSTOMER_CREATE)){//创建用户
  249. log.setAfterFollowSituation(CustomerFollowFiled.getField(Integer.parseInt(cus.getFollowSituation())).getDesc()); //跟进状态
  250. log.setAfterCustomerIntention(getCompanyIntention(cus.getCompanyIntention())); //跟进意向
  251. log.setAfterCustomerStatus(CustomerStatusFiled.getField(Integer.parseInt(cus.getCustomerStatus())).getDesc()); //客户状态
  252. log.setAfterAdminName(TokenManager.getAdminToken().getName());
  253. log.setOperatorType(AFTConstants.CUSTOMER_CREATE);
  254. }else if(operatorType.equals(AFTConstants.CUSTOMER_MODIFY)){ //修改客户
  255. String beforeAdminName = adminMapper.selectByPrimaryKey(c.getAid()).getName();
  256. log.setBeforeAdminName(beforeAdminName);//修改前操作人
  257. log.setBeforeCustomerIntention(getCompanyIntention(c.getCompanyIntention())); //修改前跟进意向
  258. log.setBeforeCustomerStatus(CustomerStatusFiled.getField(c.getCustomerStatus()).getDesc()); //修改前跟进状态
  259. log.setBeforeFollowSituation(CustomerFollowFiled.getField(c.getFollowSituation()).getDesc());//修改前跟进进度
  260. log.setAfterAdminName(TokenManager.getAdminToken().getName());
  261. log.setAfterFollowSituation(CustomerFollowFiled.getField(Integer.parseInt(cus.getFollowSituation())).getDesc()); //修改后跟进状态
  262. log.setAfterCustomerIntention(getCompanyIntention(cus.getCompanyIntention())); //修改后跟进意向
  263. log.setAfterCustomerStatus(CustomerStatusFiled.getField(Integer.parseInt(cus.getCustomerStatus())).getDesc()); //修改后客户状态
  264. log.setOperatorType(AFTConstants.CUSTOMER_MODIFY);
  265. }else if(operatorType.equals(AFTConstants.CUSTOMER_DELETE)){ //删除客户
  266. String beforeAdminName = adminMapper.selectByPrimaryKey(c.getAid()).getName();
  267. log.setBeforeAdminName(beforeAdminName); //删除前跟进人
  268. log.setBeforeCustomerIntention(getCompanyIntention(c.getCompanyIntention())); //删除前跟进意向
  269. log.setBeforeCustomerStatus(CustomerStatusFiled.getField(c.getCustomerStatus()).getDesc()); //删除前跟进状态
  270. log.setBeforeFollowSituation(CustomerFollowFiled.getField(c.getFollowSituation()).getDesc());//删除前跟进进度
  271. log.setOperatorType(AFTConstants.CUSTOMER_DELETE);
  272. }else if(operatorType.equals(AFTConstants.CUSTOMER_TRANSFER)){ //转交客户
  273. log.setBeforeAdminName(cus.getBeforeAdminName());//转交前的人
  274. log.setAfterAdminName(cus.getAdminName());//转交后的人
  275. log.setBeforeCustomerIntention(getCompanyIntention(c.getCompanyIntention())); //转交前跟进意向
  276. log.setBeforeCustomerStatus(CustomerStatusFiled.getField(c.getCustomerStatus()).getDesc()); //转交前跟进状态
  277. log.setBeforeFollowSituation(CustomerFollowFiled.getField(c.getFollowSituation()).getDesc());//转交前跟进进度
  278. log.setOperatorType(AFTConstants.CUSTOMER_TRANSFER);
  279. }else if(operatorType.equals(AFTConstants.CUSTOMER_RECEIVE)){ //领取客户
  280. String beforeAdminName = adminMapper.selectByPrimaryKey(c.getAid()).getName();
  281. log.setBeforeAdminName(beforeAdminName);//领取前的人
  282. log.setAfterAdminName(TokenManager.getAdminToken().getName());//领取后跟进人
  283. log.setBeforeCustomerIntention(getCompanyIntention(c.getCompanyIntention())); //领取前跟进意向
  284. log.setBeforeCustomerStatus(CustomerStatusFiled.getField(c.getCustomerStatus()).getDesc()); //领取前跟进状态
  285. log.setBeforeFollowSituation(CustomerFollowFiled.getField(c.getFollowSituation()).getDesc());//领取前跟进进度
  286. log.setOperatorType(AFTConstants.CUSTOMER_RECEIVE);
  287. }else if(operatorType.equals(AFTConstants.CUSTOMER_TO_PUBLIC)){ //转为公共客户
  288. log.setBeforeAdminName(cus.getBeforeAdminName());//转为公共客户前跟进人
  289. log.setBeforeCustomerIntention(getCompanyIntention(c.getCompanyIntention())); //转换前跟进意向
  290. log.setBeforeCustomerStatus(CustomerStatusFiled.getField(c.getCustomerStatus()).getDesc()); //转换前跟进状态
  291. log.setBeforeFollowSituation(CustomerFollowFiled.getField(c.getFollowSituation()).getDesc());//转换前跟进进度
  292. log.setOperatorType(AFTConstants.CUSTOMER_TO_PUBLIC);
  293. }else if(operatorType.equals(AFTConstants.CUSTOMER_FOLLOW)){ //跟进客户
  294. String beforeAdminName = adminMapper.selectByPrimaryKey(c.getAid()).getName();
  295. log.setBeforeAdminName(beforeAdminName);//跟进前操作人
  296. log.setBeforeCustomerIntention(getCompanyIntention(c.getCompanyIntention())); //修改前跟进意向
  297. log.setBeforeCustomerStatus(CustomerStatusFiled.getField(c.getCustomerStatus()).getDesc()); //修改前跟进状态
  298. log.setBeforeFollowSituation(CustomerFollowFiled.getField(c.getFollowSituation()).getDesc());//修改前跟进进度
  299. log.setAfterAdminName(TokenManager.getAdminToken().getName());
  300. log.setAfterFollowSituation(CustomerFollowFiled.getField(Integer.parseInt(cus.getFollowSituation())).getDesc()); //修改后跟进状态
  301. log.setAfterCustomerIntention(getCompanyIntention(cus.getCompanyIntention())); //修改后跟进意向
  302. log.setAfterCustomerStatus(CustomerStatusFiled.getField(Integer.parseInt(cus.getCustomerStatus())).getDesc()); //修改后客户状态
  303. log.setOperatorType(AFTConstants.CUSTOMER_FOLLOW);
  304. }
  305. customerLogMapper.insert(log);
  306. }
  307. /**
  308. * 查询客户详情
  309. * @param intentions
  310. * @return
  311. */
  312. @Override
  313. public CustomerOut findCustomerDetails(String id) {
  314. CustomerOut co = customerMapper.selectCustomerById(id);
  315. if(null !=co) setFiled(co);
  316. return co;
  317. }
  318. /**
  319. * 修改客户信息
  320. * @param intentions
  321. * @return
  322. */
  323. @Override
  324. public int updateCustomer(Customer c) {
  325. //修改主表
  326. return customerMapper.updateByPrimaryKeySelective(c);
  327. }
  328. /**
  329. * 查询客户基本信息
  330. */
  331. @Override
  332. public CustomerOut findCustomerBaseInfo(String id) {
  333. Customer cus = customerMapper.selectByPrimaryKey(id);
  334. CustomerOut cusOut = new CustomerOut();
  335. try {
  336. BeanUtils.copyProperties(cusOut, cus);
  337. } catch (IllegalAccessException | InvocationTargetException e) {
  338. e.printStackTrace();
  339. }
  340. if(cusOut != null) setFiled(cusOut);
  341. return cusOut;
  342. }
  343. /**
  344. * 客户资料脱敏
  345. * @param cusOut
  346. */
  347. private List<CustomerOut> descResult(List<CustomerOut> outList){
  348. for(CustomerOut out : outList){
  349. //out.setAdminName("***");
  350. //out.set_followSituation("***");
  351. //out.set_companyIntention("***");
  352. out.setContactName("***");
  353. out.setMobile("***");
  354. out.setTelNum("***");
  355. }
  356. return outList;
  357. }
  358. @Override
  359. public List<CustomerLog> listCustomerLog(String customerId) {
  360. return customerLogMapper.listCustomerLog(customerId);
  361. }
  362. @Override
  363. public CustomerOut findCustomerHistory(String customerId) {
  364. return customerMapper.findCustomerHistory(customerId);
  365. }
  366. }