Helius
2021-07-01 c3b54650540167000b044fbd6d724765e5aae978
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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);
        }
    }
 
 
 
 
 
 
}