diningCoin.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. 'use strict';
  2. const BaseService = require('./base.js');
  3. // 餐饮币变动服务类
  4. class DiningCoinService 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. case 0:
  17. case 2:
  18. action_user = '店铺提现';
  19. break;
  20. case 1:
  21. action_user = '购买餐币';
  22. break;
  23. case 3:
  24. action_user = '餐饮消费';
  25. break;
  26. case 4:
  27. action_user = '核销餐币';
  28. break;
  29. }
  30. const createBean = await that.app.comoBean.instance({
  31. user_id: params.user_id,
  32. order_id: params.order_id,
  33. type: params.type,
  34. account: params.account,
  35. out_batch_no: params.out_batch_no,
  36. out_detail_no: params.out_detail_no,
  37. partner_id: params.partner_id,
  38. action_user,
  39. log_desc: params.log_desc,
  40. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  41. ori_partner: params.ori_partner,
  42. customer_id: params.customer_id,
  43. customer_name: params.customer_name,
  44. customer_img: params.customer_img,
  45. }, { transaction });
  46. const result = await that.create(createBean, that.app.model.DinnerCoinLogs, '记录用户餐饮币变动失败');
  47. return result;
  48. }
  49. /**
  50. * 餐饮币发放到用户账户 并 记录
  51. * @param orderInfo
  52. */
  53. async dispatchDiningCoin(orderInfo = null) {
  54. const that = this;
  55. const transaction = await that.app.model.transaction();
  56. try {
  57. if (orderInfo && !orderInfo.dining_coin_send) {
  58. // 2023/2/28 客源来源鉴别
  59. let ori_partner_id = -1;
  60. const inviterInfo = await that.app.model.Users.findOne({
  61. where: { user_id: orderInfo.inviter_id },
  62. attributes: [ 'partner_id' ],
  63. });
  64. if (inviterInfo) {
  65. ori_partner_id = inviterInfo.partner_id;
  66. }
  67. // 2023/2/28 订单餐饮币发放状态
  68. let dining_coin_send = false;
  69. // 2023/2/27 订单商品匹配发放餐饮币
  70. if (orderInfo.orders_products.length > 0) {
  71. for (const ordersProduct of orderInfo.orders_products) {
  72. if (ordersProduct.dining_partner_id > 0 && ordersProduct.dinning_coin_amount > 0) {
  73. // 2022/11/18 记录餐饮币发放记录
  74. await that.addDiningCoinChangeLog({
  75. user_id: orderInfo.user_id,
  76. order_id: orderInfo.order_id,
  77. partner_id: ordersProduct.dining_partner_id,
  78. type: 1,
  79. account: ordersProduct.dinning_coin_amount * ordersProduct.product_count,
  80. log_desc: '购酒获得餐饮币',
  81. }, transaction);
  82. // 2023/2/28 查询用户餐饮币账户列表 添加 或 更新账户余额
  83. const info = await that.app.model.DinnerCoins.findOne({
  84. where: {
  85. user_id: orderInfo.user_id,
  86. ori_partner: ori_partner_id === ordersProduct.dining_partner_id,
  87. partner_id: ordersProduct.dining_partner_id,
  88. expired: false,
  89. },
  90. transaction,
  91. raw: true,
  92. });
  93. if (!info) {
  94. // 2023/2/27 没有对应类型的餐饮币 则插入该类型的餐饮币账户
  95. const data = {
  96. user_id: orderInfo.user_id,
  97. account: ordersProduct.dinning_coin_amount * ordersProduct.product_count,
  98. ori_partner: ori_partner_id === ordersProduct.dining_partner_id,
  99. partner_id: ordersProduct.dining_partner_id,
  100. create_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  101. expired: false,
  102. expired_time: that.app.szjcomo.date('Y-m-d H:i:s', parseInt(+new Date() + '') / 1000 + 90 * 24 * 60 * 60),
  103. };
  104. const res = await that.app.model.PartnerInfo.findOne({
  105. where: { id: ordersProduct.dining_partner_id },
  106. transaction,
  107. raw: true,
  108. });
  109. data.partner_name = res.name;
  110. data.partner_address = res.address;
  111. data.partner_tel = res.tel_num;
  112. data.partner_opening_time = res.opening_time;
  113. const createBean = await that.app.comoBean.instance(data, { transaction });
  114. await that.service.base.create(createBean, that.app.model.DinnerCoins, '餐饮币发放失败,请重试');
  115. } else {
  116. const updateBean = await that.app.comoBean.instance({
  117. account: info.account + ordersProduct.dinning_coin_amount * ordersProduct.product_count,
  118. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  119. expired: false,
  120. expired_time: that.app.szjcomo.date('Y-m-d H:i:s', parseInt(+new Date() + '') / 1000 + 90 * 24 * 60 * 60),
  121. }, {
  122. where: {
  123. user_id: orderInfo.user_id,
  124. ori_partner: ori_partner_id === ordersProduct.dining_partner_id,
  125. partner_id: ordersProduct.dining_partner_id,
  126. }, transaction,
  127. });
  128. await that.service.base.update(updateBean, that.app.model.DinnerCoins, '餐饮币余额更新失败,请重试');
  129. }
  130. dining_coin_send = true;
  131. // 2023/3/28 更新餐店订单量+1
  132. const updatePartnerBean = await that.app.comoBean.instance({
  133. order_amount: that.app.Sequelize.literal('order_amount + ' + 1),
  134. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  135. }, { where: { id: ordersProduct.dining_partner_id }, transaction });
  136. await that.update(updatePartnerBean, that.app.model.PartnerInfo, '合作餐店订单量更新失败,请稍候重试');
  137. }
  138. }
  139. // 2023/2/28 更新订单餐饮币发放状态
  140. if (dining_coin_send) {
  141. const updateBean = that.app.comoBean.instance({ dining_coin_send }, {
  142. where: { order_id: orderInfo.order_id }, transaction,
  143. });
  144. await that.service.base.update(updateBean, that.app.model.Orders, '更新订单餐饮币发放状态失败,请稍候重试');
  145. }
  146. await transaction.commit();
  147. }
  148. }
  149. } catch (e) {
  150. if (transaction) await transaction.rollback();
  151. throw new Error(e.toString());
  152. }
  153. }
  154. /**
  155. * 付款后操作检查订单及餐饮币发放
  156. * @return {Promise<void>}
  157. */
  158. async orderDiningCoinDispatchAfterPay(order_id = -1) {
  159. const that = this;
  160. // 2022/12/24 查询订单包含商品列表详情
  161. const order = await that.selectOrderInfo(order_id);
  162. if (!order) throw new Error('订单不存在');
  163. await that.dispatchDiningCoin(order);
  164. }
  165. /**
  166. * 查询订单详情
  167. * @param order_id
  168. * @return {Promise<null|*>}
  169. */
  170. async selectOrderInfo(order_id) {
  171. const that = this;
  172. const options = {
  173. where: { order_id }, include: [
  174. {
  175. model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
  176. exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
  177. },
  178. },
  179. ], attributes: {
  180. exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
  181. },
  182. };
  183. const selectBean = await that.app.comoBean.instance({}, options);
  184. try {
  185. return await that.service.base.select(selectBean, that.app.model.Orders, '查询订单详情失败,请稍候重试', false, false);
  186. } catch (e) {
  187. that.logger.error('订单id:%s ;分佣查询订单详情失败', order_id);
  188. return null;
  189. }
  190. }
  191. }
  192. module.exports = DiningCoinService;