package com.qtzl.alterSales.manager.service; import cn.com.taiji.common.manager.ManagerException; import cn.com.taiji.common.manager.net.http.ServiceHandleException; import cn.hutool.core.util.DesensitizedUtil; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import com.qtzl.alterSales.dao.entity.second.AflCenterUserInfo; import com.qtzl.alterSales.dao.repo.jpa.second.AflCenterUserInfoRepo; import com.qtzl.alterSales.manager.enums.AccountTypeEnum; import com.qtzl.alterSales.manager.enums.AuditStatusEnum; import com.qtzl.alterSales.manager.model.protocol.UcServiceError; import com.qtzl.alterSales.manager.model.protocol.sales.AflCenterUserInfoAuditRequest; import com.qtzl.alterSales.manager.model.protocol.sales.AflCenterUserInfoFindPageViewRequest; import com.qtzl.alterSales.manager.model.protocol.sales.AflCenterUserInfoUpdateRequest; import com.qtzl.alterSales.manager.service.third.ConstantConfig; import com.qtzl.alterSales.manager.service.third.FmsService; import com.qtzl.alterSales.manager.tools.ExcelUtils; import com.qtzl.alterSales.manager.vo.UpdateCompanyAccountInfoVo; import com.qtzl.alterSales.manager.vo.UpdatePersonalAccountInfoVo; import com.qtzl.alterSales.zt.enums.ZttxInterfaceCode; import com.qtzl.alterSales.zt.utils.ZtHttpResponse; import com.qtzl.alterSales.zt.utils.ZtHttpUtil; import com.qtzl.alterSales.zt.vo.RefreshTokenResponseVo; import com.qtzl.alterSales.zt.vo.UserInfoAuthenticationRequestVo; import com.qtzl.alterSales.zt.vo.UserInfoAuthenticationResponseVo; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.persistence.criteria.Predicate; import java.io.File; import java.time.LocalDateTime; import java.util.List; import java.util.Map; @Service public class AflCenterUserInfoServiceImpl implements AflCenterUserInfoService{ @Resource AflCenterUserInfoRepo aflCenterUserInfoRepo; @Resource FmsService fmsService; @Resource ConstantConfig constantConfig; @Autowired ZtHttpUtil httpUtil; @Resource TokenService tokenService; @Override public void audit(AflCenterUserInfoAuditRequest request) throws ServiceHandleException { if (StringUtils.isEmpty(request.getReviewer())){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("审核人不能为空"); } if (AuditStatusEnum.REJECTED.getCode().equals(request.getStatus())){ if (StringUtils.isEmpty(request.getRemark())){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("审核不通过原因不能为空"); } } AflCenterUserInfo aflCenterUserInfo = selectById(request.getId()); if (request.getStatus().equals(aflCenterUserInfo.getStatus())){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("审核状态未发生改变"); } if (AuditStatusEnum.REVIEWED.getCode().toString().equals(request.getStatus())){ // AflCenterUserInfo aflCenterUserInfo = aflCenterUserInfoRepo.findByAccountId(aflCenterVehicleInfo.getAccountId()); getToken(aflCenterUserInfo); //设置修改账户信息参数 //进行个人用户信息变更和单位用户信息变更 if (AccountTypeEnum.PERSONAL.getCode().equals(aflCenterUserInfo.getAccountType())){ //修改个人用户信息 UpdatePersonalAccountInfoVo updatePersonalAccountInfoVo = new UpdatePersonalAccountInfoVo(); getUpdatePersonalAccountInfoVo(updatePersonalAccountInfoVo,aflCenterUserInfo); Map requestParam = updatePersonalAccountInfoVo.getRequestParam(); ZtHttpResponse response = null; try { response = httpUtil.doPost(ZttxInterfaceCode.UPDATE_PERSONAL_USER_INFO, requestParam); } catch (ManagerException e) { e.getMessage(); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("修改个人账户信息失败,请重新审核,错误原因:"+e.getMessage()); } if (response == null || response.getRcode() != 0) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("修改个人账户信息失败,请重新审核"); } aflCenterUserInfo.setName(aflCenterUserInfo.getNewName()); aflCenterUserInfo.setAddress(aflCenterUserInfo.getNewAddress()); }else if (AccountTypeEnum.COMPANY.getCode().equals(aflCenterUserInfo.getAccountType())){ //修改单位用户信息 UpdateCompanyAccountInfoVo updateCompanyAccountInfoVo = new UpdateCompanyAccountInfoVo(); getUpdateCompanyAccountInfoVo(updateCompanyAccountInfoVo,aflCenterUserInfo); Map requestParam = updateCompanyAccountInfoVo.getRequestParam(); ZtHttpResponse response = null; try { response = httpUtil.doPost(ZttxInterfaceCode.UPDATE_COMPANY_USER_INFO, requestParam); } catch (ManagerException e) { e.getMessage(); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("修改单位账户信息失败,请重新审核,错误原因:"+e.getMessage()); } if (response == null || response.getRcode() != 0) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("修改单位账户信息失败,请重新审核"); } aflCenterUserInfo.setName(aflCenterUserInfo.getNewName()); aflCenterUserInfo.setAddress(aflCenterUserInfo.getNewAddress()); if (StringUtils.isEmpty(aflCenterUserInfo.getNewBank())){ aflCenterUserInfo.setBank(aflCenterUserInfo.getNewBank()); } if (StringUtils.isEmpty(aflCenterUserInfo.getNewBankAddr())){ updateCompanyAccountInfoVo.setBankAddr(aflCenterUserInfo.getNewBankAddr()); } if (StringUtils.isEmpty(aflCenterUserInfo.getNewBankAccount())){ updateCompanyAccountInfoVo.setBankAccount(aflCenterUserInfo.getNewBankAccount()); } if (StringUtils.isEmpty(aflCenterUserInfo.getTaxpayerCode())){ updateCompanyAccountInfoVo.setTaxpayerCode(aflCenterUserInfo.getNewTaxpayerCode()); } }else { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("审核失败,账户类型未知"); } } BeanUtils.copyProperties(request,aflCenterUserInfo); aflCenterUserInfo.setUpdateTime(LocalDateTime.now()); try { aflCenterUserInfoRepo.save(aflCenterUserInfo); } catch (Exception e) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("审核失败,保存变更信息失败"); } } @Override public void update(AflCenterUserInfoUpdateRequest request) throws ServiceHandleException { if (StringUtils.isEmpty(request.getUpdater())){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("修改人不能为空"); } if (StringUtils.isEmpty(request.getNewAddress())){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("地址不能为空"); } if (StringUtils.isEmpty(request.getNewName())){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("姓名不能为空"); } AflCenterUserInfo aflCenterUserInfo = selectById(request.getId()); if (AccountTypeEnum.PERSONAL.getCode().equals(aflCenterUserInfo.getAccountType())&& (aflCenterUserInfo.getName().equals(request.getNewName()) ||request.getNewName().equals(aflCenterUserInfo.getAddress()) ) ){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("个人用户变更前参数与变更后参数未发生改变"); } aflCenterUserInfo.setNewName(request.getNewName()); aflCenterUserInfo.setNewAddress(request.getNewAddress()); aflCenterUserInfo.setNewBank(request.getNewBank()); aflCenterUserInfo.setNewBankAddr(request.getNewBankAddr()); aflCenterUserInfo.setNewBankAccount(request.getNewBankAccount()); aflCenterUserInfo.setNewTaxpayerCode(request.getNewTaxpayerCode()); aflCenterUserInfo.setUpdater(request.getUpdater()); aflCenterUserInfo.setStatus(AuditStatusEnum.PENDINGREVIEW.getCode().toString()); aflCenterUserInfo.setUpdateTime(LocalDateTime.now()); try { aflCenterUserInfoRepo.save(aflCenterUserInfo); } catch (Exception e) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("修改信息失败"); } } /** * 由于导出存在全量导出,该功能需要优化, * @param request * @param logger * @return * @throws ServiceHandleException */ @Override public String export(AflCenterUserInfoFindPageViewRequest request, Logger logger) throws ServiceHandleException { String pathFile =null; try { long count = count(request); if (count>10000l){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("账户管理导出失败,数据量超过10000,不允许导出"); } List aflCenterUserInfos = select(request); if (aflCenterUserInfos==null||aflCenterUserInfos.size()==0) { pathFile = ExcelUtils.export(null, "账户管理",constantConfig.getFilePath() ,null, AflCenterUserInfo.class); } else { for (AflCenterUserInfo aflCenterUserInfo : aflCenterUserInfos) { if (!StringUtils.isEmpty(aflCenterUserInfo.getIdNum())&&aflCenterUserInfo.getIdNum().length()==18){ aflCenterUserInfo.setIdNum(DesensitizedUtil.idCardNum(aflCenterUserInfo.getIdNum(), 3, 4)); } if (!StringUtils.isEmpty(aflCenterUserInfo.getMobile())&&aflCenterUserInfo.getMobile().length()==11){ aflCenterUserInfo.setIdNum(DesensitizedUtil.mobilePhone(aflCenterUserInfo.getMobile())); } } pathFile = ExcelUtils.export(null, "账户管理",constantConfig.getFilePath(), aflCenterUserInfos, AflCenterUserInfo.class); } if (StringUtils.isEmpty(pathFile)){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("账户管理导出失败"); } String s = null; try { s = fmsService.uploadFile(constantConfig.getUploadFile(),pathFile, 90000); } catch (ServiceHandleException e) { logger.error("选装-账户管理导出失败:{}", e.getMessage()); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException(e.getMessage()); } if (StringUtils.isEmpty(s)){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("账户管理导出失败"); } return s; } catch (Exception e) { if (e instanceof ServiceHandleException) { throw e; } logger.error("选装-账户管理导出失败:{}", e.getMessage()); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("账户管理导出失败"); }finally { File file = null; try { file = new File(pathFile); } catch (Exception e) { } if (file!=null){ file.delete(); } } } private List select(AflCenterUserInfoFindPageViewRequest request) throws ServiceHandleException { return aflCenterUserInfoRepo.findAll(((root, query, cb) -> { List list = Lists.newArrayList(); if (!StringUtils.isEmpty(request.getAccountId())) { list.add(cb.equal(root.get("accountId"), request.getAccountId())); } if (!StringUtils.isEmpty(request.getName())) { list.add(cb.like(root.get("name"), "%"+request.getName()+"%")); } if (!StringUtils.isEmpty(request.getAccountType())) { list.add(cb.equal(root.get("accountType"), request.getAccountType())); } if (!StringUtils.isEmpty(request.getIdType())) { list.add(cb.equal(root.get("idType"), request.getIdType())); } if (!StringUtils.isEmpty(request.getIdNum())) { list.add(cb.equal(root.get("idNum"), request.getIdNum())); } if (!StringUtils.isEmpty(request.getMobile())) { list.add(cb.equal(root.get("mobile"), request.getMobile())); } if (!StringUtils.isEmpty(request.getAgentName())) { list.add(cb.like(root.get("agentName"), "%"+request.getAgentName()+"%")); } if (!StringUtils.isEmpty(request.getAgentIdType())) { list.add(cb.equal(root.get("agentIdType"), request.getAgentIdType())); } if (!StringUtils.isEmpty(request.getAccountId())) { list.add(cb.equal(root.get("accountId"), request.getAccountId())); } if (!StringUtils.isEmpty(request.getStatus())) { list.add(cb.equal(root.get("status"), request.getStatus())); } if (request.getInsertTimeStart() != null) { list.add(cb.greaterThanOrEqualTo(root.get("insertTime"), request.getInsertTimeStart())); } if (request.getInsertTimeEnd() != null) { list.add(cb.lessThanOrEqualTo(root.get("insertTime"), request.getInsertTimeEnd())); } Predicate[] p = new Predicate[list.size()]; return cb.and(list.toArray(p)); }), Sort.by(Sort.Direction.DESC, "insertTime")); } public AflCenterUserInfo selectById(String id) throws ServiceHandleException { if (StringUtils.isEmpty(id)){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("账户编号不能为空"); } AflCenterUserInfo aflCenterUserInfo = aflCenterUserInfoRepo.findById(id).orElse(null); if (aflCenterUserInfo == null){ throw UcServiceError.NOT_FOUND_DATA.toHandleException("未查询到数据"); } return aflCenterUserInfo; } private void getUpdatePersonalAccountInfoVo(UpdatePersonalAccountInfoVo updatePersonalAccountInfoVo,AflCenterUserInfo aflCenterUserInfo){ updatePersonalAccountInfoVo.setOpenId(aflCenterUserInfo.getOpenId()); updatePersonalAccountInfoVo.setAccountId(aflCenterUserInfo.getAccountId()); updatePersonalAccountInfoVo.setAccessToken(aflCenterUserInfo.getAccessToken()); updatePersonalAccountInfoVo.setName(aflCenterUserInfo.getNewName()); updatePersonalAccountInfoVo.setAddress(aflCenterUserInfo.getNewAddress()); } private void getUpdateCompanyAccountInfoVo(UpdateCompanyAccountInfoVo updateCompanyAccountInfoVo, AflCenterUserInfo aflCenterUserInfo){ updateCompanyAccountInfoVo.setOpenId(aflCenterUserInfo.getOpenId()); updateCompanyAccountInfoVo.setAccountId(aflCenterUserInfo.getAccountId()); updateCompanyAccountInfoVo.setAccessToken(aflCenterUserInfo.getAccessToken()); updateCompanyAccountInfoVo.setName(aflCenterUserInfo.getNewName()); updateCompanyAccountInfoVo.setAddress(aflCenterUserInfo.getNewAddress()); updateCompanyAccountInfoVo.setBank(aflCenterUserInfo.getNewBank()); updateCompanyAccountInfoVo.setBankAddr(aflCenterUserInfo.getNewBankAddr()); updateCompanyAccountInfoVo.setBankAccount(aflCenterUserInfo.getNewBankAccount()); updateCompanyAccountInfoVo.setTaxpayerCode(aflCenterUserInfo.getNewTaxpayerCode()); } public void getToken(AflCenterUserInfo aflCenterUserInfo) throws ServiceHandleException { if (StringUtils.isEmpty(aflCenterUserInfo.getOpenId())||StringUtils.isEmpty(aflCenterUserInfo.getAccessToken())){ UserInfoAuthenticationRequestVo userInfoAuthenticationRequestVo = new UserInfoAuthenticationRequestVo(); userInfoAuthenticationRequestVo.setId(aflCenterUserInfo.getIdNum()); userInfoAuthenticationRequestVo.setMobile(aflCenterUserInfo.getMobile()); userInfoAuthenticationRequestVo.setName(aflCenterUserInfo.getName()); Integer i = null; try { i = Integer.parseInt(aflCenterUserInfo.getIdType()); } catch (NumberFormatException e) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("证件类型转换失败,请确认,请重新审核"); } userInfoAuthenticationRequestVo.setUserIdType(i); Map requestParam = userInfoAuthenticationRequestVo.getRequestParam(); ZtHttpResponse response = null; try { response = httpUtil.doPost(ZttxInterfaceCode.USER_INFO_AUTHENTICATION, requestParam); } catch (ManagerException e) { e.getMessage(); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("获取账号信息token失败,请重新审核。失败原因:"+e.getMessage()); } if (response != null && response.getRcode() == 0) { try { UserInfoAuthenticationResponseVo res = JSONObject.parseObject(response.getData(), UserInfoAuthenticationResponseVo.class); aflCenterUserInfo.setOpenId(res.getOpenId()); aflCenterUserInfo.setAccessToken(res.getAccessToken()); aflCenterUserInfoRepo.save(aflCenterUserInfo); } catch (Exception e) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("返回数据解析异常,请重新审核"); } } }else { RefreshTokenResponseVo refreshTokenResponseVo = null; try { refreshTokenResponseVo = tokenService.refreshToken(aflCenterUserInfo.getOpenId(), aflCenterUserInfo.getAccessToken()); } catch (ServiceHandleException e) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("获取刷新令牌失败,请重新审核。失败原因:"+e.getMessage()); } if (refreshTokenResponseVo==null){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("获取刷新令牌失败,请重新审核"); } aflCenterUserInfo.setAccessToken(refreshTokenResponseVo.getAccessToken()); } } private long count(AflCenterUserInfoFindPageViewRequest request) throws ServiceHandleException { return aflCenterUserInfoRepo.count(((root, query, cb) -> { List list = Lists.newArrayList(); if (!StringUtils.isEmpty(request.getAccountId())) { list.add(cb.equal(root.get("accountId"), request.getAccountId())); } if (!StringUtils.isEmpty(request.getName())) { list.add(cb.like(root.get("name"), "%"+request.getName()+"%")); } if (!StringUtils.isEmpty(request.getAccountType())) { list.add(cb.equal(root.get("accountType"), request.getAccountType())); } if (!StringUtils.isEmpty(request.getIdType())) { list.add(cb.equal(root.get("idType"), request.getIdType())); } if (!StringUtils.isEmpty(request.getIdNum())) { list.add(cb.equal(root.get("idNum"), request.getIdNum())); } if (!StringUtils.isEmpty(request.getMobile())) { list.add(cb.equal(root.get("mobile"), request.getMobile())); } if (!StringUtils.isEmpty(request.getAgentName())) { list.add(cb.like(root.get("agentName"), "%"+request.getAgentName()+"%")); } if (!StringUtils.isEmpty(request.getAgentIdType())) { list.add(cb.equal(root.get("agentIdType"), request.getAgentIdType())); } if (!StringUtils.isEmpty(request.getAccountId())) { list.add(cb.equal(root.get("accountId"), request.getAccountId())); } if (!StringUtils.isEmpty(request.getStatus())) { list.add(cb.equal(root.get("status"), request.getStatus())); } if (request.getInsertTimeStart() != null) { list.add(cb.greaterThanOrEqualTo(root.get("insertTime"), request.getInsertTimeStart())); } if (request.getInsertTimeEnd() != null) { list.add(cb.lessThanOrEqualTo(root.get("insertTime"), request.getInsertTimeEnd())); } Predicate[] p = new Predicate[list.size()]; return cb.and(list.toArray(p)); })); } }