129 lines
6.2 KiB
JavaScript
129 lines
6.2 KiB
JavaScript
// 授信管理(贷款申请)路由:list/detail/personal-loan-application(create/update)/
|
|
// enterprise-loan-application(create/update)。字段严格对齐 db/seedCredit.js 及前端
|
|
// PersonalLoanForm.vue / EnterpriseLoanForm.vue 实际提交结构。
|
|
import express from 'express'
|
|
import { loanApplications } from '../db/seedCredit.js'
|
|
import { nextId } from '../utils/id.js'
|
|
import { paginate } from '../utils/pagination.js'
|
|
import { sendOk, sendFail } from '../utils/response.js'
|
|
import { requireAuth } from '../middleware/requireAuth.js'
|
|
|
|
const router = express.Router()
|
|
router.use(requireAuth)
|
|
|
|
function nowStr() {
|
|
return new Date().toISOString().slice(0, 19).replace('T', ' ')
|
|
}
|
|
|
|
function genLoanApplicationIdentity() {
|
|
const id = nextId('loanApplication')
|
|
return { id, loanApplicationId: `LA${String(id).padStart(8, '0')}` }
|
|
}
|
|
|
|
// ---------------- 贷款申请列表/详情 ----------------
|
|
router.post('/loan-application/list', (req, res) => {
|
|
const { loanApplicationId, customerName, applicationType, applicationStatus, createDateStart, createDateEnd, page, pageSize } =
|
|
req.body || {}
|
|
let list = loanApplications
|
|
if (loanApplicationId) list = list.filter((a) => a.loanApplicationId.includes(loanApplicationId))
|
|
if (customerName) list = list.filter((a) => a.customerName && a.customerName.includes(customerName))
|
|
if (applicationType) list = list.filter((a) => a.applicationType === applicationType)
|
|
if (applicationStatus) list = list.filter((a) => a.applicationStatus === applicationStatus)
|
|
if (createDateStart) list = list.filter((a) => a.createTime >= createDateStart)
|
|
if (createDateEnd) list = list.filter((a) => a.createTime <= createDateEnd)
|
|
list = [...list].sort((a, b) => (a.createTime < b.createTime ? 1 : -1))
|
|
sendOk(res, paginate(list, page, pageSize))
|
|
})
|
|
|
|
router.post('/loan-application/detail', (req, res) => {
|
|
const { loanApplicationId } = req.body || {}
|
|
const app = loanApplications.find((a) => a.loanApplicationId === loanApplicationId)
|
|
if (!app) return sendFail(res, '贷款申请单不存在', 404)
|
|
sendOk(res, app)
|
|
})
|
|
|
|
// ---------------- 个人贷款申请 ----------------
|
|
router.post('/personal-loan-application/create', (req, res) => {
|
|
const body = req.body || {}
|
|
if (!body.channel) return sendFail(res, '所属渠道不能为空', 400)
|
|
if (!body.loanProductCode) return sendFail(res, '贷款产品编号不能为空', 400)
|
|
if (!body.goodsCategory) return sendFail(res, '商品类目不能为空', 400)
|
|
if (!body.paymentCycle) return sendFail(res, '回款周期不能为空', 400)
|
|
if (!body.singleOrderLimit) return sendFail(res, '单笔汇总订单限额不能为空', 400)
|
|
if (body.financingRatio === undefined || body.financingRatio === null) return sendFail(res, '融资比例不能为空', 400)
|
|
if (!body.loanTerm) return sendFail(res, '借款期限不能为空', 400)
|
|
if (!body.orderFinancingTerm) return sendFail(res, '订单融资期限不能为空', 400)
|
|
if (!body.loanAmount) return sendFail(res, '申请金额不能为空', 400)
|
|
if (!body.businessScope) return sendFail(res, '经营范围不能为空', 400)
|
|
if (!body.emergencyContactName) return sendFail(res, '紧急联系人姓名不能为空', 400)
|
|
|
|
const { id, loanApplicationId } = genLoanApplicationIdentity()
|
|
const now = nowStr()
|
|
const entity = {
|
|
...body,
|
|
id,
|
|
loanApplicationId,
|
|
applicationType: 'PERSONAL',
|
|
applicationStatus: 'DRAFT',
|
|
createdBy: 'admin',
|
|
createTime: now,
|
|
updateTime: now
|
|
}
|
|
loanApplications.push(entity)
|
|
sendOk(res, loanApplicationId, '贷款申请草稿已提交')
|
|
})
|
|
|
|
router.post('/personal-loan-application/update', (req, res) => {
|
|
const { loanApplicationId, ...rest } = req.body || {}
|
|
const app = loanApplications.find((a) => a.loanApplicationId === loanApplicationId)
|
|
if (!app) return sendFail(res, '贷款申请单不存在', 404)
|
|
if (app.applicationStatus !== 'DRAFT') return sendFail(res, '仅草稿状态的贷款申请可修改', 400)
|
|
Object.assign(app, rest, { updateTime: nowStr() })
|
|
sendOk(res, true, '修改成功')
|
|
})
|
|
|
|
// ---------------- 企业贷款申请 ----------------
|
|
router.post('/enterprise-loan-application/create', (req, res) => {
|
|
const body = req.body || {}
|
|
if (!body.channel) return sendFail(res, '所属渠道不能为空', 400)
|
|
if (!body.loanProductCode) return sendFail(res, '贷款产品编号不能为空', 400)
|
|
if (!body.enterpriseName) return sendFail(res, '企业客户不能为空', 400)
|
|
if (!body.goodsCategory) return sendFail(res, '商品类目不能为空', 400)
|
|
if (!body.paymentCycle) return sendFail(res, '回款周期不能为空', 400)
|
|
if (!body.singleOrderLimit) return sendFail(res, '单笔汇总订单限额不能为空', 400)
|
|
if (body.financingRatio === undefined || body.financingRatio === null) return sendFail(res, '融资比例不能为空', 400)
|
|
if (!body.loanTerm) return sendFail(res, '借款期限不能为空', 400)
|
|
if (!body.orderFinancingTerm) return sendFail(res, '订单融资期限不能为空', 400)
|
|
if (!body.loanAmount) return sendFail(res, '申请金额不能为空', 400)
|
|
if (!body.businessScope) return sendFail(res, '经营范围不能为空', 400)
|
|
if (!body.legalPersonName) return sendFail(res, '法人姓名不能为空', 400)
|
|
if (!body.legalPersonCertId) return sendFail(res, '法人证件号码不能为空', 400)
|
|
if (!body.emergencyContactName) return sendFail(res, '紧急联系人姓名不能为空', 400)
|
|
|
|
const { id, loanApplicationId } = genLoanApplicationIdentity()
|
|
const now = nowStr()
|
|
const entity = {
|
|
...body,
|
|
id,
|
|
loanApplicationId,
|
|
applicationType: 'BUSINESS',
|
|
applicationStatus: 'DRAFT',
|
|
createdBy: 'admin',
|
|
createTime: now,
|
|
updateTime: now
|
|
}
|
|
loanApplications.push(entity)
|
|
sendOk(res, { loanApplicationId }, '贷款申请草稿已提交')
|
|
})
|
|
|
|
router.post('/enterprise-loan-application/update', (req, res) => {
|
|
const { loanApplicationId, ...rest } = req.body || {}
|
|
const app = loanApplications.find((a) => a.loanApplicationId === loanApplicationId)
|
|
if (!app) return sendFail(res, '贷款申请单不存在', 404)
|
|
if (app.applicationStatus !== 'DRAFT') return sendFail(res, '仅草稿状态的贷款申请可修改', 400)
|
|
Object.assign(app, rest, { updateTime: nowStr() })
|
|
sendOk(res, true, '修改成功')
|
|
})
|
|
|
|
export default router
|