coupon.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. const BaseService = require('./base.js');
  3. // 卡券变动服务类
  4. class CouponService extends BaseService {
  5. /**
  6. * 卡券变化记录
  7. * @return {Promise<type[]>}
  8. */
  9. async addDiningCoinChangeLog(params = {}, transaction = null) {
  10. const that = this;
  11. let action_user = '';
  12. // -1提现失败,0提现成功;1现金券发放;2提现中;3
  13. // eslint-disable-next-line default-case
  14. switch (params.type) {
  15. case 1:
  16. action_user = '现金券发放';
  17. break;
  18. }
  19. const createBean = await that.app.comoBean.instance({
  20. user_id: params.user_id,
  21. order_id: params.order_id,
  22. type: params.type,
  23. account: params.account,
  24. out_batch_no: params.out_batch_no,
  25. out_detail_no: params.out_detail_no,
  26. partner_id: params.partner_id,
  27. action_user,
  28. log_desc: params.log_desc,
  29. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  30. ori_partner: params.ori_partner,
  31. customer_id: params.customer_id,
  32. customer_name: params.customer_name,
  33. customer_img: params.customer_img,
  34. }, { transaction });
  35. const result = await that.create(createBean, that.app.model.DinnerCoinLogs, '记录用户卡券变动失败');
  36. return result;
  37. }
  38. /**
  39. * 卡券发放到用户账户 并 记录
  40. * @param user_id
  41. */
  42. async dispatchCoupon(user_id = -1) {
  43. const that = this;
  44. const transaction = await that.app.model.transaction();
  45. try {
  46. let order_id = -1;
  47. let partner_id = 1;
  48. let type = 1;
  49. let account = 300;
  50. let log_desc = '抽中UIOT现金券';
  51. // 2023/2/28 查询用户卡券账户列表, 判断添加或更新卡券账户余额
  52. const couponInfo = await that.app.model.DinnerCoins.findOne({
  53. where: {
  54. user_id,
  55. // expired: false,
  56. }, transaction, raw: true,
  57. });
  58. // 2024/9/13 没有UIOT现金券的用户自动发放
  59. if (!couponInfo) {
  60. // 2022/11/18 记录卡券发放记录
  61. await that.addDiningCoinChangeLog({
  62. user_id, order_id, partner_id, type, account, log_desc,
  63. }, transaction);
  64. // 2023/2/27 没有对应类型的coupon 则插入该类型的coupon账户
  65. const data = {
  66. user_id, account, ori_partner: false, partner_id,
  67. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  68. expired: false,
  69. expired_time: that.app.szjcomo.date('Y-m-d H:i:s', parseInt(+new Date() + '') / 1000 + 999 * 24 * 60 * 60),
  70. };
  71. // 2024/9/13 查询卡券对应商家信息
  72. const res = await that.app.model.PartnerInfo.findOne({
  73. where: { id: 1 },// 2024/9/13 1.源森家具
  74. transaction, raw: true,
  75. });
  76. data.partner_name = res.name;
  77. data.partner_address = res.address;
  78. data.partner_tel = res.tel_num;
  79. data.partner_opening_time = res.opening_time;
  80. const createBean = await that.app.comoBean.instance(data, { transaction });
  81. await that.service.base.create(createBean, that.app.model.DinnerCoins, '卡券发放失败,请重试');
  82. }
  83. await transaction.commit();
  84. } catch (e) {
  85. if (transaction) await transaction.rollback();
  86. throw new Error(e.toString());
  87. }
  88. }
  89. }
  90. module.exports = CouponService;