package com.goafanti.customer.controller; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Set; import java.util.TreeSet; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.goafanti.admin.service.AftFileService; import com.goafanti.common.bo.Error; import com.goafanti.common.bo.Result; import com.goafanti.common.constant.AFTConstants; import com.goafanti.common.constant.ErrorConstants; import com.goafanti.common.controller.BaseApiController; import com.goafanti.common.error.BusinessException; import com.goafanti.common.model.AftFile; import com.goafanti.common.model.OrganizationContactBook; import com.goafanti.common.model.User; import com.goafanti.common.model.UserBusiness; import com.goafanti.common.utils.BeanUtilsExt; import com.goafanti.common.utils.DateUtils; import com.goafanti.common.utils.ExcelUtils; import com.goafanti.common.utils.StringUtils; import com.goafanti.core.shiro.token.TokenManager; import com.goafanti.customer.bo.CustomerExcelBo; import com.goafanti.customer.bo.CustomerListIn; import com.goafanti.customer.bo.CustomerOrganizationDetailBo; import com.goafanti.customer.bo.CustomerPersonalDetailBo; import com.goafanti.customer.bo.FollowBusinessBo; import com.goafanti.customer.bo.UserDetailBo; import com.goafanti.customer.service.BusinessService; import com.goafanti.customer.service.CustomerService; @RestController @RequestMapping("api/admin/customer") public class CustomerApiController extends BaseApiController{ @Resource private CustomerService customerService; @Resource private BusinessService businessService; @Resource private AftFileService aftFileService; @Value(value = "${upload.private.path}") private String uploadPrivatePath = null; /** 私有个人客户列表 **/ @RequestMapping(value = "/listPrivatePersonalCustomer" , method = RequestMethod.POST) public Result listPrivatePersonalCustomer(CustomerListIn cli,Integer pageNo, Integer pageSize){ Result res = new Result(); res.setData(customerService.listPrivatePersonalCustomer(cli, pageNo, pageSize)); return res; } /** 公共个人客户列表 **/ @RequestMapping(value = "/listPublicPersonalCustomer" , method = RequestMethod.POST) public Result listPublicPersonalCustomer(CustomerListIn cli,Integer pageNo, Integer pageSize){ Result res = new Result(); res.setData(customerService.listPublicPersonalCustomer(cli, pageNo, pageSize)); return res; } /** 个人客户查询 **/ @RequestMapping(value = "/listAllPersonalCustomer" , method = RequestMethod.POST) public Result listAllPersonalCustomer(CustomerListIn cli,Integer pageNo, Integer pageSize){ Result res = new Result(); res.setData(customerService.listAllPersonalCustomer(cli, pageNo, pageSize)); return res; } /** 私有单位客户列表 **/ @RequestMapping(value = "/listPrivateOrganizationCustomer" , method = RequestMethod.POST) public Result listPrivateOrganizationCustomer(CustomerListIn cli,Integer pageNo, Integer pageSize){ Result res = new Result(); res.setData(customerService.listPrivateOrganizationCustomer(cli, pageNo, pageSize)); return res; } /** 公共单位客户列表 **/ @RequestMapping(value = "/listPublicOrganizationCustomer" , method = RequestMethod.POST) public Result listPublicOrganizationCustomer(CustomerListIn cli,Integer pageNo, Integer pageSize){ Result res = new Result(); res.setData(customerService.listPublicOrganizationCustomer(cli, pageNo, pageSize)); return res; } /** 单位客户查询 **/ @RequestMapping(value = "/listAllOrganizationCustomer" , method = RequestMethod.POST) public Result listAllOrganizationCustomer(CustomerListIn cli,Integer pageNo, Integer pageSize){ Result res = new Result(); res.setData(customerService.listAllOrganizationCustomer(cli, pageNo, pageSize)); return res; } /** 客户即时检索 **/ @RequestMapping(value = "/findCustomerByName",method = RequestMethod.GET) public Result findCustomerByName(String name){ Result res = new Result(); res.setData(customerService.findCustomerByName(name)); return res; } /** 添加客户基本信息 * @throws Exception * @throws NumberFormatException **/ @RequestMapping(value = "/addCustomer", method = RequestMethod.POST) public Result addCustomer(String name,String contacts,String contactMobile,String type,String societyTag) throws Exception{ Result res = new Result(); if(StringUtils.isBlank(name) || StringUtils.isBlank(contacts) || StringUtils.isBlank(contactMobile) || StringUtils.isBlank(societyTag)){ res.getError().add(buildError("客户名称、联系人、联系电话、社会性质不能为空")); return res; } customerService.addCustomer(name, contacts, contactMobile, Integer.parseInt(type),societyTag); return res; } /** 个人客户详情信息 **/ @RequestMapping(value = "/findPersonalCustomerDetail" ,method = RequestMethod.GET) public Result findPersonalCustomerDetail(String uid){ Result res = new Result(); res.setData(customerService.findPersonalCustomerDetail(uid)); return res; } /** 单位客户详情信息 **/ @RequestMapping(value = "/findOrganizationCustomerDetail", method = RequestMethod.GET) public Result findOrganizationCustomerDetail(String uid){ Result res = new Result(); res.setData(customerService.findOrganizationCustomerDetail(uid)); return res; } /** 修改单位客户信息 **/ @RequestMapping(value = "/updateOrganizationCustomer", method = RequestMethod.POST) public Result updateOrganizationCustomer(CustomerOrganizationDetailBo bo){ Result res = new Result(); if(StringUtils.isBlank(bo.getId()) || StringUtils.isBlank(bo.getUid())){ res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"")); return res; } customerService.updateOrganizationCustomer(bo); return res; } /** 修改个人客户信息 **/ @RequestMapping(value = "/updatePersonalCustomer", method = RequestMethod.POST) public Result updatePersonalCustomer(CustomerPersonalDetailBo bo){ Result res = new Result(); if(StringUtils.isBlank(bo.getUid()) || StringUtils.isBlank(bo.getId())){ res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"")); return res; } customerService.updatePersonalCustomer(bo); return res; } /** 查看跟进记录 **/ @RequestMapping(value = "/listFollowHistory", method = RequestMethod.GET) public Result listFollowHistory(Integer pageNo, Integer pageSize,String uid,String businessGlossoryId){ Result res = new Result(); res.setData(customerService.listFollowHistory(pageNo,pageSize,uid,businessGlossoryId)); return res; } /** 查看客户账户信息 **/ @RequestMapping(value = "/findUserAccountDetail", method = RequestMethod.GET) public Result findUserAcountDetail(String uid){ Result res = new Result(); UserDetailBo bo = new UserDetailBo(); User user = customerService.findUserAccountDetail(uid); try { BeanUtilsExt.copyProperties(bo, user); } catch (InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } SimpleDateFormat format = new SimpleDateFormat(AFTConstants.YYYYMMDDHHMMSS); bo.setCreateTimes(format.format(user.getCreateTime())); res.setData(bo); return res; } /** 修改客户账户信息 **/ @RequestMapping(value = "/updateUserAccount", method = RequestMethod.POST) public Result updateUserAccount(User user){ Result res = new Result(); customerService.updateUserAccount(user); return res; } /** 查看客户联系人列表 **/ @RequestMapping(value = "/findCustomerContacts", method = RequestMethod.GET) public Result findCustomerContacts(String uid){ Result res = new Result(); res.setData(customerService.findCustomerContacts(uid)); return res; } /** 进入添加拜访记录 **/ @RequestMapping(value = "/toAddFollow", method = RequestMethod.GET) public Result toAddFollow(String uid){ Result res = new Result(); if(StringUtils.isBlank(uid)){ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"")); return res; } User user = customerService.findUserAccountDetail(uid); FollowBusinessBo fbb = new FollowBusinessBo(); SimpleDateFormat format = new SimpleDateFormat(AFTConstants.YYYYMMDDHHMMSS); fbb.setFollowTime(format.format(new Date())); fbb.setIdentifyName(user.getIdentifyName()); fbb.setUid(uid); fbb.setUserBusinessList(businessService.findBusinessByUAid(uid, TokenManager.getAdminId())); res.setData(fbb); return res; } /** 添加拜访记录 * @throws ParseException * @throws Exception **/ @RequestMapping(value = "/addFollow", method = RequestMethod.POST) public Result addFollow(String userBusinessList,String uid,String ocbId,String contactType,String result,String followTime) throws BusinessException{ Result res = new Result(); if(StringUtils.isBlank(uid) || StringUtils.isBlank(ocbId)){ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "")); return res; } JSONArray ja = (JSONArray) JSON.parse(userBusinessList); List list = new ArrayList(); if (ja != null && !ja.isEmpty()) { UserBusiness userBusiness = null; for (int idx = 0; idx < ja.size(); idx++) { userBusiness = ja.getJSONObject(idx).toJavaObject(UserBusiness.class); for(UserBusiness ub:list){ if(ub.getBusinessGlossoryId() == userBusiness.getBusinessGlossoryId()){ res.getError().add(new com.goafanti.common.bo.Error("业务类型重复,请检查后重新提交!")); return res; } } list.add(userBusiness); } } FollowBusinessBo fbb = new FollowBusinessBo(); fbb.setOcbId(ocbId); fbb.setUid(uid); fbb.setContactType(contactType); fbb.setResult(result); fbb.setFollowTime(followTime); fbb.setUserBusinessList(list); customerService.addFollow(fbb); return res; } /** 进入修改拜访记录 **/ @RequestMapping(value = "/toUpdateFollow", method = RequestMethod.GET) public Result toUpdateFollow(String followId){ Result res = new Result(); FollowBusinessBo fbb = customerService.findFollowById(followId); fbb.setUserBusinessList(customerService.findBusinessByFollowId(followId)); res.setData(fbb); return res; } /** 修改拜访记录 * @throws ParseException * @throws BusinessException */ @RequestMapping(value = "/updateFollow", method = RequestMethod.POST) public Result updateFollow(String userBusinessList,String followId,String followTime,String uid,String contactType,String result) throws BusinessException{ Result res = new Result(); if(StringUtils.isBlank(uid) || StringUtils.isBlank(followId)){ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "")); return res; } JSONArray ja = (JSONArray) JSON.parse(userBusinessList); List list = new ArrayList(); if (ja != null && !ja.isEmpty()) { UserBusiness userBusiness = null; for (int idx = 0; idx < ja.size(); idx++) { userBusiness = ja.getJSONObject(idx).toJavaObject(UserBusiness.class); for(UserBusiness ub:list){ if(ub.getBusinessGlossoryId() == userBusiness.getBusinessGlossoryId()){ res.getError().add(new com.goafanti.common.bo.Error("业务类型重复,请检查后重新提交!")); return res; } } list.add(userBusiness); } } FollowBusinessBo fbb = new FollowBusinessBo(); fbb.setFollowTime(followTime); fbb.setUid(uid); fbb.setContactType(contactType); fbb.setResult(result); fbb.setUserBusinessList(list); fbb.setFollowId(followId); customerService.updateFollow(fbb); return res; } /** 删除跟进记录 **/ @RequestMapping(value = "/deleteFollow", method = RequestMethod.GET) public Result deleteFollow(String followId){ Result res = new Result(); if(StringUtils.isBlank(followId)){ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "")); return res; } customerService.deleteFollow(followId); return res; } /** 查询客户的所有联系人 **/ @RequestMapping(value = "/findAllContacts", method = RequestMethod.GET) public Result findAllContacts(String uid){ Result res = new Result(); res.setData(customerService.findAllContacts(uid)); return res; } /** 修改企业联系人 **/ @RequestMapping(value = "/updateCustomerContacts", method = RequestMethod.POST) public Result updateCustomerContacts(String contactList,String uid){ Result res = new Result(); if(StringUtils.isBlank(uid)){ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "")); return res; } JSONArray ja = (JSONArray) JSON.parse(contactList); List ocbList = new ArrayList(); OrganizationContactBook ocb = null; if(ja != null & !ja.isEmpty()){ for (int idx = 0; idx < ja.size(); idx++) { ocb = ja.getJSONObject(idx).toJavaObject(OrganizationContactBook.class); if(StringUtils.isBlank(ocb.getMobile()) || StringUtils.isBlank(ocb.getName())){ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "用户名和手机号码为必填")); return res; } if(StringUtils.isBlank(ocb.getUid())) ocb.setUid(uid); ocbList.add(ocb); } } customerService.updateCustomerContacts(ocbList,uid); return res; } /** 领取客户 **/ @RequestMapping(value = "/receiveCustomer", method = RequestMethod.GET) public Result receiveCustomer(String uid){ Result res = new Result(); if(StringUtils.isBlank(uid)){ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "")); return res; } customerService.updateByOperatorType(uid, AFTConstants.USER_RECEIVE); return res; } /** 删除客户 **/ @RequestMapping(value = "/deleteCustomer", method = RequestMethod.GET) public Result deleteCustomer(String uid){ Result res = new Result(); if(StringUtils.isBlank(uid)){ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "")); return res; } customerService.updateByOperatorType(uid, AFTConstants.USER_DELETE); return res; } /** * 上传excel文档 * @param req * @return */ @RequestMapping(value = "/uploadExcel",method = RequestMethod.POST) public Result uploadExcel(HttpServletRequest req){ Result res = new Result(); String excelPath = handleFile(res, "/customer_sys_file/", true, req, ""); ExcelUtils utils = new ExcelUtils(); Set boSet; try { boSet = utils.parseExcel(uploadPrivatePath + excelPath); String errorMessage = ""; if(boSet.size()>100) errorMessage += "导入数据不能超过一百条;"; errorMessage += utils.getVacantRows(); if(StringUtils.isNotBlank(errorMessage)) { res.getError().add(new Error(errorMessage)); return res; } Set existRows = new TreeSet(); //数据库已存在 Set filterRows = new TreeSet(); //过滤提示 customerService.checkCustomer(boSet,existRows,filterRows); if(existRows.size()>0){ errorMessage = StringUtils.join(existRows.toArray(),",")+ " 行客户业务已存在;"; res.getError().add(new Error(errorMessage)); return res; } if(filterRows.size()>0){ errorMessage = StringUtils.join(filterRows.toArray(),",")+ " 行已经被系统过滤;"; res.getError().add(new Error(errorMessage)); return res; } customerService.saveUploadData(boSet); } catch (IOException e) { e.printStackTrace(); } return res; } /** * 下载科技成果批量导入Excel模板 */ @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET) public Result downloadTemplateFile(HttpServletResponse response,String type) { Result res = new Result(); if(AFTConstants.USER_TYPE_ORGANIZATION.equals(type)){ AftFile af = aftFileService.selectAftFileBySign("organization_customer_template"); if (null == af) { res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "找不到文件!")); } else { String path = af.getFilePath(); String suffix = path.substring(path.lastIndexOf(".")); String fileName = af.getFileName() + suffix; downloadFile(response, fileName, path); } }else if(AFTConstants.USER_TYPE_PERSONAL.equals(type)){ AftFile af = aftFileService.selectAftFileBySign("personal_customer_template"); if (null == af) { res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "找不到文件!")); } else { String path = af.getFilePath(); String suffix = path.substring(path.lastIndexOf(".")); String fileName = af.getFileName() + suffix; downloadFile(response, fileName, path); } } return res; } /** 转为公共客户 **/ @RequestMapping(value = "/transferToPublic", method = RequestMethod.GET) public Result transferToPublic(String uid){ Result res = new Result(); if(StringUtils.isBlank(uid)){ res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "")); } customerService.updateByOperatorType(uid, AFTConstants.USER_TRANSFER_TO_PUBLIC); return res; } /** 图片上传 **/ @RequestMapping(value = "/uploadCustomerImg", method = RequestMethod.POST) public Result uploadCustomerImg(HttpServletRequest req,String sign){ Result res = new Result(); res.setData(handleFile(res, "/customer_sys_file/", false, req, sign)); return res; } /** * * @param startDate 开始日期 * @param endDate 结束日期 * @param timeSpan 时间差 * @param depId 部门编号 * @param pageNo 页码 * @param pageSize 页数 * @return * @throws ParseException */ @RequestMapping(value = "/customerStatistics",method = RequestMethod.GET) public Result customerStatistics(String startDate,String endDate,String timeSpan,String depId,Integer pageNo, Integer pageSize) throws ParseException{ Result res = new Result(); Date sDate = null; Date eDate = null; DateFormat format = new SimpleDateFormat(AFTConstants.YYYYMMDD); if(StringUtils.isNotBlank(startDate)) sDate = format.parse(startDate); if(StringUtils.isNotBlank(endDate)) eDate = format.parse(endDate); if(StringUtils.isBlank(startDate)&&StringUtils.isBlank(endDate)){ Date date = new Date(); if(timeSpan.equals(DateUtils.DAY_SPAN)){ sDate = DateUtils.getYesterday(); }else if(timeSpan.equals(DateUtils.WEEK_SPAN)){ sDate = DateUtils.getLastDayOfLastWeek(date); }else if(timeSpan.equals(DateUtils.MONTH_SPAN)){ sDate = DateUtils.getLastDayOfLastMonth(date); }else if(timeSpan.equals(DateUtils.QUARTER_SPAN)){ sDate = DateUtils.getLastDayOfLastQuarter(date); }else if(timeSpan.equals(DateUtils.YEAR_SPAN)){ sDate = DateUtils.getLastDayOfLastYear(date); } } res.setData(customerService.customerStatistics(sDate,eDate,depId,pageNo,pageSize)); return res; } /** * * @param startDate 开始日期 * @param endDate 结束日期 * @param timeSpan 时间差 * @param depId 部门编号 * @param businessGlossoryId 业务编号 * @param pageNo 页码 * @param pageSiz 页数 * @return * @throws ParseException */ @RequestMapping(value = "/businessStatistic", method = RequestMethod.GET) public Result businessStatistic(String startDate,String endDate,String timeSpan,String depId,String businessGlossoryId,Integer pageNo, Integer pageSize) throws ParseException{ Result res = new Result(); Date sDate = null; Date eDate = null; DateFormat format = new SimpleDateFormat(AFTConstants.YYYYMMDD); if(StringUtils.isNotBlank(startDate)) sDate = format.parse(startDate); if(StringUtils.isNotBlank(endDate)) eDate = format.parse(endDate); if(StringUtils.isBlank(startDate)&&StringUtils.isBlank(endDate)){ Date date = new Date(); if(timeSpan.equals(DateUtils.DAY_SPAN)){ sDate = DateUtils.getYesterday(); }else if(timeSpan.equals(DateUtils.WEEK_SPAN)){ sDate = DateUtils.getLastDayOfLastWeek(date); }else if(timeSpan.equals(DateUtils.MONTH_SPAN)){ sDate = DateUtils.getLastDayOfLastMonth(date); }else if(timeSpan.equals(DateUtils.QUARTER_SPAN)){ sDate = DateUtils.getLastDayOfLastQuarter(date); }else if(timeSpan.equals(DateUtils.YEAR_SPAN)){ sDate = DateUtils.getLastDayOfLastYear(date); } } res.setData(customerService.businessStatistic(sDate,eDate,depId,businessGlossoryId,pageNo,pageSize)); return res; } /** * * @param startDate 开始日期 * @param endDate 结束日期 * @param timeSpan 时间差 * @param depId 部门编号 * @param businessGlossoryId 业务编号 * @param pageNo 页码 * @param pageSiz 页数 * @return * @throws ParseException */ @RequestMapping(value = "/followStatistic",method = RequestMethod.GET) public Result followStatistic(String startDate,String endDate,String timeSpan,String depId,String businessGlossoryId,Integer pageNo, Integer pageSize) throws ParseException{ Result res = new Result(); Date sDate = null; Date eDate = null; DateFormat format = new SimpleDateFormat(AFTConstants.YYYYMMDD); if(StringUtils.isNotBlank(startDate)) sDate = format.parse(startDate); if(StringUtils.isNotBlank(endDate)) eDate = format.parse(endDate); if(StringUtils.isBlank(startDate)&&StringUtils.isBlank(endDate)){ Date date = new Date(); if(timeSpan.equals(DateUtils.DAY_SPAN)){ sDate = DateUtils.getYesterday(); }else if(timeSpan.equals(DateUtils.WEEK_SPAN)){ sDate = DateUtils.getLastDayOfLastWeek(date); }else if(timeSpan.equals(DateUtils.MONTH_SPAN)){ sDate = DateUtils.getLastDayOfLastMonth(date); }else if(timeSpan.equals(DateUtils.QUARTER_SPAN)){ sDate = DateUtils.getLastDayOfLastQuarter(date); }else if(timeSpan.equals(DateUtils.YEAR_SPAN)){ sDate = DateUtils.getLastDayOfLastYear(date); } } res.setData(customerService.followStatistic(sDate, eDate, businessGlossoryId, depId, pageNo, pageSize)); return res; } }