166 lines
6.0 KiB
JavaScript
166 lines
6.0 KiB
JavaScript
'use strict';
|
|
const dayjs = require("dayjs");
|
|
var isBetween = require('dayjs/plugin/isBetween')
|
|
dayjs.extend(isBetween)
|
|
|
|
module.exports = {
|
|
/**
|
|
* 新增报价
|
|
* @url client/enquiry/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, totalPrice} = data;
|
|
let {company, manager, person, accountType} = userInfo
|
|
let res = {code: 0, msg: ""};
|
|
// 业务逻辑开始-----------------------------------------------------------
|
|
|
|
let noQuoteNo = false
|
|
|
|
if (parseInt(userInfo.userLevel) < 3) {
|
|
if (parseInt(userInfo.remainQuoteNum) === 0 || !userInfo.remainQuoteNum) {
|
|
|
|
const checkSheetQuoteNum = await vk.baseDao.findById({
|
|
dbName: "jjx-old",
|
|
id: data.sheetId
|
|
})
|
|
if (checkSheetQuoteNum.quoteAmount) {
|
|
const checkQuoteNum = await vk.baseDao.findByWhereJson({
|
|
dbName: "jjx-user-quote-num",
|
|
whereJson: {
|
|
uid,
|
|
type: 'old',
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
|
|
let subjectName = ''
|
|
let subjectMobile = ''
|
|
let managerName = ''
|
|
let managerMobile = ''
|
|
|
|
const checkSheet = await vk.baseDao.findById({
|
|
dbName: "jjx-old",
|
|
id: data.sheetId
|
|
})
|
|
|
|
if (checkSheet.status > 0) return {
|
|
code: -1,
|
|
msg: "该旧货交易已关闭"
|
|
}
|
|
|
|
if (accountType === 1 && person.personName && person.mobile) {
|
|
subjectName = person.personName
|
|
subjectMobile = person.mobile
|
|
} else {
|
|
subjectName = company.companyName
|
|
subjectMobile = company.companyMobile
|
|
// if (manager && manager.mobile && manager.personName) {
|
|
// managerName = manager.personName
|
|
// managerMobile = manager.mobile
|
|
// }
|
|
}
|
|
|
|
// 添加报价单
|
|
res.data = await vk.baseDao.add({
|
|
dbName: "jjx-old-quote",// 表名
|
|
dataJson: { // 需要新增的数据 json格式
|
|
...data,
|
|
...{
|
|
userId: data.uid,
|
|
status: 0,
|
|
subjectName,
|
|
subjectMobile,
|
|
managerName,
|
|
managerMobile,
|
|
}
|
|
}
|
|
});
|
|
|
|
const sheet = vk.baseDao.updateAndReturn({
|
|
dbName: "jjx-old",// 表名
|
|
whereJson: {
|
|
_id: data.sheetId,
|
|
},
|
|
dataJson: {
|
|
quoteNum: _.inc(1)
|
|
}
|
|
})
|
|
|
|
const checkMessage = await vk.baseDao.findByWhereJson({
|
|
dbName: "jjx-message-config",
|
|
whereJson: {
|
|
old: true,
|
|
uid: sheet.userId
|
|
}
|
|
})
|
|
|
|
if (checkMessage) {
|
|
let send = true
|
|
// 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}`)))
|
|
// send = false
|
|
// }
|
|
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}`)))
|
|
send = false
|
|
}
|
|
|
|
if (checkMessage.tradeAmount && checkMessage.tradeAmount[0]) {
|
|
if (sheet.totalPrice < checkMessage.tradeAmount[0] || sheet.totalPrice > checkMessage.tradeAmount[1])
|
|
send = false
|
|
}
|
|
|
|
if (send) {
|
|
await vk.baseDao.add({
|
|
dbName: "jjx-message",
|
|
dataJson: {
|
|
uid: sheet.userId,
|
|
title: `旧货:${sheet.title}报价`,
|
|
content: `您的旧货信息有新的报价,赶紧去看看吧`,
|
|
type: 'old',
|
|
hasRead: false,
|
|
pk: data.sheetId
|
|
}
|
|
});
|
|
await pubFun.sendNotifySms(sheet.userId, 'SMS_474185570')
|
|
}
|
|
}
|
|
|
|
// 业务逻辑结束-----------------------------------------------------------
|
|
return res;
|
|
}
|
|
}
|