Files
jjx-wap/uniCloud-tcb/cloudfunctions/router/service/client/process/quote/kh/addQuote.js
T

158 lines
5.7 KiB
JavaScript

'use strict';
const dayjs = require("dayjs");
var isBetween = require('dayjs/plugin/isBetween')
dayjs.extend(isBetween)
module.exports = {
/**
* 新增报价
* @url client/process/quote/kh/addQuote 前端调用的url参数地址
* data 请求参数
* @param {String} params1 参数1
*/
main: async (event) => {
let {data = {}, userInfo, util, filterResponse, originalParam} = event;
let {customUtil, uniID, config, pubFun, vk, db, _} = util;
let {uid} = data;
let res = {code: 0, msg: ""};
// 业务逻辑开始-----------------------------------------------------------
if (parseInt(userInfo.factoryStatus) !== 1) return {
code: 1,
msg: "非加工厂,无法报价,请去个人中心申请加工厂"
}
let noQuoteNo = false
if (parseInt(userInfo.userLevel) < 3) {
if (parseInt(userInfo.remainQuoteNum) === 0 || !userInfo.remainQuoteNum) {
const checkSheetQuoteNum = await vk.baseDao.findById({
dbName: "jjx-process-sheet",
id: data.sheetId
})
if (checkSheetQuoteNum.quoteAmount) {
const checkQuoteNum = await vk.baseDao.findByWhereJson({
dbName: "jjx-user-quote-num",
whereJson: {
uid,
type: 'process',
sheetId: data.sheetId,
}
});
if (!checkQuoteNum || checkQuoteNum.num === 0) noQuoteNo = true
} else noQuoteNo = true
}
noQuoteNo = !!(userInfo.quoteExpiredAt && dayjs(userInfo.quoteExpiredAt).isBefore(dayjs()));
}
if (noQuoteNo) {
return {
code: 1,
msg: "您的报价次数已过期/用完"
}
}
if (userInfo.remainQuoteNum && userInfo.remainQuoteNum > 0) {
await vk.baseDao.update({
dbName: "uni-id-users",
whereJson: {
uid
},
dataJson: {
remainQuoteNum: _.inc(-1)
}
})
}
const checkSheet = await vk.baseDao.findById({
dbName: "jjx-process-sheet",
id: data.sheetId
})
if (checkSheet.status > 0) return {
code: 11,
msg: "该加工单已关闭"
}
if (checkSheet.earnestMoney && checkSheet.earnestMoney > 0 && checkSheet.earnestMoney > userInfo.paidEnsure) {
return {
code: 12,
msg: "您的保证金不足,请先缴纳保证金"
}
}
const dbName = 'jjx-process-quote'
await vk.baseDao.add({
dbName,// 表名
dataJson: { // 需要新增的数据 json格式
...data,
...{
userId: data.uid,
status: 0
}
}
});
await vk.baseDao.update({
dbName: "jjx-process-sheet",// 表名
whereJson: {
_id: data.sheetId,
},
dataJson: {
quoteNum: _.inc(1)
}
})
const sheet = await vk.baseDao.findById({
dbName: "jjx-process-sheet",// 表名
id: data.sheetId
});
const checkMessage = await vk.baseDao.findByWhereJson({
dbName: "jjx-message-config",
whereJson: {
process: true,
uid: sheet.userId
}
})
if (checkMessage) {
// if (checkMessage.doNotDisturbRange && checkMessage.doNotDisturbRange[0]) {
// const date = dayjs().format("YYYY-MM-DD")
// const doNotDisturbRangeStart = dayjs(checkMessage.doNotDisturbRange[0]).format("HH:mm")
// const doNotDisturbRangeEnd = dayjs(checkMessage.doNotDisturbRange[1]).format("HH:mm")
// if (dayjs().isBetween(dayjs(`${date} ${doNotDisturbRangeStart}`), dayjs(`${date} ${doNotDisturbRangeEnd}`)))
// return res
// }
const date = dayjs().format("YYYY-MM-DD")
if (checkMessage.doNotDisturbStart && checkMessage.doNotDisturbStart.length) {
const doNotDisturbRangeStart = dayjs(`${date} ${checkMessage.doNotDisturbStart[0]}:${checkMessage.doNotDisturbStart[1]}`).format("HH:mm")
const doNotDisturbRangeEnd = dayjs(`${date} ${checkMessage.doNotDisturbEnd[0]}:${checkMessage.doNotDisturbEnd[1]}`).format("HH:mm")
if (dayjs().isBetween(dayjs(`${date} ${doNotDisturbRangeStart}`), dayjs(`${date} ${doNotDisturbRangeEnd}`)))
return res
}
if (checkMessage.tradeAmount && checkMessage.tradeAmount[0]) {
if (sheet.totalPrice < checkMessage.tradeAmount[0] || sheet.totalPrice > checkMessage.tradeAmount[1])
return res
}
await vk.baseDao.add({
dbName: "jjx-message",
dataJson: {
uid: sheet.userId,
title: `加工:${sheet.title}报价`,
content: `您的加工信息有新的报价,赶紧去看看吧`,
type: 'process',
hasRead: false,
pk: data.sheetId
}
});
await pubFun.sendNotifySms(sheet.userId, 'SMS_474165512')
}
// 业务逻辑结束-----------------------------------------------------------
return res;
}
}