123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 'use strict';
- const BaseService = require('./base.js');
- // 卡券变动服务类
- class CouponService extends BaseService {
- /**
- * 卡券变化记录
- * @return {Promise<type[]>}
- */
- async addDiningCoinChangeLog(params = {}, transaction = null) {
- const that = this;
- let action_user = '';
- // -1提现失败,0提现成功;1现金券发放;2提现中;3
- // eslint-disable-next-line default-case
- switch (params.type) {
- case 1:
- action_user = '现金券发放';
- break;
- }
- const createBean = await that.app.comoBean.instance({
- user_id: params.user_id,
- order_id: params.order_id,
- type: params.type,
- account: params.account,
- out_batch_no: params.out_batch_no,
- out_detail_no: params.out_detail_no,
- partner_id: params.partner_id,
- action_user,
- log_desc: params.log_desc,
- create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- ori_partner: params.ori_partner,
- customer_id: params.customer_id,
- customer_name: params.customer_name,
- customer_img: params.customer_img,
- }, { transaction });
- const result = await that.create(createBean, that.app.model.DinnerCoinLogs, '记录用户卡券变动失败');
- return result;
- }
- /**
- * 卡券发放到用户账户 并 记录
- * @param user_id
- */
- async dispatchCoupon(user_id = -1) {
- const that = this;
- const transaction = await that.app.model.transaction();
- try {
- let order_id = -1;
- let partner_id = 1;
- let type = 1;
- let account = 300;
- let log_desc = '抽中UIOT现金券';
- // 2023/2/28 查询用户卡券账户列表, 判断添加或更新卡券账户余额
- const couponInfo = await that.app.model.DinnerCoins.findOne({
- where: {
- user_id,
- // expired: false,
- }, transaction, raw: true,
- });
- // 2024/9/13 没有UIOT现金券的用户自动发放
- if (!couponInfo) {
- // 2022/11/18 记录卡券发放记录
- await that.addDiningCoinChangeLog({
- user_id, order_id, partner_id, type, account, log_desc,
- }, transaction);
- // 2023/2/27 没有对应类型的coupon 则插入该类型的coupon账户
- const data = {
- user_id, account, ori_partner: false, partner_id,
- create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
- expired: false,
- expired_time: that.app.szjcomo.date('Y-m-d H:i:s', parseInt(+new Date() + '') / 1000 + 999 * 24 * 60 * 60),
- };
- // 2024/9/13 查询卡券对应商家信息
- const res = await that.app.model.PartnerInfo.findOne({
- where: { id: 1 },// 2024/9/13 1.源森家具
- transaction, raw: true,
- });
- data.partner_name = res.name;
- data.partner_address = res.address;
- data.partner_tel = res.tel_num;
- data.partner_opening_time = res.opening_time;
- const createBean = await that.app.comoBean.instance(data, { transaction });
- await that.service.base.create(createBean, that.app.model.DinnerCoins, '卡券发放失败,请重试');
- }
- await transaction.commit();
- } catch (e) {
- if (transaction) await transaction.rollback();
- throw new Error(e.toString());
- }
- }
- }
- module.exports = CouponService;
|