Commit 3609b20c authored by 王波's avatar 王波 Committed by qinhaitao

优化修改

parent d30d2d07
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { services, preCheck, registeInfos, preUpdate, checkParams } from '../decorator/common' import { services, preCheck, registeInfos, preUpdate, checkParams } from '../decorator/common'
import { resultsModel } from '../sdk' import { resultsModel } from '../sdk'
import { CommonUserService, CommonStatService, CommonTaskService, CommonGrowTaskService } from '../service/common' import { CommonStatService, CommonGrowTaskService } from '../service/common'
import { STAT_TYPE } from '../constants' import { STAT_TYPE } from '../constants'
import { checkActivityTime, checkInviteId, checkHelpRecord, checkNewVip } from '../utils/common/check' import { checkActivityTime, checkInviteId, checkHelpRecord, checkNewVip } from '../utils/common/check'
import { updateHelpRecord, updateVip } from '../utils/common/update' import { updateHelpRecord, updateVip } from '../utils/common/update'
......
...@@ -25,11 +25,11 @@ export default class Task { ...@@ -25,11 +25,11 @@ export default class Task {
@preUpdate([updateVip, updateSignGrowTask, updateGrowOrderGoods]) @preUpdate([updateVip, updateSignGrowTask, updateGrowOrderGoods])
async getTaskList( async getTaskList(
context: IContext<IParams>, context: IContext<IParams>,
{ userInfo, activityInfo }: IControllerInfos, { userInfo, activityInfo, task }: IControllerInfos,
[growTaskService]: [CommonGrowTaskService] [growTaskService]: [CommonGrowTaskService]
): Promise<IResult<{ list: ITaskInfo[] }>> { ): Promise<IResult<{ list: ITaskInfo[] }>> {
// 读取B端 tasks字段配置自动生成任务列表 // 读取B端 tasks字段配置自动生成任务列表
const result = await growTaskService.initTaskList(userInfo, activityInfo) const result = await growTaskService.initTaskList(userInfo, activityInfo, task)
return resultsModel.success({ return resultsModel.success({
...result ...result
......
...@@ -22,3 +22,4 @@ export const PRIZE_CONFIG_DB_NAME = 'b_prize_config' ...@@ -22,3 +22,4 @@ export const PRIZE_CONFIG_DB_NAME = 'b_prize_config'
export const ACTIVITY_CONFIG_DB_NAME = 'b_activity_config' export const ACTIVITY_CONFIG_DB_NAME = 'b_activity_config'
export const OEDER_DB_NAME = 'c_user_order'
...@@ -7,7 +7,13 @@ ...@@ -7,7 +7,13 @@
import UserService from './user.service' import UserService from './user.service'
import { getToday, getUserOrderlist, generateVipUrl, formatVipCbUrl, setNewFollowUserData } from '../../utils' import { getToday, getUserOrderlist, generateVipUrl, formatVipCbUrl, setNewFollowUserData } from '../../utils'
import { TASK_RATE_TYPE, TASK_STATUS } from '../../constants' import { TASK_RATE_TYPE, TASK_STATUS } from '../../constants'
import { getTodayCompleteTask, getTotalCompleteTask, setTaskStatus, updateTaskInfo } from '../../utils/common/task' import {
getTotalCompleteTask,
setGrowTaskStatus,
updateTaskInfo,
getTodayCompleteGrowTask,
getOrderCount
} from '../../utils/common/task'
import { CODE_TYPES } from '../../errorCode' import { CODE_TYPES } from '../../errorCode'
export interface ITaskInfo { export interface ITaskInfo {
...@@ -29,11 +35,16 @@ export default class TaskService extends UserService { ...@@ -29,11 +35,16 @@ export default class TaskService extends UserService {
} }
// 根据活动tasks字段渲染任务 // 根据活动tasks字段渲染任务
async initTaskList(userInfo: IUserInfo, activityInfo: IActivityInfo) { async initTaskList(userInfo: IUserInfo, activityInfo: IActivityInfo, task: ITask) {
let list = []
const keyList = Object.keys(activityInfo?.tasks || {})
for (let index = 0; index < keyList.length; index++) {
// @ts-ignore
const taskType: ITaskType = keyList[index]
list.push(await this.initTask(task, taskType, activityInfo?.tasks?.[taskType]?.title, userInfo, activityInfo))
}
return { return {
list: Object.keys(activityInfo?.tasks || {}).map((task: ITaskType) => { list
return this.initTask(task, activityInfo?.tasks?.[task]?.title, userInfo, activityInfo)
})
} }
} }
/** /**
...@@ -46,14 +57,29 @@ export default class TaskService extends UserService { ...@@ -46,14 +57,29 @@ export default class TaskService extends UserService {
* @return {ITaskInfo} {ITaskInfo} 任务信息 * @return {ITaskInfo} {ITaskInfo} 任务信息
* @memberof TaskService * @memberof TaskService
*/ */
initTask(taskType: ITaskType, title: string, userInfo: IUserInfo, activityInfo: IActivityInfo): ITaskInfo { async initTask(
task: ITask,
taskType: ITaskType,
title: string,
userInfo: IUserInfo,
activityInfo: IActivityInfo
): Promise<ITaskInfo> {
if (!activityInfo?.tasks?.[taskType]) { if (!activityInfo?.tasks?.[taskType]) {
console.error(`活动缺少${taskType}任务配置项`) console.error(`活动缺少${taskType}任务配置项`)
} }
const { value, itemIds = '', taskRateType, times = 1, link = '' } = activityInfo?.tasks?.[taskType] || {} const { value, itemIds = '', taskRateType, times = 1, link = '' } = activityInfo?.tasks?.[taskType] || {}
const { remainTimes } = userInfo const { remainTimes } = userInfo
const { todayCompleteTimes } = getTodayCompleteTask(taskType, userInfo) let { todayCompleteTimes } = getTodayCompleteGrowTask(taskType, task)
const { completeTimes } = getTotalCompleteTask(taskType, userInfo) let { completeTimes } = getTotalCompleteTask(taskType, userInfo)
if (taskType === 'orderGoods') {
//如果是下单任务,需要单独去根据订单类型去订单表获取下单列表
todayCompleteTimes = completeTimes = await getOrderCount(
this.context,
userInfo.openId,
activityInfo._id,
taskRateType
)
}
return { return {
taskType, taskType,
...@@ -65,7 +91,7 @@ export default class TaskService extends UserService { ...@@ -65,7 +91,7 @@ export default class TaskService extends UserService {
url: taskType === 'member' ? generateVipUrl(formatVipCbUrl(this.context)) : link, url: taskType === 'member' ? generateVipUrl(formatVipCbUrl(this.context)) : link,
todayCompleteTimes, todayCompleteTimes,
completeTimes, completeTimes,
status: setTaskStatus(userInfo, taskType, taskRateType, times), status: setGrowTaskStatus(userInfo, taskType, taskRateType, times, todayCompleteTimes, completeTimes),
waitReceive: remainTimes?.[taskType] waitReceive: remainTimes?.[taskType]
} }
} }
......
...@@ -37,7 +37,7 @@ export default class TaskService extends UserService { ...@@ -37,7 +37,7 @@ export default class TaskService extends UserService {
} }
} }
/** /**
* 根据任务类型初始化 b端 tasks字段必须有相应的配置 * 根据任务类型初始化 b端 tasks字段必须有相应的配置TODO
* *
* @param {ITaskType} taskType 任务类型 * @param {ITaskType} taskType 任务类型
* @param {string} title 任务标题 * @param {string} title 任务标题
......
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
type IAwards = ICommonAwards & ICustomAwards type IAwards = ICommonAwards & ICustomAwards
interface ICustomAwards { interface ICustomAwards {}
}
interface ICommonAwards { interface ICommonAwards {
_id?: string _id?: string
id?: string id?: string
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { getToday } from './getToday' import { getToday } from './getToday'
import { TASK_STATUS, TASK_RATE_TYPE } from '../../constants' import { TASK_STATUS, TASK_RATE_TYPE } from '../../constants'
import { TASK_DB_NAME } from '../../db' import { TASK_DB_NAME, OEDER_DB_NAME } from '../../db'
import { BaseDao } from '../../sdk' import { BaseDao } from '../../sdk'
import Task from '../../controller/task.controller' import Task from '../../controller/task.controller'
...@@ -176,21 +176,63 @@ export async function updateTaskInfo(context: IContext<IParams>, _id: string, ta ...@@ -176,21 +176,63 @@ export async function updateTaskInfo(context: IContext<IParams>, _id: string, ta
} }
/** /**
* 增加每日任务每日任务,适用于普通每日任务 * 更新每日任务,适用于购买商品每日任务
* @param context * @param context
* @param activityId * @param activityId
*/ */
export async function updateGoodsTaskInfo(context: IContext<IParams>, _id: string, taskType: string, value) { export async function getOrderCount(
let taskInfodao: IBaseDao = new BaseDao(context, TASK_DB_NAME) context: IContext<IParams>,
const today = getToday() openId: string,
return await taskInfodao.update( activityId: string,
{ taskRateType: number
_id ) {
}, let orderdao: IBaseDao = new BaseDao(context, OEDER_DB_NAME)
{ const project =
$set: { taskRateType === TASK_RATE_TYPE.EVERYDAY
[`taskInfo.${today}.${taskType}`]: value ? {
} openId,
} activityId,
) createDay: getToday()
}
: {
openId,
activityId
}
return await orderdao.count(project)
}
/**
*
* 根据用户和任务完成情况,设置任务状态
*
* @param {IUserInfo} useInfo
* @param {string} taskType
* @param {number} taskRateType
* @param {number} [limitTimesEverday] 每天限制次数 任务频率为每天的时候必填
* @return {taskTatus} 1未完成 2 待领取 3 已完成
* @memberof TaskService
*/
export function setGrowTaskStatus(
userInfo: IUserInfo,
taskType: string,
taskRateType: number,
limitTimesEveryday: number = 1,
todayCompleteTimes: number,
completeTimes: number
): number {
const waitReceive = userInfo?.remainTimes?.[taskType] || 0
if (waitReceive) return TASK_STATUS.WAIT_RECEIVE
switch (taskRateType) {
case TASK_RATE_TYPE.FOREVER:
return completeTimes >= limitTimesEveryday ? TASK_STATUS.DONE : TASK_STATUS.WAIT_DO
break
case TASK_RATE_TYPE.EVERYDAY:
return todayCompleteTimes >= limitTimesEveryday ? TASK_STATUS.DONE : TASK_STATUS.WAIT_DO
break
case TASK_RATE_TYPE.NOLIMIT:
return TASK_STATUS.WAIT_DO
break
}
} }
...@@ -7,8 +7,6 @@ import updateUserInfo, { reduceGameTimes, updateHelpRecord } from './updateUserI ...@@ -7,8 +7,6 @@ import updateUserInfo, { reduceGameTimes, updateHelpRecord } from './updateUserI
import updateSignGrowTask from './updateSignGrowTask' import updateSignGrowTask from './updateSignGrowTask'
import updateGrowOrderGoods from './updateGrowOrderGoods' import updateGrowOrderGoods from './updateGrowOrderGoods'
const update = { const update = {
updateVip, updateVip,
updateSignTask, updateSignTask,
......
/** @format */ /** @format */
import { getToday } from '../getToday' import { getToday } from '../getToday'
import { getTotalCompleteTask, getTodayCompleteGrowTask, updateGoodsTaskInfo } from '../task'
import { TASK_RATE_TYPE } from '../../../constants' import { TASK_RATE_TYPE } from '../../../constants'
import { getUserOrderlist } from '../getUserOrderlist' import { getUserOrderlist } from '../getUserOrderlist'
import { OEDER_DB_NAME } from '../../../db'
import { resultsModel } from '../../../sdk' import { resultsModel } from '../../../sdk'
import { BaseDao } from '../../../sdk'
import { CODE_TYPES } from '../../../errorCode' import { CODE_TYPES } from '../../../errorCode'
import { formatDate } from '../date'
export default async function updateOrderGoods( export default async function updateGrowOrderGoods(
context: IContext<IParams>, context: IContext<IParams>,
{ userInfo, activityInfo, task }: IControllerInfos { userInfo, activityInfo }: IControllerInfos
): Promise<IPreUpdateQuery> { ): Promise<IPreUpdateQuery> {
const taskType = 'orderGoods' const taskType = 'orderGoods'
const { value, itemIds, taskRateType, times } = activityInfo?.tasks?.[taskType] || {} const { value, itemIds, taskRateType, times } = activityInfo?.tasks?.[taskType] || {}
const { completeTimes, taskInfo } = getTotalCompleteTask(taskType, userInfo) let orderdao: IBaseDao = new BaseDao(context, OEDER_DB_NAME)
const { todayCompleteTimes } = getTodayCompleteGrowTask(taskType, task) const orderList =
taskRateType === TASK_RATE_TYPE.EVERYDAY
? await orderdao.find({
openId: userInfo.openId,
activityId: userInfo.activityId,
createDay: getToday
})
: await orderdao.find({
openId: userInfo.openId,
activityId: userInfo.activityId
})
const completeTimes = orderList?.length || 0
if (!activityInfo?.tasks?.[taskType]) return {} if (!activityInfo?.tasks?.[taskType]) return {}
// 永久任务且已完成 // 每日任务或者是永久任务的任务且已完成
if (taskRateType === TASK_RATE_TYPE.FOREVER && completeTimes >= times) { if (taskRateType !== TASK_RATE_TYPE.NOLIMIT && completeTimes >= times) {
return {} return {}
} }
// 每日限制完成且完成次数达到限制
if (taskRateType === TASK_RATE_TYPE.EVERYDAY && todayCompleteTimes >= times) {
return {}
}
return taskRateType === TASK_RATE_TYPE.EVERYDAY ?
await getEverydayGoods(context, taskType, itemIds, task, times, value, userInfo)
: await getOrderGoods(taskType, itemIds, times, value, userInfo, taskInfo)
}
/**
* 更新永久订单信息
* @param taskType
* @param itemIds
* @param times
* @param value
* @param userInfo
*/
async function getOrderGoods(taskType: string, itemIds: string, times: number, value: number, userInfo: IUserInfo, taskInfo: ITaskDetail[]): Promise<IPreUpdateQuery> {
const today = getToday() const today = getToday()
const orderResult = await getUserOrderlist( //设置查询淘宝订单时间
this.context, let startTime = Date.now()
//@ts-ignore if (taskRateType === TASK_RATE_TYPE.EVERYDAY) {
userInfo.createTime || activityInfo?.startTime || Date.now(), let TodayTime = formatDate(today).getTimestamp()
Date.now() startTime = TodayTime > userInfo.createTime ? TodayTime : userInfo.createTime
) } else {
startTime = userInfo.createTime || activityInfo?.startTime || Date.now()
const itemIdsArr = itemIds.split(',').map(v => +v)
let projection = {
$inc: {
[`remainTimes.${taskType}`]: 0
},
$set: {}
} }
let targetOrders = userInfo?.taskInfo?.[today]?.[taskType] || []
orderResult.forEach(v => {
// @ts-ignore
// 商品订单包含目标商品 且orderId为新订单
if (itemIdsArr.includes(v.itemId) && !taskInfo.some(order => order.orderId === v.orderId)) {
if (targetOrders.length >= times) {
return
}
projection.$inc[`remainTimes.${taskType}`] += +value
targetOrders.push({
itemId: v.itemId,
orderId: v.orderId,
payTime: v.payTime,
tId: v.tId,
createTime: Date.now()
})
}
if (targetOrders?.length) {
projection.$set[`taskInfo.${today}.${taskType}`] = targetOrders
}
})
return projection
}
/**
* 更新每日订单信息,仅适用于下单,如果是每日确认收货,需要单独处理
* @param context
* @param taskType
* @param itemIds
* @param task
* @param times
* @param value
* @param userInfo
*/
async function getEverydayGoods(context: IContext<IParams>, taskType: string, itemIds: string, task: ITask, times: number, value: number, userInfo: IUserInfo): Promise<IPreUpdateQuery> {
const today = getToday()
const TodayTime = new Date(today).getTime()
let startTime = TodayTime > userInfo.createTime ? TodayTime : userInfo.createTime
const orderResult = await getUserOrderlist( const orderResult = await getUserOrderlist(
context, context,
//@ts-ignore //@ts-ignore
...@@ -113,33 +59,34 @@ async function getEverydayGoods(context: IContext<IParams>, taskType: string, it ...@@ -113,33 +59,34 @@ async function getEverydayGoods(context: IContext<IParams>, taskType: string, it
}, },
$set: {} $set: {}
} }
//筛选符合任务的订单并添加到下单表
let targetOrders = task?.taskInfo?.[today]?.[taskType] || [] let targetOrders = []
let targetLength = targetOrders.length
orderResult.forEach(v => { orderResult.forEach(v => {
// @ts-ignore // @ts-ignore
// 商品订单包含目标商品 且orderId为新订单 // 商品订单包含目标商品 且orderId为新订单
if (itemIdsArr.includes(v.itemId) && !task?.taskInfo.some(order => order.orderId === v.orderId)) { if (itemIdsArr.includes(v.itemId) && !orderList.some(order => order.orderId === v.orderId)) {
if (targetOrders.length >= times) { if (targetOrders.length + completeTimes >= times) {
return return
} }
projection.$inc[`remainTimes.${taskType}`] += +value
targetOrders.push({ targetOrders.push({
openId: userInfo.openId,
activityId: activityInfo._id,
itemId: v.itemId, itemId: v.itemId,
orderId: v.orderId, orderId: v.orderId,
payTime: v.payTime, payTime: v.payTime,
tId: v.tId, tId: v.tId,
createTime: Date.now() createTime: Date.now(),
createDay: today
}) })
} }
}) })
if (targetOrders?.length && targetLength !== targetOrders?.length) { if (targetOrders?.length) {
const result = await updateGoodsTaskInfo(context, today, taskType, targetOrders) const result = await orderdao.insertMany(targetOrders)
if (result !== 1) { if (!result || result.length === 0) {
// @ts-ignore // @ts-ignore
return resultsModel.error(CODE_TYPES.SYSTEM_ERROR, '订单任务更新失败') return resultsModel.error(CODE_TYPES.SYSTEM_ERROR, '订单任务更新失败')
} }
projection.$inc[`remainTimes.${taskType}`] = result.length * value
} }
return projection return projection
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment