unscramble_order.ts
6.6 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as moment from 'moment'
import * as _ from 'lodash'
import { v1 as Uid } from 'uuid';
import { ms_unscramble_order as order, db as info_db, ms_user } from '../table'
export async function createUnscrambleOrder(cramble: any, userid: number) {
let money = cramble.plan_saleprice
const user: any = await ms_user.findById(userid)
if (!user || !user.user_id) throw new Error('用户不存在')
//是否购买过
const isBought = await order.count({ where: { userid } }) > 0
if (isBought && await order.count({ where: { dbno: cramble.dbno, userid } }) > 0)
throw new Error('您已购买过,请勿重复购买')
//首单免费
if (!isBought) money = 0
if (isBought && user.balance < money) throw new Error('余额不够,请充值')
const scrambleOrder = {
userid,
buytime: moment(),
buymoney: money,
username: user.user_name,
..._.pick(cramble, ['dbno', 'typeid', 'expertid', 'author_name', 'plan_lotterytype', 'plan_issueno', 'plan_title', 'plan_summary', 'plan_begintime', 'plan_endtime', 'plan_passtype'])
}
return info_db.sequelize.transaction(async (transaction: any) => {
let res: any = await order.create(scrambleOrder, { transaction })
const orderId = res.id
if (!orderId) throw Error('推荐购买异常')
if (money > 0) {
const updateUser = `UPDATE ms_user SET balance=balance-${money} WHERE user_id=${userid} and balance>=${money}`;
res = await info_db.sequelize.query(updateUser, { transaction, type: info_db.sequelize.QueryTypes.UPDATE });
if (res[1] !== 1) throw Error('余额不够,请充值')
}
const updateUserOperation = `INSERT INTO ms_user_operation (uuid,user_id,created_at,user_name,user_nick_name,user_safe_code,type_name,type_id,money,balance,freeze,memo)SELECT '${Uid()}',user_id,'${moment().format('YYYY-MM-DD HH:mm:ss')}',user_name,nick_name,safe_code,'购买推荐订单',-1,${money},balance,freeze,'购买大神推荐,订单编号:${orderId}' FROM ms_user WHERE user_id=${userid}`;
await info_db.sequelize.query(updateUserOperation, { transaction });
return orderId
});
}
export async function getUnscrambleOrder(args: any) {
return order.findAll({
where: { userid: args.userid },
offset: args.offset,
limit: args.limit,
order: [['buytime', 'desc']]
})
}
// /**
// * 检查用户余额是否足够
// * @param data
// */
// @Dct.api({
// dbNo: Joi.number().required().error(() => '查询条件不能为空!'),
// buyedMoney: Joi.number().integer(),
// chargePayType: Joi.number().integer(),
// userId: Joi.number().integer()
// })
// public static async CheckUserMoney(data) {
// if (!data.userId) {
// return;
// }
// const user = await UserAdapter.getUserById(data.userId);
// if (!user) {
// return;
// }
// //余额不足直接充值
// if (user.Balance < data.buyedMoney) {
// const balance = _.floor(user.Balance);//向下取整数
// const money = (data.buyedMoney - balance).toString();
// return await PaymentAdapter.buildRequestUrl(Unscramble.PayTypeId[config.expert.PayTypeId], money, 'IOS', '', '', data.chargePayType, data.userId);
// }
// //余额不足
// if (data.Balance < data.buyedMoney) throw Error('账户余额不足请充值。');
// const unscramb = await UnscrambleInfo.find({ attributes: ['dbNo', 'plan_salePrice', 'typeId'], where: { dbNo: data.dbNo } });
// if (!unscramb) {
// throw Error('解读信息错误。');
// }
// if (data.buyedMoney !== unscramb.plan_salePrice) {
// throw Error('购买金额错误。');
// }
// if (data.UserStatus > 0 && data.UserStatus <= 50) {
// throw Error('用户已限制消费');
// }
// const cramble = await UnscrambleInfo.find({ where: { dbNo: data.dbNo } });
// if (!cramble) {
// throw Error('解读信息错误');
// }
// const trade = []
// const orderId = await Cache.getUniqId('unscrambleorder')
// const tradeno = `unscrambleorder:${orderId}`;
// const scrambleOrder = {
// Id: orderId,
// dbNo: data.dbNo,
// userId: data.userId,
// typeId: cramble.typeId,
// expertId: cramble.expertId,
// author_name: cramble.author_name,
// buyTime: moment(),
// userName: user.Name,
// buyMoney: data.buyedMoney,
// plan_lotteryType: cramble.plan_lotteryType,
// plan_issueNo: cramble.plan_issueNo,
// plan_title: cramble.plan_title,
// plan_summary: cramble.plan_summary,
// plan_beginTime: cramble.plan_beginTime,
// plan_endTime: cramble.plan_endTime,
// plan_passType: cramble.plan_passType
// };
// trade.push({
// sourceid: orderId,
// userid: user.Id,
// opertypeid: 2611,
// money: -data.buyedMoney,
// memo: `购买大神推荐,订单编号:${orderId}`,
// tradeno: tradeno
// })
// const time = Date.now()
// const msg: any = await sendMsg({
// topic: topicName.Account.AccoutnBackMoney, content: JSON.stringify({
// userid: user.Id,
// sourceid: orderId,
// tradeno: tradeno
// }),
// nextSendTime: moment().add(1, 'm').format('YYYY-MM-DD HH:mm:ss')
// })//退钱消息 60秒后发送
// const result = await AccountAdapter.trade(trade).catch(() => false)
// if (!result) {
// if (Date.now() - time < buyTimeout) Message.Model.update({ nextSendTime: moment().format('YYYY-MM-DD HH:mm:ss') }, { where: { id: msg.id, deletedAt: null } })
// throw new Error('扣款失败')
// }
// await Db.dbs[0].transaction(async (transaction) => {
// await UnscrambleOrder.create(scrambleOrder, { transaction });
// const r = await Message.Model.destroy({ where: { id: msg.id, deletedAt: null }, transaction })
// if (r < 1) throw new Error('下单失败')
// if (Date.now() - time > buyTimeout) throw new Error('购买超时')
// });
// if (unscramb.typeId === 0)//只有购买天天赢球接口的推荐才通知到他们接口
// {
// const timestamp = moment().format("YYYYMMDDHHmmss");
// const sign = await md5Sign(`${orderId}${config.expert.AppSecret}${timestamp}`, 'utf8', 'hex').toLocaleLowerCase();//md5(orderId+key+timestamp)
// const postData = `orderId=${orderId}×tamp=${timestamp}&sign=${sign}`;
// await Unscramble.httpPostRequest(config.expert.postPay, postData);
// }
// return 1;
// }