zj
2024-06-03 287ac389edd047696d956afafdb855a93830bc0c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.nq.service.impl;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nq.common.ServerResponse;
import com.nq.dao.*;
import com.nq.pojo.*;
import com.nq.service.IFundsApplyService;
import com.nq.service.IFundsSettingService;
import com.nq.service.IUserService;
import com.nq.utils.DateTimeUtil;
import com.nq.utils.KeyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
/**
 * 配资申请
 * @author lr
 * @date 2020/07/24
 */
@Service("IFundsApplyService")
public class FundsApplyServiceImpl implements IFundsApplyService {
    private static final Logger log = LoggerFactory.getLogger(FundsApplyServiceImpl.class);
 
    @Resource
    private FundsApplyMapper fundsApplyMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserCashDetailMapper userCashDetailMapper;
    @Autowired
    FundsTradingAccountMapper fundsTradingAccountMapper;
 
    @Autowired
    IUserService iUserService;
    @Autowired
    IFundsSettingService iFundsSettingService;
 
 
    @Transactional
    public ServerResponse insert(FundsApply model, HttpServletRequest request) throws Exception {
        int ret = 0;
        if (model == null) {
            return ServerResponse.createBySuccessMsg("操作异常,请稍后重试!");
        }
        User user = this.iUserService.getCurrentRefreshUser(request);
        if(user == null){
            return ServerResponse.createBySuccessMsg("请登录后操作");
        }
        BigDecimal user_enable_amt = user.getEnableAmt();
        //支付金额=保证金+管理费
        BigDecimal pay_amount = model.getMargin().add(model.getManageFee());
        int compareUserAmtInt = user_enable_amt.compareTo(pay_amount);
        log.info("用户可用金额 = {}  实际购买金额 =  {} 比较结果 = {} ", user_enable_amt, pay_amount, compareUserAmtInt);
        if (compareUserAmtInt == -1) {
            return ServerResponse.createByErrorMsg("申请失败,可用金额小于" + pay_amount + "元");
        }
 
        //自动生成订单编号
        model.setOrderNumber(KeyUtils.getUniqueKey());
        model.setPayAmount(pay_amount);
        ret = fundsApplyMapper.insert(model);
        if(ret>0){
            //修改用户可用余额= 当前余额-支付金额
            BigDecimal reckon_enable = user_enable_amt.subtract(pay_amount);
            user.setEnableAmt(reckon_enable);
            int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
            if (updateUserCount > 0) {
                log.info("【用户交易下单】修改用户金额成功");
                UserCashDetail ucd = new UserCashDetail();
                ucd.setPositionId(model.getId());
                ucd.setAgentId(user.getAgentId());
                ucd.setAgentName(user.getAgentName());
                ucd.setUserId(user.getId());
                ucd.setUserName(user.getRealName());
                ucd.setDeType("配资冻结");
                ucd.setDeAmt(model.getPayAmount().multiply(new BigDecimal("-1")));
                ucd.setDeSummary("申请按天配资:" + model.getOrderNumber() + ",冻结金额:" + model.getPayAmount().multiply(new BigDecimal("-1")) );
                ucd.setAddTime(new Date());
                ucd.setIsRead(Integer.valueOf(0));
                int insertSxfCount = this.userCashDetailMapper.insert(ucd);
                if (insertSxfCount > 0) {
                    log.info("【按天配资】申请成功");
                }
 
            } else {
                log.error("【按天配资】修改用户金额出错");
                throw new Exception("【按天配资】修改用户金额出错");
            }
            return ServerResponse.createBySuccessMsg("申请成功!");
        } else {
            return ServerResponse.createBySuccessMsg("申请失败,请稍后重试!");
        }
    }
 
    @Override
    public int update(FundsApply model) {
        int ret = fundsApplyMapper.update(model);
        return ret>0 ? ret: 0;
    }
 
    /**
     * 配资申请-保存
     */
    @Override
    public ServerResponse save(FundsApply model) {
        int ret = 0;
        if(model!=null && model.getId()>0){
            ret = fundsApplyMapper.update(model);
        } else{
            ret = fundsApplyMapper.insert(model);
        }
        if(ret>0){
            return ServerResponse.createBySuccessMsg("操作成功");
        }
        return ServerResponse.createByErrorMsg("操作失败");
    }
 
    /**
     * 配资申请-审核
     */
    /*     */   @Transactional
    /*     */   public ServerResponse audit(FundsApply model, HttpServletRequest request) throws Exception {
        /* 135 */     FundsApply fundsApply = this.fundsApplyMapper.load(model.getId().intValue());
        /* 136 */     int ret = 0;
        /* 137 */     if (model != null && model.getId().intValue() > 0) {
            /* 138 */       User user = this.userMapper.selectByPrimaryKey(fundsApply.getUserId());
            /*     */
            /* 140 */       if (model.getStatus().intValue() == 1) {
                /* 141 */         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                /* 142 */         String begtime = df.format(new Date()).split(" ")[0] + " 0:00:00";
                /* 143 */         Date date = DateTimeUtil.strToDate(begtime);
                /* 144 */         Date begDate = DateTimeUtil.addDay(date, 1);
                /* 145 */         model.setBeginTime(begDate);
                /* 146 */         String endtime = df.format(new Date()).split(" ")[0] + " 23:59:59";
                /* 147 */         Date endDate = DateTimeUtil.strToDate(endtime);
                /* 148 */         endDate = DateTimeUtil.addDay(endDate, model.getTradersCycle().intValue() + 1);
                /* 149 */         model.setEndTime(endDate);
                /* 150 */         model.setEnabledTradingAmount(fundsApply.getTotalTradingAmount());
                /* 151 */         FundsSetting fundsSetting = this.iFundsSettingService.getFundsSetting();
                /*     */
                /* 153 */         BigDecimal lineUnwind = fundsApply.getMargin().multiply(fundsSetting.getDaysUnwind()).add(fundsApply.getFundsAmount()).setScale(2, 4);
                /* 154 */         model.setLineUnwind(lineUnwind);
                /*     */
                /* 156 */         BigDecimal lineWarning = fundsApply.getMargin().multiply(fundsSetting.getDaysWarning()).add(fundsApply.getFundsAmount()).setScale(2, 4);
                /* 157 */         model.setLineWarning(lineWarning);
                /*     */       }
            /* 159 */       model.setAuditTime(DateTimeUtil.getCurrentDate());
            /* 160 */       ret = this.fundsApplyMapper.update(model);
            /* 161 */       if (ret > 0) {
                /* 162 */         BigDecimal user_enable_amt = user.getEnableAmt();
                /*     */
                /* 164 */         if (model.getStatus().intValue() == 1) {
                    /* 165 */           BigDecimal user_all_amt = user.getUserAmt();
                    /*     */
                    /* 167 */           BigDecimal reckon_all = user_all_amt.subtract(fundsApply.getManageFee());
                    /*     */
                    /* 169 */           BigDecimal tradingAmount = user.getTradingAmount().add(fundsApply.getTotalTradingAmount());
                    /* 170 */           log.info("【配资审核通过】用户平总资金  = {} , 可用资金 = {} , 总操盘资金 = {}", new Object[] { reckon_all, user_enable_amt, tradingAmount });
                    /* 171 */           user.setUserAmt(reckon_all);
                    /* 172 */           user.setTradingAmount(tradingAmount);
                    /* 173 */           int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
                    /* 174 */           if (updateUserCount > 0) {
                        /* 175 */             log.info("【配资审核通过】修改用户金额成功");
                        /*     */
                        /* 177 */             FundsTradingAccount fundsTradingAccount = this.fundsTradingAccountMapper.getAccountByNumber(model.getSubaccountNumber());
                        /* 178 */             if (fundsTradingAccount != null) {
                            /* 179 */               fundsTradingAccount.setStatus(Integer.valueOf(1));
                            /* 180 */               this.fundsTradingAccountMapper.update(fundsTradingAccount);
                            /*     */             }
                        /*     */
                        /* 183 */             UserCashDetail ucd = new UserCashDetail();
                        /* 184 */             ucd.setPositionId(fundsApply.getId());
                        /* 185 */             ucd.setAgentId(user.getAgentId());
                        /* 186 */             ucd.setAgentName(user.getAgentName());
                        /* 187 */             ucd.setUserId(user.getId());
                        /* 188 */             ucd.setUserName(user.getRealName());
                        /* 189 */             ucd.setDeType("配资审核通过");
                        /* 190 */             ucd.setDeAmt(fundsApply.getPayAmount());
                        /* 191 */             ucd.setDeSummary("申请按天配资:" + fundsApply.getOrderNumber() + ",配资审核通过,解冻保证金到配资账户:" + fundsApply.getPayAmount());
                        /* 192 */             ucd.setAddTime(new Date());
                        /* 193 */             ucd.setIsRead(Integer.valueOf(0));
                        /* 194 */             int insertSxfCount = this.userCashDetailMapper.insert(ucd);
                        /* 195 */             if (insertSxfCount > 0) {
                            /* 196 */               log.info("【配资审核通过】申请成功");
                            /*     */             }
                        /*     */           } else {
                        /* 199 */             log.error("【配资审核通过】修改用户金额出错");
                        /* 200 */             throw new Exception("【配资审核通过】修改用户金额出错");
                        /*     */           }
                    /*     */
                    /*     */         } else {
                    /*     */
                    /* 205 */           BigDecimal reckon_enable = user_enable_amt.add(fundsApply.getPayAmount());
                    /* 206 */           user.setEnableAmt(reckon_enable);
                    /* 207 */           int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
                    /* 208 */           if (updateUserCount > 0) {
                        /* 209 */             log.info("【配资审核未通过】修改用户金额成功");
                        /* 210 */             UserCashDetail ucd = new UserCashDetail();
                        /* 211 */             ucd.setPositionId(fundsApply.getId());
                        /* 212 */             ucd.setAgentId(user.getAgentId());
                        /* 213 */             ucd.setAgentName(user.getAgentName());
                        /* 214 */             ucd.setUserId(user.getId());
                        /* 215 */             ucd.setUserName(user.getRealName());
                        /* 216 */             ucd.setDeType("配资审核未通过");
                        /* 217 */             ucd.setDeAmt(fundsApply.getPayAmount());
                        /* 218 */             ucd.setDeSummary("申请按天配资:" + fundsApply.getOrderNumber() + ",配资审核未通过,解冻保证金到余额:" + fundsApply.getPayAmount() + ",原因:" + model.getAuditOpinion());
                        /* 219 */             ucd.setAddTime(new Date());
                        /* 220 */             ucd.setIsRead(Integer.valueOf(0));
                        /* 221 */             int insertSxfCount = this.userCashDetailMapper.insert(ucd);
                        /* 222 */             if (insertSxfCount > 0) {
                            /* 223 */               log.info("【按天配资】申请成功");
                            /*     */             }
                        /*     */           } else {
                        /* 226 */             log.error("【按天配资】修改用户金额出错");
                        /* 227 */             throw new Exception("【按天配资】修改用户金额出错");
                        /*     */           }
                    /*     */         }
                /*     */
                /* 231 */         log.info("配资申请-审核 = {}  实际购买金额 =  {} 比较结果 = {} ", Integer.valueOf(0));
                /*     */       }
            /*     */     }
        /* 234 */     if (ret > 0) {
            /* 235 */       return ServerResponse.createBySuccessMsg("操作成功");
            /*     */     }
        /* 237 */     return ServerResponse.createByErrorMsg("操作失败");
        /*     */   }
 
 
    /*配资申请-查询列表*/
    @Override
    public ServerResponse<PageInfo> getList(int pageNum, int pageSize, String keyword, Integer status, HttpServletRequest request){
        PageHelper.startPage(pageNum, pageSize);
        List<FundsApply> listData = this.fundsApplyMapper.pageList(pageNum, pageSize, keyword, status);
        PageInfo pageInfo = new PageInfo(listData);
        pageInfo.setList(listData);
        return ServerResponse.createBySuccess(pageInfo);
    }
 
    /*配资申请-查询详情*/
    @Override
    public ServerResponse getDetail(int id) {
        return ServerResponse.createBySuccess(this.fundsApplyMapper.load(id));
    }
 
    /**
     * 配资申请-用户配资列表
     */
    @Override
    public ServerResponse<PageInfo> getUserApplyList(int pageNum, int pageSize, int userId, HttpServletRequest request){
        User user = this.iUserService.getCurrentRefreshUser(request);
        if (user == null){
            return ServerResponse.createBySuccessMsg("請先登錄");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<FundsApply> listData = this.fundsApplyMapper.getUserApplyList(pageNum, pageSize, user.getId());
        PageInfo pageInfo = new PageInfo(listData);
        pageInfo.setList(listData);
        return ServerResponse.createBySuccess(pageInfo);
    }
 
    /**
     * 配资申请-用户操盘中子账户
     */
    @Override
    public ServerResponse<PageInfo> getUserEnabledSubaccount(HttpServletRequest request){
        User user = this.iUserService.getCurrentRefreshUser(request);
        List<FundsApply> listData = this.fundsApplyMapper.getUserEnabledSubaccount(user.getId());
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(listData);
        return ServerResponse.createBySuccess(pageInfo);
    }
 
}