| package com.xzx.gc.pay.service;  | 
|   | 
| import cn.hutool.core.convert.Convert;  | 
| import cn.hutool.core.date.DateUtil;  | 
| import cn.hutool.core.util.NumberUtil;  | 
| import com.xzx.gc.common.constant.Constants;  | 
| import com.xzx.gc.common.exception.RestException;  | 
| import com.xzx.gc.common.utils.ExceptionUtils;  | 
| import com.xzx.gc.common.utils.RedisUtil;  | 
| import com.xzx.gc.entity.AccountInfo;  | 
| import com.xzx.gc.entity.AccountLog;  | 
| import com.xzx.gc.entity.CoreUser;  | 
| import com.xzx.gc.model.admin.XzxAccountLogModel;  | 
| import com.xzx.gc.model.comon.account.AllAcountParamDto;  | 
| import com.xzx.gc.model.user.AccountVo;  | 
| import com.xzx.gc.order.service.LimitApplyService;  | 
| import com.xzx.gc.pay.mapper.AccountLogMapper;  | 
| import com.xzx.gc.pay.mapper.AccountMapper;  | 
| import com.xzx.gc.service.AccountContext;  | 
| import com.xzx.gc.util.SessionUtil;  | 
| import org.springframework.beans.factory.annotation.Autowired;  | 
| import org.springframework.stereotype.Service;  | 
| import org.springframework.transaction.annotation.Transactional;  | 
|   | 
| import java.math.BigDecimal;  | 
| import java.util.HashMap;  | 
| import java.util.Map;  | 
|   | 
| @Service  | 
| @Transactional  | 
| public class AccountService {  | 
|   | 
|     @Autowired  | 
|     private AccountMapper accountMapper;  | 
|   | 
|     @Autowired  | 
|     private AccountLogMapper accountLogMapper;  | 
|   | 
|     @Autowired  | 
|     private SessionUtil sessionUtil;  | 
|   | 
|     @Autowired  | 
|     private RedisUtil redisUtil;  | 
|   | 
|     @Autowired  | 
|     private RestoreLimitService restoreLimitService;  | 
|   | 
|   | 
|     @Autowired  | 
|     private LimitApplyService limitApplyService;  | 
|   | 
|     public void update(AccountInfo accountInfo){  | 
|         accountMapper.updateByPrimaryKeySelective(accountInfo);  | 
|     }  | 
|   | 
|   | 
|     public AccountInfo findByUserId(String userId){  | 
|         AccountInfo accountInfo=new AccountInfo();  | 
|         accountInfo.setDelFlag(Constants.DEL_NOT_FLAG);  | 
|         accountInfo.setIsProhibit("0");  | 
|         accountInfo.setUserId(userId);  | 
|         AccountInfo accountInfo1 = accountMapper.selectOne(accountInfo);  | 
|         return accountInfo1;  | 
|     }  | 
|   | 
|     public AccountInfo findByUserIdForbidden(String userId){  | 
|         AccountInfo accountInfo=new AccountInfo();  | 
|         accountInfo.setDelFlag(Constants.DEL_NOT_FLAG);  | 
|         accountInfo.setUserId(userId);  | 
|         AccountInfo accountInfo1 = accountMapper.selectOne(accountInfo);  | 
|         return accountInfo1;  | 
|     }  | 
|   | 
|   | 
|   | 
|   | 
|   | 
|     /**  | 
|      *  | 
|      * @param receiverAccount 操作账户  | 
|      * @param channelType 操作渠道1:充值 2:提现 3:回收 4入库 5红包 6分享返利 7重量达标返利  | 
|      * @param channelId  操作ID 如充值 是充值流水号ID  | 
|      * @param channelMoney 操作金额  | 
|      */  | 
|     public void addAccount(AccountVo receiverAccount, String channelType, String channelId, String channelMoney){  | 
|   | 
|         Map map = new HashMap();  | 
|         map.put("userId", receiverAccount.getUserId());  | 
|         String accountId = null;  | 
|   | 
|         String now= DateUtil.now();  | 
|         AccountLog accountLog2 = new AccountLog();  | 
|         accountLog2.setCreateTime(now);  | 
|         accountLog2.setCreateUserId(receiverAccount.getUserId());  | 
|         accountLog2.setChannelType(Convert.toShort(channelType));  | 
|         accountLog2.setOrderId(channelId);  | 
|   | 
|   | 
|         //额度用完还用了环保金的情况  | 
|         String storageMoney = channelMoney;  | 
|         int limit = Convert.toInt(receiverAccount.getFixedLimit(),0);  | 
|         String receiverLimit = receiverAccount.getOverdraftLimit();  | 
|         accountId = receiverAccount.getAccountId();  | 
|         accountLog2.setAccountId(accountId);  | 
|         String money = receiverAccount.getMoney();  | 
|         BigDecimal v = NumberUtil.add(Convert.toBigDecimal(storageMoney), Convert.toBigDecimal(receiverLimit,Constants.MONEY_INIT));  | 
|         //查询账户信息  | 
|         if (v.compareTo(Convert.toBigDecimal(limit)) > 0) {  | 
|             //将额度填充完   再补money  | 
|             BigDecimal actualMoney = NumberUtil.sub(v, Convert.toBigDecimal(limit));  | 
|             map.put("money", actualMoney);  | 
|             map.put("limit", Convert.toBigDecimal(limit));  | 
|   | 
|             accountLog2.setOldMoney(money);  | 
|             accountLog2.setNewMoney(Convert.toStr(NumberUtil.add(Convert.toBigDecimal(money), Convert.toBigDecimal(actualMoney))));  | 
|             accountLog2.setOldLimit(Convert.toBigDecimal(receiverLimit,Constants.MONEY_INIT).toString());  | 
|             accountLog2.setNewLimit(Convert.toStr(limit));  | 
|             accountLogMapper.insertSelective(accountLog2);  | 
|             accountMapper.updateMoneyAndLimit(map);  | 
|         } else {  | 
|             //直接补额度  | 
|             map.put("money", Convert.toBigDecimal(storageMoney));  | 
|             accountLog2.setOldLimit(Convert.toBigDecimal(receiverLimit,Constants.MONEY_INIT).toString());  | 
|             accountLog2.setNewLimit(Convert.toStr(NumberUtil.add(Convert.toBigDecimal(receiverLimit,Constants.MONEY_INIT), Convert.toBigDecimal(storageMoney))));  | 
|             accountLogMapper.insertSelective(accountLog2);  | 
|             accountMapper.updateLimitByOrder(map);  | 
|         }  | 
|   | 
|     }  | 
|   | 
|     private XzxAccountLogModel getAccountLog(AccountInfo accountInfo,String money){  | 
|         XzxAccountLogModel model = new XzxAccountLogModel();  | 
|         model.setOrderId("");  | 
|         model.setChannelType(9);  | 
|         model.setCreateTime(DateUtil.now());  | 
|         CoreUser user = sessionUtil.getCurrentUser();  | 
|         model.setCreateUserId(Long.toString(user.getId()));  | 
|         model.setAccountId(accountInfo.getAccountId());  | 
|         model.setOldMoney(accountInfo.getMoney());  | 
|         model.setNewMoney(accountInfo.getMoney());  | 
|         model.setOldLimit(accountInfo.getOverdraftLimit());  | 
|         model.setNewLimit(new BigDecimal(accountInfo.getOverdraftLimit()).add(new BigDecimal(money)).toString());  | 
|         model.setOldFixedLimit(accountInfo.getFixedLimit());  | 
|         model.setNewFixedLimit(new BigDecimal(accountInfo.getFixedLimit()).add(new BigDecimal(money)).toString());  | 
|         return model;  | 
|     }  | 
|   | 
|   | 
|     public AccountInfo queryById(String accountId) {  | 
|   | 
|         return accountMapper.selectByPrimaryKey(accountId);  | 
|     }  | 
|   | 
|     public void updateOverdrawPriceByUserId(String accountId,String money){  | 
|         //修改账户推送队列  | 
|         AccountInfo accountInfo = queryById(accountId);  | 
|         XzxAccountLogModel getAccountLog=getAccountLog(accountInfo,money);  | 
|         accountMapper.updateOverdrawPriceByUserId(accountId,money);  | 
|     }  | 
|   | 
|     /**  | 
|      * 提高额度  | 
|      * @param userId  | 
|      * @param money  | 
|      */  | 
|     public void increaseOverdraftLimit(String userId,BigDecimal money,String payOrderId){  | 
|         if(redisUtil.setnx(Constants.REDIS_USER_KEY+"increaseOverdraftLimit:"+userId,"0")){  | 
|             try {  | 
|                 AllAcountParamDto allAcountParamDto=AllAcountParamDto.builder().type(8).flowNo(payOrderId).userId(userId).  | 
|                         money(money).build();  | 
| //                accountFactory.getService(8).account(allAcountParamDto);  | 
|                 new AccountContext(limitApplyService).account(allAcountParamDto);  | 
|             } catch (RestException e){  | 
|                 ExceptionUtils.err(e.getMsg(),e);  | 
|             }catch (Exception e) {  | 
|                 ExceptionUtils.err("提升额度失败",e);  | 
|             }finally {  | 
|                 redisUtil.del(Constants.REDIS_USER_KEY+"increaseOverdraftLimit:"+userId);  | 
|             }  | 
|         }  | 
|     }  | 
|   | 
|     /**  | 
|      * 恢复固定额度  | 
|      * @param userId  | 
|      */  | 
|     public void restoreFix(String userId,String payOrderId,String auditUserType){  | 
|         if(redisUtil.setnx(Constants.REDIS_USER_KEY+"restoreFix:"+userId,"0")){  | 
|             try {  | 
|                 AllAcountParamDto allAcountParamDto=AllAcountParamDto.builder().type(1).flowNo(payOrderId)  | 
|                         .userId(userId).userRealType(auditUserType).build();  | 
| //                accountFactory.getService(1).account(allAcountParamDto);  | 
|                 new AccountContext(restoreLimitService).account(allAcountParamDto);  | 
|             }catch (RestException e){  | 
|                 ExceptionUtils.err(e.getMsg(),e);  | 
|             }catch (Exception e) {  | 
|                 ExceptionUtils.err("恢复额度失败",e);  | 
|             }finally {  | 
|                 redisUtil.del(Constants.REDIS_USER_KEY+"restoreFix:"+userId);  | 
|             }  | 
|         }  | 
|     }  | 
|   | 
| }  |