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("编辑成功!");
|
}
|
|
|
}
|