package com.xzx.gc.order.service;
|
|
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.util.NumberUtil;
|
import com.xzx.gc.common.constant.Constants;
|
import com.xzx.gc.common.constant.PayEnum;
|
import com.xzx.gc.common.exception.RestException;
|
import com.xzx.gc.common.utils.*;
|
import com.xzx.gc.entity.AccountInfo;
|
import com.xzx.gc.entity.AccountLog;
|
import com.xzx.gc.model.comon.account.AllAcountParamDto;
|
import com.xzx.gc.model.user.AccountVo;
|
import com.xzx.gc.order.mapper.AccountLogMapper;
|
import com.xzx.gc.order.mapper.AccountMapper;
|
import com.xzx.gc.order.mapper.PayInfoMapper;
|
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 tk.mybatis.mapper.entity.Example;
|
|
import java.math.BigDecimal;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@Service
|
@Transactional
|
public class AccountService {
|
|
@Autowired
|
private AccountMapper accountMapper;
|
|
@Autowired
|
private AccountLogMapper accountLogMapper;
|
|
|
|
@Autowired
|
private PayInfoMapper payInfoMapper;
|
|
|
@Autowired
|
private IdUtils idUtils;
|
|
|
|
|
|
public void updateMoneyByUserId(String userId,BigDecimal money){
|
Map<String,Object> map=MapUtil.newHashMap();
|
map.put("userId", userId);
|
map.put("money", money);
|
accountMapper.updateMyMoneyByOrder(map);
|
}
|
|
public void updateScoreByUserId(String userId,BigDecimal money){
|
Map<String,Object> map=MapUtil.newHashMap();
|
map.put("userId", userId);
|
map.put("money", money);
|
accountMapper.updateMyScoreByOrder(map);
|
}
|
|
|
|
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 findById(String id){
|
return accountMapper.selectByPrimaryKey(id);
|
}
|
|
|
|
|
|
/**
|
*
|
* @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){
|
String accountId = receiverAccount.getAccountId();
|
String now=DateUtil.now();
|
//回收员
|
Map payMap = new HashMap();
|
String payOrderId=idUtils.generate("ZF",0);
|
payMap.put("payOrderId",payOrderId);
|
payMap.put("orderId",channelId);
|
payMap.put("createUserId",receiverAccount.getUserId());
|
payMap.put("money",channelMoney);
|
payMap.put("accountId",accountId);
|
payMap.put("payType", PayEnum.环保金收入.getValue());
|
payMap.put("status",PayEnum.已支付.getValue());
|
payMap.put("createTime",now);
|
payInfoMapper.payInfoAdd(payMap);
|
|
|
//添加账户
|
AccountInfo accountInfo=new AccountInfo();
|
BeanUtil.copyProperties(receiverAccount,accountInfo);
|
addOtherUserAccount(accountInfo,channelType,payOrderId,channelMoney);
|
|
}
|
|
public void update(AccountInfo accountInfo){
|
accountMapper.updateByPrimaryKeySelective(accountInfo);
|
}
|
|
|
/**
|
* 回收员加钱
|
* @param accountInfo
|
* @param channelType
|
* @param payOrderId
|
* @param channelMoney
|
*/
|
public void addOtherUserAccount(AccountInfo accountInfo,String channelType,String payOrderId,String channelMoney) {
|
|
Map map = new HashMap();
|
map.put("userId", accountInfo.getUserId());
|
|
|
AccountLog accountLog2 = new AccountLog();
|
accountLog2.setCreateTime(DateUtil.now());
|
accountLog2.setCreateUserId(accountInfo.getUserId());
|
accountLog2.setChannelType(Convert.toShort(channelType));
|
accountLog2.setOrderId(payOrderId);
|
|
|
//额度用完还用了环保金的情况
|
BigDecimal limit = Convert.toBigDecimal(accountInfo.getFixedLimit(),Constants.MONEY_INIT);
|
String receiverLimit = accountInfo.getOverdraftLimit();
|
accountLog2.setAccountId(accountInfo.getAccountId());
|
String money = accountInfo.getMoney();
|
BigDecimal v = NumberUtil.add(Convert.toBigDecimal(channelMoney), Convert.toBigDecimal(receiverLimit,Constants.MONEY_INIT));
|
//查询账户信息
|
if (v.compareTo(limit) > 0) {
|
//将额度填充完 再补money
|
BigDecimal actualMoney = NumberUtil.sub(v, limit);
|
map.put("money", actualMoney);
|
map.put("limit", limit);
|
|
accountLog2.setOldMoney(money);
|
accountLog2.setNewMoney(Convert.toStr(NumberUtil.add(Convert.toBigDecimal(money), Convert.toBigDecimal(actualMoney))));
|
accountLog2.setOldLimit(receiverLimit);
|
accountLog2.setNewLimit(Convert.toStr(limit));
|
accountLogMapper.insertSelective(accountLog2);
|
accountMapper.updateMoneyAndLimit(map);
|
} else {
|
//直接补额度
|
map.put("money", Convert.toBigDecimal(channelMoney));
|
accountLog2.setOldLimit(receiverLimit);
|
accountLog2.setNewLimit(Convert.toStr(NumberUtil.add(Convert.toBigDecimal(receiverLimit), Convert.toBigDecimal(channelMoney))));
|
accountLogMapper.insertSelective(accountLog2);
|
accountMapper.updateLimitByOrder(map);
|
}
|
}
|
|
|
|
|
|
|
}
|