StoreCouponReward.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace addons\Store\common\models;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\Coupon\common\models\AddonsCouponReward;
  6. use addons\Coupon\common\models\AddonsCouponShareLog;
  7. use common\enums\OrderStatusEnum;
  8. use common\enums\StatusEnum;
  9. use common\enums\UserWalletEnum;
  10. use common\models\base\BaseActiveRecord;
  11. use services\common\UserWalletService;
  12. use Yii;
  13. /**
  14. * This is the model class for table "qimall_addons_store_coupon_reward".
  15. *
  16. * @property int $id
  17. * @property int $mall_id
  18. * @property int $store_id 门店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 $status 状态[-1:删除;0:禁用;1启用]
  27. * @property int $created_at
  28. * @property int $updated_at
  29. */
  30. class StoreCouponReward extends BaseActiveRecord
  31. {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public static function tableName()
  36. {
  37. return '{{%addons_store_coupon_reward}}';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['mall_id', 'store_id', 'coupon_id'], 'required'],
  46. [['mall_id', 'store_id', 'coupon_id', 'reward_type', 'status', 'created_at', 'updated_at'], 'integer'],
  47. [['balance', 'score', 'red_packet', 'number'], 'number'],
  48. [['reward_currency_type'], 'string', 'max' => 50],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'mall_id' => 'Mall ID',
  59. 'store_id' => 'Store ID',
  60. 'coupon_id' => 'Coupon ID',
  61. 'reward_type' => 'Reward Type',
  62. 'balance' => 'Balance',
  63. 'score' => 'Score',
  64. 'red_packet' => 'Red Packet',
  65. 'reward_currency_type' => 'Reward Currency Type',
  66. 'number' => 'Number',
  67. 'status' => 'Status',
  68. 'created_at' => 'Created At',
  69. 'updated_at' => 'Updated At',
  70. ];
  71. }
  72. public static function setData(int $mall_id, int $coupon_id, int $store_id, int $reward_type, array $rewards): bool
  73. {
  74. if (empty($mall_id) || empty($coupon_id) || empty($reward_type) || empty($rewards) || empty($store_id)) {
  75. self::$static_error = '参数错误';
  76. return false;
  77. }
  78. $model = self::findOne(['mall_id' => $mall_id, 'coupon_id' => $coupon_id, 'store_id' => $store_id, 'reward_type' => $reward_type]);
  79. if (empty($model)) {
  80. $model = new self();
  81. $model->loadDefaultValues();
  82. $model->mall_id = $mall_id;
  83. $model->store_id = $store_id;
  84. $model->coupon_id = $coupon_id;
  85. }
  86. foreach ($rewards as $key => $val) {
  87. $model->$key = $val;
  88. }
  89. $model->reward_type = $reward_type;
  90. if (!$model->save()) {
  91. self::$static_error = '保存奖励数据失败:' . $model->getErrorMessage();
  92. return false;
  93. }
  94. return true;
  95. }
  96. public static function giveReward($order_id, $order_detail_ids)
  97. {
  98. try {
  99. if (!empty($order_detail_ids)) {
  100. $order = StoreOrder::findOne(['id' => $order_id, 'status' => StatusEnum::ENABLED, 'pay_status' => OrderStatusEnum::PAY, 'is_recycle' => 0]);
  101. if (empty($order)) return;
  102. // 当前订单使用的优惠券
  103. $store_coupon_use_logs = StoreCouponUseLog::lists(['where' => [['order_id' => $order_id]]]);
  104. if (empty($store_coupon_use_logs)) return;
  105. $user_coupon_ids = array_column($store_coupon_use_logs, 'user_coupon_id');
  106. $store_coupon_user_relate_list = StoreCouponUserRelate::lists(['where' => [['id' => $user_coupon_ids]]]);
  107. $platform_coupon_ids = $store_coupon_ids = [];
  108. $platform_coupon_arr = $store_coupon_arr = [];
  109. foreach ($store_coupon_user_relate_list as $item) {
  110. switch ($item['from_type']) {
  111. case 1:
  112. $platform_coupon_ids[] = $item['coupon_id'];
  113. $platform_coupon_arr[$item['coupon_id']] = $item['id'];
  114. break;
  115. case 2:
  116. $store_coupon_ids[] = $item['coupon_id'];
  117. $store_coupon_arr[$item['coupon_id']] = $item['id'];
  118. break;
  119. }
  120. }
  121. $reward_list = [];
  122. if (!empty($platform_coupon_ids)) {
  123. $platform_coupon_list = AddonsCoupon::find()->where(['id' => $platform_coupon_ids])
  124. ->andWhere(['or', ['is_giving_reward' => 1], ['is_consume_reward' => 1]])
  125. ->all();
  126. if (!empty($platform_coupon_list)) {
  127. foreach ($platform_coupon_list as $coupon) {
  128. if (1 == $coupon['is_consume_reward']) {
  129. // 优惠券开启了使用奖励
  130. $desc = '优惠券使用奖励';
  131. $user_id = $order->user_id;
  132. $used_reward = AddonsCouponReward::find()
  133. ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_CONSUME])
  134. ->asArray()
  135. ->one();
  136. $used_reward['user_id'] = $user_id;
  137. $used_reward['desc'] = $desc;
  138. $used_reward['from_type'] = UserWalletEnum::CONSUME;
  139. $used_reward['capital_type'] = UserWalletEnum::USE_COUPON_GIVE;
  140. $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_GIVING;
  141. $used_reward['source_table_id'] = $coupon['id'];
  142. $reward_list[] = $used_reward;
  143. }
  144. if (1 == $coupon['is_giving_reward']) {
  145. // 优惠券开启了转赠奖励
  146. $desc = '优惠券转赠奖励';
  147. $shared_log = AddonsCouponShareLog::findOne(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'get_user_coupon_id' => $platform_coupon_arr[$coupon['id']], 'receive_user_id' => $order->user_id, 'giving_status' => 2]);
  148. if (empty($shared_log)) continue;
  149. $user_id = $shared_log->giving_user_id;
  150. // 如果转出会员的 id=0 说明是平台赠送的优惠券,不发放转赠奖励
  151. if (!empty($user_id)) {
  152. $used_reward = AddonsCouponReward::find()
  153. ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_GIVING])
  154. ->asArray()
  155. ->one();
  156. $used_reward['user_id'] = $user_id;
  157. $used_reward['desc'] = $desc;
  158. $used_reward['from_type'] = UserWalletEnum::CONSUME;
  159. $used_reward['capital_type'] = UserWalletEnum::GIVING_COUPON_REWARD;
  160. $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_CONSUME;
  161. $used_reward['source_table_id'] = $coupon['id'];
  162. $reward_list[] = $used_reward;
  163. }
  164. }
  165. }
  166. }
  167. }
  168. if (!empty($store_coupon_ids)) {
  169. $store_coupon_list = StoreCoupon::find()->where(['id' => $store_coupon_ids])
  170. ->andWhere(['or', ['is_giving_reward' => 1], ['is_consume_reward' => 1]])
  171. ->all();
  172. if (!empty($store_coupon_list)) {
  173. foreach ($store_coupon_list as $coupon) {
  174. if (1 == $coupon['is_consume_reward']) {
  175. // 优惠券开启了使用奖励
  176. $desc = '优惠券使用奖励';
  177. $user_id = $order->user_id;
  178. $used_reward = StoreCouponReward::find()
  179. ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_CONSUME])
  180. ->asArray()
  181. ->one();
  182. $used_reward['user_id'] = $user_id;
  183. $used_reward['desc'] = $desc;
  184. $used_reward['from_type'] = UserWalletEnum::CONSUME;
  185. $used_reward['capital_type'] = UserWalletEnum::USE_COUPON_GIVE;
  186. $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_GIVING;
  187. $used_reward['source_table_id'] = $coupon['id'];
  188. $reward_list[] = $used_reward;
  189. }
  190. if (1 == $coupon['is_giving_reward']) {
  191. // 优惠券开启了转赠奖励
  192. $desc = '优惠券转赠奖励';
  193. $shared_log = StoreCouponShareLog::findOne(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'get_user_coupon_id' => $store_coupon_arr[$coupon['id']], 'receive_user_id' => $order->user_id, 'giving_status' => 2]);
  194. if (empty($shared_log)) continue;
  195. $user_id = $shared_log->giving_user_id;
  196. // 如果转出会员的 id=0 说明是平台赠送的优惠券,不发放转赠奖励
  197. if (!empty($user_id)) {
  198. $used_reward = StoreCouponReward::find()
  199. ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_GIVING])
  200. ->asArray()
  201. ->one();
  202. $used_reward['user_id'] = $user_id;
  203. $used_reward['desc'] = $desc;
  204. $used_reward['from_type'] = UserWalletEnum::CONSUME;
  205. $used_reward['capital_type'] = UserWalletEnum::GIVING_COUPON_REWARD;
  206. $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_CONSUME;
  207. $used_reward['source_table_id'] = $coupon['id'];
  208. $reward_list[] = $used_reward;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. $insert_wallet = [];
  215. foreach ($reward_list as $value) {
  216. $balance = floatval($value['balance']);
  217. $score = floatval($value['score']);
  218. if (!empty($balance)) {
  219. $insert_wallet[] = [
  220. 'mall_id' => $order['mall_id'],
  221. 'user_id' => $value['user_id'],
  222. 'is_frozen' => false,
  223. 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE,
  224. 'money' => $balance,
  225. 'desc' => $value['desc'] . '余额 ' . $balance . ' 元',
  226. 'source_table' => StoreOrder::tableName(),
  227. 'source_table_id' => $order_id,
  228. 'from_type' => $value['from_type'],
  229. 'capital_type' => $value['capital_type'],
  230. ];
  231. }
  232. if (!empty($score)) {
  233. $insert_wallet[] = [
  234. 'mall_id' => $order['mall_id'],
  235. 'user_id' => $value['user_id'],
  236. 'is_frozen' => false,
  237. 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE,
  238. 'money' => $score,
  239. 'desc' => $value['desc'] . ' ' . $score . ' 积分',
  240. 'source_table' => StoreOrder::tableName(),
  241. 'source_table_id' => $order_id,
  242. 'from_type' => $value['from_type'],
  243. 'capital_type' => $value['capital_type'],
  244. ];
  245. }
  246. }
  247. if ($insert_wallet) {
  248. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  249. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  250. // 更新 qimall_addons_coupon_user_relate 表奖励状态
  251. $rst = StoreCouponUserRelate::updateAll(['is_send_reward' => 1, 'coupon_status' => AddonsCouponEnum::STATUS_FAILURE], ['id' => $user_coupon_ids, 'is_use' => 1]);
  252. }
  253. }
  254. return true;
  255. } catch (\Exception $e) {
  256. echo 'StoreCouponReward:giveReward:' . $e->getMessage().$e->getLine().$e->getFile();
  257. }
  258. }
  259. }