cshenfu
2024-06-27 58aa6e282bb343503be3cb198948328ea3ed7b79
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
const orderState = require('../../../../../application/service/orderState.js')
const applicationConstant = require('../../../../../application/constant/applicationsConstant.js')
const businessState = require('../../../../../application/service/businessState.js')
const orderPaySuccess = require('../../../../../application/service/orderPaySuccess.js');
module.exports = {
    /**
     * 创建课程订单
     * @url client/order/kh/create/course 前端调用的url参数地址
     * data 请求参数
     * @params {String} params1  参数1
     */
    main: async (event) => {
        let { data = {}, userInfo, util, originalParam } = event;
        let { customUtil, config, pubFun, vk, db, _ } = util;
        let { uid } = data;
        let res = { code: 0, msg: '' };
        // 业务逻辑开始-----------------------------------------------------------
        // 可写与数据库的交互逻辑等等
 
        //检查用户是否已经开通该课程 防止重复下单
        if (await businessState.hasBuyFreeLiveCourses(event, data._id, userInfo)) {
            res.code = -1
            res.msg = "您已订购该课程"
            return res;
        }
 
 
        // todo:先检查是否有未关闭订单 有的话直接返回; 不太好直接查询 后面想下办法
        // let order = await vk.baseDao.findByWhereJson({
        //     dbName: "uni-id-base-order",
        //     whereJson: {
        //         "user_id": uid,
        //         "platform": data.platform,
        //         "status": 1,
        //     }
        // });
        // if (true) {
        //     // res.order_id = order._id
        //     res.order_id = '61020e8fb1e18c00011c1257'
        //     return res
        // }
 
        //新建订单
        let course = await vk.baseDao.findById({
            dbName: "free_live_course",
            id: data._id
        });
 
        //课程不存在
        if (!course) {
            res.code = -1
            res.msg = "无效的课程"
            return res;
        }
 
        //数据准备
        let promoCodeId = ""
 
        let platform = data.platform
 
        let title = course['title']
 
        let total = course.charge
 
        let type = applicationConstant.FERR_LIVE_COURSE
 
        let goods_item = [{
            'oid': null,
            'user_id': uid,
            'goods_id': course['_id'],
            'charge': course['charge'],
            'type': type,
            'num': 1,
            'goods_name': "[公开课]" + course['title'],
            'goods_thumb': course['avatar'],
            'goods_charge': course['charge'],
            'goods_ori_charge': course['charge'],
        }]
 
 
        //统一创建订单
        res = await orderState.createOrder(event, uid, total, title, type, platform, goods_item, promoCodeId)
 
        //如果课程价格为0 则直接发货
        if (total == 0 && res.code == 0) {
            let payOrderInfo = await vk.baseDao.findById({
                dbName: 'uni-id-base-order',
                id: res.order_id
            });
            let obj = {
                data: {
                    ...payOrderInfo,
                    out_trade_no: res.order_id,
                    total_fee: total,
                    status: 1,
                    notify_time: new Date().getTime()
                },
                verifyResult: true
            }
            await orderPaySuccess(obj, { db, _ })
        }
 
        // 业务逻辑结束-----------------------------------------------------------
        return res;
    }
}