Search in sources :

Example 1 with BaseBusinessException

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;
}
Also used : FundInfoHistory(com.tony.billing.entity.FundInfoHistory) FundInfo(com.tony.billing.entity.FundInfo) BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) BigDecimal(java.math.BigDecimal) Date(java.util.Date) LocalDate(java.time.LocalDate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with BaseBusinessException

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("用户名不存在, 或者未绑定邮箱");
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) Admin(com.tony.billing.entity.Admin) ModifyAdmin(com.tony.billing.entity.ModifyAdmin)

Example 3 with 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();
}
Also used : BaseResponse(com.tony.billing.response.BaseResponse) BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) ModelAndView(org.springframework.web.servlet.ModelAndView) BindException(org.springframework.validation.BindException) List(java.util.List) FieldError(org.springframework.validation.FieldError) IOException(java.io.IOException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 4 with BaseBusinessException

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;
}
Also used : BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) Admin(com.tony.billing.entity.Admin) ModifyAdmin(com.tony.billing.entity.ModifyAdmin)

Example 5 with BaseBusinessException

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);
}
Also used : Context(org.thymeleaf.context.Context) MimeMessage(javax.mail.internet.MimeMessage) BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Map(java.util.Map)

Aggregations

BaseBusinessException (com.tony.billing.exceptions.BaseBusinessException)7 FundInfo (com.tony.billing.entity.FundInfo)3 BigDecimal (java.math.BigDecimal)3 LocalDate (java.time.LocalDate)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Admin (com.tony.billing.entity.Admin)2 FundInfoHistory (com.tony.billing.entity.FundInfoHistory)2 ModifyAdmin (com.tony.billing.entity.ModifyAdmin)2 Date (java.util.Date)2 FundHistoryValue (com.tony.billing.entity.FundHistoryValue)1 FundPreSaleInfo (com.tony.billing.entity.FundPreSaleInfo)1 FundPreSaleRef (com.tony.billing.entity.FundPreSaleRef)1 BaseResponse (com.tony.billing.response.BaseResponse)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MessagingException (javax.mail.MessagingException)1 MimeMessage (javax.mail.internet.MimeMessage)1