TbAccountService.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.pj.project.tb_account;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.codec.Base64;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.NumberUtil;
  6. import cn.hutool.core.util.RandomUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.baomidou.mybatisplus.extension.service.IService;
  9. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. import com.pj.current.satoken.StpUserUtil;
  11. import com.pj.project.tb_charge_record.TbChargeRecord;
  12. import com.pj.project.tb_charge_record.TbChargeRecordService;
  13. import com.pj.project.tb_costomer.TbCostomer;
  14. import com.pj.project.tb_costomer.TbCostomerService;
  15. import com.pj.utils.AesUtil;
  16. import com.pj.utils.sg.AjaxError;
  17. import com.pj.utils.sg.NbUtil;
  18. import com.pj.utils.so.SoMap;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import javax.annotation.Resource;
  22. import java.math.BigDecimal;
  23. import java.util.Date;
  24. import java.util.List;
  25. /**
  26. * Service: tb_account -- 客户账户
  27. *
  28. * @author qzy
  29. */
  30. @Service
  31. public class TbAccountService extends ServiceImpl<TbAccountMapper, TbAccount> implements IService<TbAccount> {
  32. /**
  33. * 底层 Mapper 对象
  34. */
  35. @Autowired
  36. TbAccountMapper tbAccountMapper;
  37. @Resource
  38. TbCostomerService tbCostomerService;
  39. @Resource
  40. TbChargeRecordService tbChargeRecordService;
  41. /**
  42. * 根据客户Id,获取客户存款账户,并解密金额
  43. * @param customerId
  44. * @return
  45. */
  46. public TbAccount getByCustomerId(String customerId) {
  47. QueryWrapper<TbAccount> ew = new QueryWrapper<>();
  48. ew.eq("customer_id", customerId);
  49. TbAccount account = tbAccountMapper.selectOne(ew);
  50. if (account == null) {
  51. account = new TbAccount();
  52. }
  53. String key = AesUtil.reverse(account.getAccSalt());
  54. account.setTotalMoney(AesUtil.decryptECB(account.getTotalMoney(),key));
  55. return account;
  56. }
  57. public List<TbAccountBO> getList(SoMap so){
  58. return tbAccountMapper.getTbAccountBOList(so);
  59. }
  60. public void recharge(String customerId, String money) {
  61. TbAccount account = this.getByCustomerId(customerId);
  62. recharge(account, TbChargeRecord.Ch.PC.getType(), new BigDecimal(money), new BigDecimal(money));
  63. }
  64. public void recharge(String id, BigDecimal money, String chargeType, BigDecimal payMoney) {
  65. TbAccount account = this.getById(id);
  66. recharge(account, chargeType, money, payMoney);
  67. }
  68. public void recharge(TbAccount account, String chargeType, BigDecimal money, BigDecimal payMoney) {
  69. String totalMoney = account.getTotalMoney();
  70. if (NumberUtil.isNumber(totalMoney)) {
  71. totalMoney = Base64.encode(totalMoney.getBytes());
  72. }
  73. BigDecimal total = new BigDecimal(Base64.decodeStr(totalMoney)).add(money);
  74. account.setTotalMoney(Base64.encode(total.toString()));
  75. this.updateById(account);
  76. String customerId = account.getCustomerId();
  77. TbChargeRecord record = new TbChargeRecord();
  78. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  79. record.setChargeChannel(chargeType).setMoney(money).setPayMoney(payMoney)
  80. .setCreateTime(new Date()).setNo(DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4))
  81. .setCustomerId(customerId).setCustomerName(tbCostomer.getName());
  82. tbChargeRecordService.save(record);
  83. }
  84. /**
  85. * 充值
  86. */
  87. public void recharge(SoMap so){
  88. String salt = so.getString("salt");
  89. String preTopupMoney = so.getString("preTopupMoney");//充值金额
  90. String discountMoney = so.getString("discountMoney");//优惠金额
  91. String key = AesUtil.reverse(salt);
  92. String decTotalTopup = AesUtil.decryptECB(preTopupMoney,key);
  93. if(!NumberUtil.isNumber(decTotalTopup)){
  94. throw new AjaxError("充值金额必须是数字!");
  95. }
  96. if(Double.valueOf(decTotalTopup)<=0){
  97. throw new AjaxError("总计充值金额不得小于零元!");
  98. }
  99. String beforeBalance = preTopupMoney;
  100. TbAccount account;
  101. String accIdStr = so.getString("id");
  102. if(!NumberUtil.isNumber(accIdStr)){
  103. account = getTbAccountByMap(so);
  104. account.setTotalMoney(preTopupMoney);
  105. account.setAccSalt(salt);
  106. }else {
  107. long accId = Long.parseLong(accIdStr);
  108. account = tbAccountMapper.getById(accId);
  109. String totalMoneyStr = AesUtil.decryptECB(account.getTotalMoney(),AesUtil.reverse(account.getAccSalt()));
  110. if(NumberUtil.isNumber(totalMoneyStr)){
  111. beforeBalance = account.getTotalMoney();
  112. BigDecimal totalMoneyBig =new BigDecimal(totalMoneyStr).add(new BigDecimal(decTotalTopup));
  113. account.setTotalMoney(AesUtil.encryptECB(totalMoneyBig.toString(),key));
  114. account.setAccSalt(salt);
  115. account.setUpdateTime(new Date());
  116. account.setUpdateBy(StpUserUtil.getLoginName());
  117. }
  118. }
  119. account.insertOrUpdate();
  120. //保存充值记录
  121. TbChargeRecord record = BeanUtil.fillBeanWithMap(so, new TbChargeRecord(),false);
  122. record.setStatus(1)
  123. .setId(null)
  124. .setAccountId(account.getId())
  125. .setTotalTopupMoney(preTopupMoney)
  126. .setPreTopupMoney(preTopupMoney)
  127. .setDiscountMoney(discountMoney)
  128. .setTotalMoney(account.getTotalMoney())
  129. .setBeforeBalance(beforeBalance)
  130. .setChargeChannel(TbChargeRecord.Ch.PC.getType())
  131. .setCreateTime(new Date())
  132. .setSalt(salt)
  133. .setChargePeople(StpUserUtil.getLoginName())
  134. .setNo(getIdByNow());
  135. tbChargeRecordService.save(record);
  136. }
  137. /**
  138. * 修改Account
  139. * @param so
  140. */
  141. public void updateAccount(SoMap so){
  142. String accIdStr = so.getString("id");
  143. TbAccount account = null;
  144. if(!NumberUtil.isNumber(accIdStr)){
  145. account = getTbAccountByMap(so);
  146. }else {
  147. account = tbAccountMapper.getById(Long.parseLong(accIdStr));
  148. account = BeanUtil.fillBeanWithMap(so, account,false);
  149. account.setUpdateBy(StpUserUtil.getLoginName());
  150. account.setUpdateTime(new Date());
  151. }
  152. account.insertOrUpdate();
  153. TbCostomer costomer = tbCostomerService.getById(account.getCustomerId());
  154. costomer.setDutyPeople(so.getString("dutyPeople"));
  155. costomer.setPhone(so.getString("phone"));
  156. costomer.updateById();
  157. }
  158. /**
  159. * 根据Map赋值,生成Account对象
  160. * @param so
  161. * @return
  162. */
  163. private TbAccount getTbAccountByMap(SoMap so){
  164. TbAccount account = BeanUtil.fillBeanWithMap(so, new TbAccount(),false);
  165. account.setId(null);
  166. String loginName = StpUserUtil.getLoginName();
  167. account.setCreateBy(loginName);
  168. account.setUpdateBy(loginName);
  169. account.setCreateTime(new Date());
  170. account.setUpdateTime(new Date());
  171. account.setStatus(1);
  172. account.setAccountNo(getIdByNow());
  173. return account;
  174. }
  175. /**
  176. * 根据车牌号获取绑定信息
  177. * @param plate
  178. * @return
  179. */
  180. public TbAccount getTbAccountByPlate(String plate){
  181. if(NbUtil.isNullStr(plate)) return null;
  182. return tbAccountMapper.getAccountByPlate(plate);
  183. }
  184. public String getIdByNow(){
  185. return DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4);
  186. }
  187. }