StoreOrderGive.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\UserWalletEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use services\common\UserWalletService;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_store_order_give".
  9. *
  10. * @property int $id 主键
  11. * @property int $mall_id mall_id
  12. * @property int $store_id 门店id
  13. * @property int $order_id 订单id
  14. * @property float $give_score 赠送积分
  15. * @property string|null $give_coupon 赠送优惠券(json字符串保存)
  16. * @property string|null $give_currency 赠送货币(json字符串保存)
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 修改时间
  20. */
  21. class StoreOrderGive extends BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_store_order_give}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'store_id', 'order_id', 'status', 'created_at', 'updated_at'], 'integer'],
  37. [['give_score'], 'number'],
  38. [['give_coupon', 'give_currency'], 'string'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => 'Mall ID',
  49. 'store_id' => 'Store ID',
  50. 'order_id' => 'Order ID',
  51. 'give_score' => 'Give Score',
  52. 'give_coupon' => 'Give Coupon',
  53. 'give_currency' => 'Give Currency',
  54. 'status' => 'Status',
  55. 'created_at' => 'Created At',
  56. 'updated_at' => 'Updated At',
  57. ];
  58. }
  59. public static function setData($give_data, $order_id, $store_id, $mall_id)
  60. {
  61. if (empty($give_data)) return true;
  62. $model = new self();
  63. $model->loadDefaultValues();
  64. $give_score = 0;
  65. $give_coupon = [];
  66. $give_currency = [];
  67. foreach ($give_data as $item) {
  68. if ($item['type'] == 'score') {
  69. $give_score = $item['num'];
  70. }
  71. if ($item['type'] == 'coupon') {
  72. $give_coupon[] = $item;
  73. }
  74. }
  75. $model->give_score = $give_score;
  76. $model->give_coupon = json_encode($give_coupon);
  77. $model->give_currency = json_encode($give_currency);
  78. $model->order_id = $order_id;
  79. $model->store_id = $store_id;
  80. $model->mall_id = $mall_id;
  81. return $model->save();
  82. }
  83. public static function give($mall_id, $user_id, $order_id, $store_id)
  84. {
  85. try {
  86. $order_give = self::findOne(['order_id' => $order_id, 'mall_id' => $mall_id]);
  87. if (empty($order_give)) return true;
  88. /*if (!empty($order_give['give_score'])) {
  89. $insert_wallet[] = [
  90. 'mall_id' => $mall_id,
  91. 'user_id' => $user_id,
  92. 'is_frozen' => false,
  93. 'wallet_type' => 'score',
  94. 'money' => $order_give['give_score'],
  95. 'desc' => 'o2o订单支付赠送',
  96. 'source_table' => StoreOrder::tableName(),
  97. 'source_table_id' => $order_id,
  98. 'from_type' => UserWalletEnum::CONSUME,
  99. 'capital_type' => UserWalletEnum::CONSUME_ORDER_GIVE,
  100. ];
  101. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  102. if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
  103. }*/
  104. $give_coupon = json_decode($order_give['give_coupon'], true);
  105. if (!empty($give_coupon)) {
  106. foreach ($give_coupon as $item) {
  107. StoreCouponUserRelate::giveCouponByOrder($mall_id, $store_id, $user_id, [$item['id']=>$item['num']], $item['from_type'], 2, 'o2o');
  108. }
  109. }
  110. return true;
  111. } catch (\Exception $e) {
  112. self::setStaticError($e->getMessage());
  113. return false;
  114. }
  115. }
  116. }