123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package com.pj.project.tb_account;
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.codec.Base64;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.NumberUtil;
- import cn.hutool.core.util.RandomUtil;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.extension.service.IService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.pj.current.satoken.StpUserUtil;
- import com.pj.project.tb_charge_record.TbChargeRecord;
- import com.pj.project.tb_charge_record.TbChargeRecordService;
- import com.pj.project.tb_costomer.TbCostomer;
- import com.pj.project.tb_costomer.TbCostomerService;
- import com.pj.utils.AesUtil;
- import com.pj.utils.sg.AjaxError;
- import com.pj.utils.sg.NbUtil;
- import com.pj.utils.so.SoMap;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.math.BigDecimal;
- import java.util.Date;
- import java.util.List;
- /**
- * Service: tb_account -- 客户账户
- *
- * @author qzy
- */
- @Service
- public class TbAccountService extends ServiceImpl<TbAccountMapper, TbAccount> implements IService<TbAccount> {
- /**
- * 底层 Mapper 对象
- */
- @Autowired
- TbAccountMapper tbAccountMapper;
- @Resource
- TbCostomerService tbCostomerService;
- @Resource
- TbChargeRecordService tbChargeRecordService;
- /**
- * 根据客户Id,获取客户存款账户,并解密金额
- * @param customerId
- * @return
- */
- public TbAccount getByCustomerId(String customerId) {
- QueryWrapper<TbAccount> ew = new QueryWrapper<>();
- ew.eq("customer_id", customerId);
- TbAccount account = tbAccountMapper.selectOne(ew);
- if (account == null) {
- account = new TbAccount();
- }
- String key = AesUtil.reverse(account.getAccSalt());
- account.setTotalMoney(AesUtil.decryptECB(account.getTotalMoney(),key));
- return account;
- }
- public List<TbAccountBO> getList(SoMap so){
- return tbAccountMapper.getTbAccountBOList(so);
- }
- public void recharge(String customerId, String money) {
- TbAccount account = this.getByCustomerId(customerId);
- recharge(account, TbChargeRecord.Ch.PC.getType(), new BigDecimal(money), new BigDecimal(money));
- }
- public void recharge(String id, BigDecimal money, String chargeType, BigDecimal payMoney) {
- TbAccount account = this.getById(id);
- recharge(account, chargeType, money, payMoney);
- }
- public void recharge(TbAccount account, String chargeType, BigDecimal money, BigDecimal payMoney) {
- String totalMoney = account.getTotalMoney();
- if (NumberUtil.isNumber(totalMoney)) {
- totalMoney = Base64.encode(totalMoney.getBytes());
- }
- BigDecimal total = new BigDecimal(Base64.decodeStr(totalMoney)).add(money);
- account.setTotalMoney(Base64.encode(total.toString()));
- this.updateById(account);
- String customerId = account.getCustomerId();
- TbChargeRecord record = new TbChargeRecord();
- TbCostomer tbCostomer = tbCostomerService.getById(customerId);
- record.setChargeChannel(chargeType).setMoney(money).setPayMoney(payMoney)
- .setCreateTime(new Date()).setNo(DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4))
- .setCustomerId(customerId).setCustomerName(tbCostomer.getName());
- tbChargeRecordService.save(record);
- }
- /**
- * 充值
- */
- public void recharge(SoMap so){
- String salt = so.getString("salt");
- String preTopupMoney = so.getString("preTopupMoney");//充值金额
- String discountMoney = so.getString("discountMoney");//优惠金额
- String key = AesUtil.reverse(salt);
- String decTotalTopup = AesUtil.decryptECB(preTopupMoney,key);
- if(!NumberUtil.isNumber(decTotalTopup)){
- throw new AjaxError("充值金额必须是数字!");
- }
- if(Double.valueOf(decTotalTopup)<=0){
- throw new AjaxError("总计充值金额不得小于零元!");
- }
- String beforeBalance = preTopupMoney;
- TbAccount account;
- String accIdStr = so.getString("id");
- if(!NumberUtil.isNumber(accIdStr)){
- account = getTbAccountByMap(so);
- account.setTotalMoney(preTopupMoney);
- account.setAccSalt(salt);
- }else {
- long accId = Long.parseLong(accIdStr);
- account = tbAccountMapper.getById(accId);
- String totalMoneyStr = AesUtil.decryptECB(account.getTotalMoney(),AesUtil.reverse(account.getAccSalt()));
- if(NumberUtil.isNumber(totalMoneyStr)){
- beforeBalance = account.getTotalMoney();
- BigDecimal totalMoneyBig =new BigDecimal(totalMoneyStr).add(new BigDecimal(decTotalTopup));
- account.setTotalMoney(AesUtil.encryptECB(totalMoneyBig.toString(),key));
- account.setAccSalt(salt);
- account.setUpdateTime(new Date());
- account.setUpdateBy(StpUserUtil.getLoginName());
- }
- }
- account.insertOrUpdate();
- //保存充值记录
- TbChargeRecord record = BeanUtil.fillBeanWithMap(so, new TbChargeRecord(),false);
- record.setStatus(1)
- .setId(null)
- .setAccountId(account.getId())
- .setTotalTopupMoney(preTopupMoney)
- .setPreTopupMoney(preTopupMoney)
- .setDiscountMoney(discountMoney)
- .setTotalMoney(account.getTotalMoney())
- .setBeforeBalance(beforeBalance)
- .setChargeChannel(TbChargeRecord.Ch.PC.getType())
- .setCreateTime(new Date())
- .setSalt(salt)
- .setChargePeople(StpUserUtil.getLoginName())
- .setNo(getIdByNow());
- tbChargeRecordService.save(record);
- }
- /**
- * 修改Account
- * @param so
- */
- public void updateAccount(SoMap so){
- String accIdStr = so.getString("id");
- TbAccount account = null;
- if(!NumberUtil.isNumber(accIdStr)){
- account = getTbAccountByMap(so);
- }else {
- account = tbAccountMapper.getById(Long.parseLong(accIdStr));
- account = BeanUtil.fillBeanWithMap(so, account,false);
- account.setUpdateBy(StpUserUtil.getLoginName());
- account.setUpdateTime(new Date());
- }
- account.insertOrUpdate();
- TbCostomer costomer = tbCostomerService.getById(account.getCustomerId());
- costomer.setDutyPeople(so.getString("dutyPeople"));
- costomer.setPhone(so.getString("phone"));
- costomer.updateById();
- }
- /**
- * 根据Map赋值,生成Account对象
- * @param so
- * @return
- */
- private TbAccount getTbAccountByMap(SoMap so){
- TbAccount account = BeanUtil.fillBeanWithMap(so, new TbAccount(),false);
- account.setId(null);
- String loginName = StpUserUtil.getLoginName();
- account.setCreateBy(loginName);
- account.setUpdateBy(loginName);
- account.setCreateTime(new Date());
- account.setUpdateTime(new Date());
- account.setStatus(1);
- account.setAccountNo(getIdByNow());
- return account;
- }
- /**
- * 根据车牌号获取绑定信息
- * @param plate
- * @return
- */
- public TbAccount getTbAccountByPlate(String plate){
- if(NbUtil.isNullStr(plate)) return null;
- return tbAccountMapper.getAccountByPlate(plate);
- }
- public String getIdByNow(){
- return DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4);
- }
- }
|