use of com.tony.billing.exceptions.BaseBusinessException in project BillingDubbo by TonyJiangWJ.
the class FundInfoServiceImpl method preSalePortion.
@Override
@Transactional(rollbackFor = { Exception.class, BaseBusinessException.class })
public boolean preSalePortion(FundPreSalePortionRequest request) {
FundInfo fundInfo = mapper.getById(request.getId(), UserIdContainer.getUserId());
if (fundInfo != null) {
BigDecimal saleAmount = request.getSaleAmount();
if (saleAmount.compareTo(fundInfo.getPurchaseAmount()) <= 0) {
BigDecimal restAmount = fundInfo.getPurchaseAmount().subtract(saleAmount);
Long saleFundId = null;
if (restAmount.compareTo(BigDecimal.ZERO) > 0) {
BigDecimal oneHundred = new BigDecimal("100");
BigDecimal salePercent = saleAmount.multiply(oneHundred).divide(fundInfo.getPurchaseAmount(), BigDecimal.ROUND_HALF_UP);
BigDecimal salePurchaseCost = salePercent.multiply(fundInfo.getPurchaseCost()).divide(oneHundred, 2, BigDecimal.ROUND_HALF_UP);
BigDecimal salePurchaseFee = salePercent.multiply(fundInfo.getPurchaseFee()).divide(oneHundred, 2, BigDecimal.ROUND_HALF_UP);
FundInfo saleFund = new FundInfo();
BeanUtils.copyProperties(fundInfo, saleFund);
saleFund.setPurchaseAmount(saleAmount);
saleFund.setPurchaseCost(salePurchaseCost);
saleFund.setPurchaseFee(salePurchaseFee);
saleFund.setId(null);
// 售出的记录下来
saleFundId = super.insert(saleFund);
// 记录部分售出基金的历史信息
FundInfoHistory fundInfoHistory = new FundInfoHistory();
BeanUtils.copyProperties(fundInfo, fundInfoHistory);
fundInfoHistory.setId(null);
fundInfoHistory.setOriginId(fundInfo.getId());
fundInfoHistory.setChangeType(EnumFundChangeType.SALE_PORTION.getType());
fundInfoHistory.setCreateTime(new Date());
fundInfoHistory.setModifyTime(new Date());
fundInfoHistoryMapper.insert(fundInfoHistory);
// 更新剩余份额
fundInfo.setPurchaseAmount(restAmount);
fundInfo.setPurchaseCost(fundInfo.getPurchaseCost().subtract(salePurchaseCost));
fundInfo.setPurchaseFee(fundInfo.getPurchaseFee().subtract(salePurchaseFee));
super.update(fundInfo);
} else {
// 全部卖出
saleFundId = fundInfo.getId();
}
// 标记为卖出
if (preMarkFundAsSold(saleFundId, UserIdContainer.getUserId(), request.getSaleFeeRate(), request.getAssessmentDate())) {
return true;
} else {
throw new BaseBusinessException("预售出基金信息保存失败");
}
}
}
return false;
}
use of com.tony.billing.exceptions.BaseBusinessException in project BillingDubbo by TonyJiangWJ.
the class AdminServiceImpl method preResetPwd.
@Override
public Admin preResetPwd(String userName) {
Admin user = mapper.queryByUserName(userName);
if (user != null) {
String email = user.getEmail();
if (StringUtils.isNotEmpty(email)) {
String token = sha256(UUID.randomUUID().toString());
user = deleteSecret(user);
redisUtils.set(token, deleteSecret(user), 3600);
user.setTokenId(token);
// TODO send reset email
Map<String, Object> contents = new HashMap<>();
contents.put("title", "重置密码");
contents.put("typeDesc", "重置密码");
contents.put("resetLink", resetPwdUrl + "?token=" + token);
try {
emailService.sendThymeleafMail(email, "用户重置密码", contents, EnumMailTemplateName.RESET_PWD_MAIL.getTemplateName());
} catch (MessagingException e) {
throw new BaseBusinessException("发送重置邮件失败");
}
return user;
}
}
throw new BaseBusinessException("用户名不存在, 或者未绑定邮箱");
}
use of com.tony.billing.exceptions.BaseBusinessException in project BillingDubbo by TonyJiangWJ.
the class ExceptionResolver method doResolveException.
@ExceptionHandler
@Override
protected ModelAndView doResolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
logger.error("biz error:", e);
BaseResponse baseResponse = ResponseUtil.sysError();
if (e instanceof BaseBusinessException) {
baseResponse.setMsg(e.getMessage());
} else if (e instanceof BindException) {
baseResponse = ResponseUtil.paramError();
List<ObjectError> errorList = ((BindException) e).getAllErrors();
if (CollectionUtils.isNotEmpty(errorList)) {
errorList.forEach((error -> {
if (error instanceof FieldError) {
logger.error("[{}]参数错误:{}", ((FieldError) error).getField(), error.getDefaultMessage());
} else {
logger.error("参数错误:{}", error.getDefaultMessage());
}
}));
}
}
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setHeader("Cache-Control", "no-cache, must-revalidate");
try {
httpServletResponse.getWriter().write(JSON.toJSONString(baseResponse));
} catch (IOException ioe) {
logger.error("与客户端通讯异常:" + ioe.getMessage(), ioe);
}
return new ModelAndView();
}
use of com.tony.billing.exceptions.BaseBusinessException in project BillingDubbo by TonyJiangWJ.
the class AdminServiceImpl method resetPwd.
@Override
public boolean resetPwd(ModifyAdmin admin) {
Preconditions.checkNotNull(admin.getNewPassword(), "新密码不能为空");
String token = admin.getTokenId();
Optional<Admin> optional = redisUtils.get(token, Admin.class);
if (optional.isPresent()) {
Admin cachedUser = optional.get();
cachedUser.setPassword(sha256(rsaUtil.decrypt(admin.getNewPassword()), admin.getUserName()));
if (mapper.modifyPwd(cachedUser) > 0) {
// 密码修改完毕之后将缓存删除
redisUtils.del(token);
return true;
}
} else {
throw new BaseBusinessException("token无效,请重新申请重置密码");
}
logger.error("重置密码失败");
return false;
}
use of com.tony.billing.exceptions.BaseBusinessException in project BillingDubbo by TonyJiangWJ.
the class EmailServiceImpl method sendThymeleafMail.
@Override
public void sendThymeleafMail(String sendTo, String title, Map<String, Object> contents, String templateName) throws MessagingException {
if (EnumMailTemplateName.getByName(templateName) == null) {
throw new BaseBusinessException("邮件模板不存在");
}
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(from);
messageHelper.setTo(sendTo);
messageHelper.setSubject(title);
Context context = new Context();
if (contents != null) {
for (Map.Entry<String, Object> content : contents.entrySet()) {
context.setVariable(content.getKey(), content.getValue());
}
}
String emailText = thymeleaf.process(templateName, context);
messageHelper.setText(emailText, true);
mailSender.send(message);
}
Aggregations