zj
2025-01-06 0e7b38c2b3af72ea2a7f8a2fcbaad4d78e2c1977
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
package com.gear.admin.controller.swx;
 
import java.math.BigDecimal;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gear.admin.vo.swx.IdentificationVo;
import com.gear.admin.vo.swx.LoanRecordVo;
import com.gear.common.builder.WhereBuilder;
import com.gear.common.constant.SwxConstons;
import com.gear.common.exception.CustomerException;
import com.gear.common.vo.Result;
import com.gear.swx.domain.*;
import com.gear.swx.service.ISwxMoneyLogService;
import com.gear.swx.service.ISwxSettingsService;
import com.gear.swx.service.ISwxUserService;
import io.jsonwebtoken.lang.Collections;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.gear.common.annotation.Log;
import com.gear.common.core.controller.BaseController;
import com.gear.common.enums.BusinessType;
import com.gear.swx.service.ISwxLoanRecordService;
import com.gear.common.utils.poi.ExcelUtil;
 
/**
 * 贷款记录Controller
 * 
 * @author czx
 * @date 2023-11-18
 */
@RestController
@RequestMapping("/swx/loanRecord")
public class SwxLoanRecordController extends BaseController
{
    @Autowired
    private ISwxLoanRecordService swxLoanRecordService;
 
    @Autowired
    private ISwxUserService swxUserService;
 
    @Autowired
    private ISwxMoneyLogService swxMoneyLogService;
 
    @Autowired
    private ISwxSettingsService swxSettingsService;
    /**
     * 查询贷款记录列表
     */
    @PreAuthorize("@ss.hasPermi('swx:loanRecord:list')")
    @GetMapping("/list")
    public Result<IPage<LoanRecordVo>> list(LoanRecordVo swxLoanRecord,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        QueryWrapper<SwxLoanRecord> queryWrapper = new QueryWrapper<>();
        if(!Strings.isEmpty(swxLoanRecord.getPhone()) || !Strings.isEmpty(swxLoanRecord.getUserName())){
            QueryWrapper<SwxUser> userQueryWrapper = new QueryWrapper<>();
            if(!Strings.isEmpty(swxLoanRecord.getUserName())){
                userQueryWrapper.lambda().like(SwxUser::getUserName,swxLoanRecord.getUserName());
            }
            if(!Strings.isEmpty(swxLoanRecord.getPhone())){
                userQueryWrapper.lambda().like(SwxUser::getPhone,swxLoanRecord.getPhone());
            }
 
            List<SwxUser> list = swxUserService.list(userQueryWrapper);
            List<String> ids = new ArrayList<>();
            if(!Collections.isEmpty(list)){
                for (SwxUser item : list){
                    ids.add(item.getId());
                }
            }else{
                ids.add("");
            }
            queryWrapper.lambda().in(SwxLoanRecord::getUserId,ids);
        }
        if(swxLoanRecord.getStatus() != null){
            queryWrapper.lambda().eq(SwxLoanRecord::getStatus,swxLoanRecord.getStatus());
        }
        Page<SwxLoanRecord> page = new Page<SwxLoanRecord>(pageNo, pageSize);
        queryWrapper.lambda().orderByDesc(SwxLoanRecord::getCreateTime);
        IPage<SwxLoanRecord> pageList = swxLoanRecordService.page(page, queryWrapper);
        IPage<LoanRecordVo> result = new Page<>();
        List<LoanRecordVo> records = new ArrayList<>();
        for(SwxLoanRecord item : pageList.getRecords()){
            LoanRecordVo vo = new LoanRecordVo();
            BeanUtils.copyProperties(item,vo);
            SwxUser swxUser = swxUserService.getById(item.getUserId());
            if(swxUser != null){
                vo.setUserName(swxUser.getUserName());
                vo.setPhone(swxUser.getPhone());
            }
            records.add(vo);
        }
        result.setRecords(records);
        result.setCurrent(pageList.getCurrent());
        result.setPages(pageList.getPages());
        result.setSize(pageList.getSize());
        result.setTotal(pageList.getTotal());
        return Result.OK(result);
    }
 
    /**
     * 导出贷款记录列表
     */
    @PreAuthorize("@ss.hasPermi('swx:loanRecord:export')")
    @Log(title = "贷款记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SwxLoanRecord swxLoanRecord){
        QueryWrapper<SwxLoanRecord> queryWrapper = WhereBuilder.build(swxLoanRecord);
        List<SwxLoanRecord> list = swxLoanRecordService.list(queryWrapper);
        ExcelUtil<SwxLoanRecord> util = new ExcelUtil<SwxLoanRecord>(SwxLoanRecord.class);
        util.exportExcel(response, list, "贷款记录数据");
    }
 
    /**
     * 获取贷款记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('swx:loanRecord:query')")
    @GetMapping(value = "/{id}")
    public Result<SwxLoanRecord>  getInfo(@PathVariable("id") String id){
        SwxLoanRecord swxLoanRecord = swxLoanRecordService.getById(id);
        if(swxLoanRecord==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(swxLoanRecord);
    }
 
 
    /**
     * 修改贷款记录
     */
    @PreAuthorize("@ss.hasPermi('swx:loanRecord:edit')")
    @Log(title = "贷款记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public  Result<String> edit(@RequestBody SwxLoanRecord swxLoanRecord){
        //判断修改状态
        if(swxLoanRecord == null || swxLoanRecord.getStatus() <= 0){
            return Result.error("编辑失败!");
        }
        //充值成功
        if (swxLoanRecord.getStatus() == SwxConstons.SWX_EXAMINE_STATUS_YES){
            SwxLoanRecord old = swxLoanRecordService.getById(swxLoanRecord.getId());
            SwxUser swxUser = swxUserService.getById(old.getUserId());
            if(swxUser != null){
                SwxMoneyLog moneyLog = new SwxMoneyLog();
                moneyLog.setInfo("用户贷款,总额为:"+old.getAmount()+"利率为:"+old.getRete());
                moneyLog.setStatus(SwxConstons.SWX_EXAMINE_STATUS_YES);
                moneyLog.setType(SwxConstons.SWX_MONEY_LOG_TYPE_LOAN);
                moneyLog.setSymbol(SwxConstons.SWX_MONEY_TYPE_INCOME);
                moneyLog.setTitle("用户贷款");
                moneyLog.setOldAmount(swxUser.getAmount());
                swxUser.setAmount(swxUser.getAmount().add(old.getAmount()));
                //增加用户贷款金额
                swxUser.setLoanAmount(swxUser.getLoanAmount().add(old.getAmount()));
                swxUserService.updateById(swxUser);
                moneyLog.setNowAmount(swxUser.getAmount());
                moneyLog.setUserId(swxUser.getId());
                moneyLog.setBusiId(old.getId());
                moneyLog.setMoney(old.getAmount());
                swxMoneyLogService.save(moneyLog);
                //计算贷款开始天数,贷款计息
                //获取系统参数
                SwxSettings dateSettings = swxSettingsService.getOne(new QueryWrapper<SwxSettings>().lambda().eq(SwxSettings::getParamKey,"loan_rate_begin_time"));
                if(dateSettings == null){
                    return Result.error("系统参数获取有误!");
                }
                Integer rateDate = Integer.parseInt(dateSettings.getParamValue());
                //进行运算,计算贷款开始计息时间,总利息
                LocalDateTime currentDate = LocalDateTime.now();
 
                // 计算计息开始时间
                LocalDateTime afterDays = currentDate.plusDays(rateDate);
                swxLoanRecord.setBeginDay(Date.from(afterDays.atZone(ZoneId.systemDefault()).toInstant()));
                //下次还款时间
                swxLoanRecord.setNextInterstTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(Date.from(afterDays.atZone(ZoneId.systemDefault()).toInstant())));
                //计算总利息
                Integer rateDays =old.getTerm() - rateDate;
                //总利息等于总金额*时间*日利率
                BigDecimal interest = (old.getAmount().multiply(new BigDecimal(rateDays)).multiply(old.getRete()).divide(new BigDecimal(100))).setScale(2, BigDecimal.ROUND_HALF_UP);
                swxLoanRecord.setInterest(interest);
                swxLoanRecord.setCurrentInterest(BigDecimal.ZERO);
                //到期时间
                LocalDateTime maturityLocalDate = currentDate.plusDays(old.getTerm());
                swxLoanRecord.setMaturityDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(Date.from(maturityLocalDate.atZone(ZoneId.systemDefault()).toInstant())));
            }
        }
 
        swxLoanRecordService.updateById(swxLoanRecord);
        return Result.ok("编辑成功!");
    }
 
 
}