AddonsCouponReward.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace addons\Coupon\common\models;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Redpack\common\enums\RedpackEnum;
  5. use addons\Redpack\common\models\RedpackWallet;
  6. use common\enums\OrderStatusEnum;
  7. use common\enums\StatusEnum;
  8. use common\enums\UserWalletEnum;
  9. use common\models\mall\MallAddons;
  10. use common\models\order\Order;
  11. use common\models\order\OrderMarketing;
  12. use services\common\UserWalletService;
  13. use Yii;
  14. /**
  15. * This is the model class for table "{{%addons_coupon_reward}}".
  16. *
  17. * @property int $id
  18. * @property int $mall_id
  19. * @property int $coupon_id 优惠券id
  20. * @property int $reward_type 奖励类型,1:转赠奖励,2:使用奖励
  21. * @property float $balance 奖励余额
  22. * @property float $score 奖励积分
  23. * @property float $red_packet 奖励红包
  24. * @property string $reward_currency_type 奖励货币种类,记录货币 code
  25. * @property float $number 奖励货币数量
  26. * @property int $created_at
  27. * @property int $updated_at
  28. */
  29. class AddonsCouponReward extends \common\models\base\BaseActiveRecord
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_coupon_reward}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'coupon_id'], 'required'],
  45. [['mall_id', 'coupon_id', 'reward_type', 'created_at', 'updated_at'], 'integer'],
  46. [['balance', 'score', 'red_packet', 'number'], 'number'],
  47. [['reward_currency_type'], 'string', 'max' => 50],
  48. ];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'mall_id' => 'Mall ID',
  58. 'coupon_id' => 'Coupon ID',
  59. 'reward_type' => 'Reward Type',
  60. 'balance' => 'Balance',
  61. 'score' => 'Score',
  62. 'red_packet' => 'Red Packet',
  63. 'reward_currency_type' => 'Reward Currency Type',
  64. 'number' => 'Number',
  65. 'created_at' => 'Created At',
  66. 'updated_at' => 'Updated At',
  67. ];
  68. }
  69. /**
  70. * @desc:订单使用优惠券,赠送奖励
  71. * @Author: hua
  72. * @Date: 2021/12/24
  73. * @Time: 10:48
  74. * @Copyright: copyright (c) 2021 广东七件事集团
  75. * @param $order_id
  76. * @param $order_detail_ids
  77. */
  78. public static function giveReward($order_id, $order_detail_ids)
  79. {
  80. try {
  81. if (!empty($order_detail_ids)) {
  82. $order = Order::findOne(['id' => $order_id, 'status' => StatusEnum::ENABLED, 'pay_status' => OrderStatusEnum::PAY, 'is_recycle' => 0]);
  83. if (empty($order)) return;
  84. // 当前订单使用的优惠券
  85. $used_coupons = AddonsCouponUseLog::find()
  86. ->alias('cul')
  87. ->select('ac.is_giving_reward,ac.is_consume_reward,cul.coupon_id,cul.user_coupon_id,cul.id')
  88. ->join('LEFT JOIN', AddonsCoupon::tableName() . ' as ac', 'ac.id = cul.coupon_id')
  89. ->where(['cul.order_id' => $order_id])
  90. ->andWhere(['or', ['ac.is_giving_reward' => 1], ['ac.is_consume_reward' => 1]])
  91. ->asArray()
  92. ->all();
  93. if (empty($used_coupons)) return;
  94. $reward_list = [];
  95. foreach ($used_coupons as $coupon) {
  96. if (1 == $coupon['is_consume_reward']) {
  97. // 优惠券开启了使用奖励
  98. $desc = '优惠券使用奖励';
  99. $user_id = $order->user_id;
  100. $user_coupon = AddonsCouponUserRelate::findOne(['id' => $coupon['user_coupon_id'], 'is_use' => 1, 'is_send_reward' => 0]);
  101. if (!empty($user_coupon)) {
  102. $used_reward = AddonsCouponReward::find()
  103. ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['coupon_id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_CONSUME])
  104. ->asArray()
  105. ->one();
  106. $used_reward['user_id'] = $user_id;
  107. $used_reward['desc'] = $desc;
  108. $used_reward['from_type'] = UserWalletEnum::CONSUME;
  109. $used_reward['capital_type'] = UserWalletEnum::USE_COUPON_GIVE;
  110. $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_GIVING;
  111. $used_reward['source_table_id'] = $coupon['id'];
  112. $reward_list[] = $used_reward;
  113. }
  114. }
  115. if (1 == $coupon['is_giving_reward']) {
  116. // 优惠券开启了转赠奖励
  117. $desc = '优惠券转赠奖励';
  118. $user_coupon = AddonsCouponUserRelate::findOne(['id' => $coupon['user_coupon_id'], 'is_giving' => 0, 'giving_status' => 0, 'is_use' => 1, 'is_send_reward' => 0]);
  119. if (!empty($user_coupon)) {
  120. $shared_log = AddonsCouponShareLog::findOne(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['coupon_id'], 'get_user_coupon_id' => $coupon['user_coupon_id'], 'receive_user_id' => $order->user_id, 'giving_status' => 2]);
  121. if (empty($shared_log)) continue;
  122. $user_id = $shared_log->giving_user_id;
  123. // 如果转出会员的 id=0 说明是平台赠送的优惠券,不发放转赠奖励
  124. if (!empty($user_id)) {
  125. $used_reward = AddonsCouponReward::find()
  126. ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['coupon_id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_GIVING])
  127. ->asArray()
  128. ->one();
  129. $used_reward['user_id'] = $user_id;
  130. $used_reward['desc'] = $desc;
  131. $used_reward['from_type'] = UserWalletEnum::CONSUME;
  132. $used_reward['capital_type'] = UserWalletEnum::GIVING_COUPON_REWARD;
  133. $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_CONSUME;
  134. $used_reward['source_table_id'] = $coupon['id'];
  135. $reward_list[] = $used_reward;
  136. }
  137. }
  138. }
  139. }
  140. $insert_wallet = [];
  141. foreach ($reward_list as $value) {
  142. $balance = floatval($value['balance']);
  143. $score = floatval($value['score']);
  144. $red_packet = floatval($value['red_packet']);
  145. $currency = floatval($value['number']);
  146. if (!empty($balance)) {
  147. $insert_wallet[] = [
  148. 'mall_id' => $order['mall_id'],
  149. 'user_id' => $value['user_id'],
  150. 'is_frozen' => false,
  151. 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE,
  152. 'money' => $balance,
  153. 'desc' => $value['desc'] . '余额 ' . $balance . ' 元',
  154. 'source_table' => Order::tableName(),
  155. 'source_table_id' => $order_id,
  156. 'from_type' => $value['from_type'],
  157. 'capital_type' => $value['capital_type'],
  158. ];
  159. }
  160. if (!empty($score)) {
  161. $insert_wallet[] = [
  162. 'mall_id' => $order['mall_id'],
  163. 'user_id' => $value['user_id'],
  164. 'is_frozen' => false,
  165. 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE,
  166. 'money' => $score,
  167. 'desc' => $value['desc'] . ' ' . $score . ' 积分',
  168. 'source_table' => Order::tableName(),
  169. 'source_table_id' => $order_id,
  170. 'from_type' => $value['from_type'],
  171. 'capital_type' => $value['capital_type'],
  172. ];
  173. }
  174. if (!empty($red_packet)) {
  175. if (MallAddons::isInstall($order['mall_id'], RedpackEnum::ADDONS_NAME)) {
  176. // 红包
  177. RedpackWallet::setData([
  178. 'mall_id' => $order['mall_id'],
  179. 'user_id' => $value['user_id'],
  180. 'is_frozen' => true,
  181. 'money' => $red_packet,
  182. 'source_table' => Order::tableName(),
  183. 'source_table_id' => $order_id,
  184. 'from_type' => RedpackEnum::FROM_USE_COUPON,
  185. 'desc' => "订单ID: {$order_id}" . $value['desc'],
  186. ]);
  187. };
  188. }
  189. if (!empty($currency)) {
  190. // 系统虚拟货币
  191. }
  192. }
  193. if ($insert_wallet) {
  194. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  195. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  196. // 更新 qimall_addons_coupon_user_relate 表奖励状态
  197. $user_coupon_ids = array_column($used_coupons, 'user_coupon_id');
  198. $rst = AddonsCouponUserRelate::updateAll(['is_send_reward' => 1, 'coupon_status' => AddonsCouponEnum::STATUS_FAILURE], ['id' => $user_coupon_ids, 'is_use' => 1]);
  199. }
  200. }
  201. return true;
  202. } catch (\Exception $e) {
  203. Yii::error('优惠券奖励发放失败');
  204. Yii::error($e);
  205. echo 'AddonsCouponReward:giveReward:' . $e->getMessage();
  206. }
  207. }
  208. /**
  209. * @name:
  210. * @msg: 保存数据
  211. * @param {int} $mall_id
  212. * @param {int} $coupon_id
  213. * @param {int} $reward_type 奖励类型
  214. * @param {array} $rewards 具体奖励
  215. * @return {*}
  216. */
  217. public static function setData(int $mall_id, int $coupon_id, int $reward_type, array $rewards): bool
  218. {
  219. if (empty($mall_id) || empty($coupon_id) || empty($reward_type) || empty($rewards)) {
  220. self::$static_error = '参数错误';
  221. return false;
  222. }
  223. $model = self::findOne(['mall_id' => $mall_id, 'coupon_id' => $coupon_id, 'reward_type' => $reward_type]);
  224. if (empty($model)) {
  225. $model = new self();
  226. $model->loadDefaultValues();
  227. $model->mall_id = $mall_id;
  228. $model->coupon_id = $coupon_id;
  229. }
  230. foreach ($rewards as $key => $val) {
  231. $model->$key = $val;
  232. }
  233. $model->reward_type = $reward_type;
  234. if (!$model->save()) {
  235. self::$static_error = '保存奖励数据失败:' . $model->getErrorMessage();
  236. return false;
  237. }
  238. return true;
  239. }
  240. }