cshenfu
2024-05-21 09d8c671d5e6e6045677bfb5218a5fb78c26aa0f
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
108
109
110
111
112
'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 (data.pass) {
            let info = await vk.baseDao.findByWhereJson({
                dbName: "user_goods",
                whereJson: {
                    user_id: uid,
                    goods_id: data._id,
                }
            });
            if (info != null) {
                await vk.baseDao.deleteById({
                    dbName: "user_goods",
                    id: info._id
                })
            }
        } else {
            //检查用户是否已经开通该课程 防止重复下单
            if (await businessState.hasBuyCourses(event, data._id, userInfo)) {
                res.code = -1
                res.msg = "您已订购该课程"
                return res;
            }
        }
 
        //新建订单
        let course = await vk.baseDao.findById({
            dbName: "course",
            id: data._id,
            whereJson: {
                is_show: 1,
                published_at: _.lte(pubFun.getNowTimeStr())
            }
        });
 
        //课程不存在
        if (!course) {
            res.code = -1
            res.msg = "无效的课程"
            return res;
        }
 
        //数据准备
        let promoCodeId = ""
 
        let title = course['title']
 
        let platform = data.platform
 
        let total = course.charge
 
        let type = applicationConstant.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['thumb'],
            'goods_charge': course['charge'],
            'goods_ori_charge': course['charge'],
            'address_info': data.address
        }]
 
        //统一创建订单
        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;
    }
}