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 implements IService { /** * 底层 Mapper 对象 */ @Autowired TbAccountMapper tbAccountMapper; @Resource TbCostomerService tbCostomerService; @Resource TbChargeRecordService tbChargeRecordService; /** * 根据客户Id,获取客户存款账户,并解密金额 * @param customerId * @return */ public TbAccount getByCustomerId(String customerId) { QueryWrapper 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 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); } }