StoreCouponUseLog.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace addons\Store\common\models;
  3. use addons\Coupon\common\models\AddonsCouponGoodStatistics;
  4. use addons\Coupon\common\models\AddonsCouponStatistics;
  5. use common\enums\AddonsEnum;
  6. use common\enums\StatusEnum;
  7. use common\models\base\BaseActiveRecord;
  8. use common\models\mall\MallAddons;
  9. use Yii;
  10. /**
  11. * This is the model class for table "qimall_addons_store_coupon_use_log".
  12. *
  13. * @property int $id 主键
  14. * @property int $mall_id mall_id
  15. * @property int $user_id user_id
  16. * @property int $order_id order_id
  17. * @property int $coupon_id coupon_id
  18. * @property int $user_coupon_id user_coupon_id
  19. * @property int $status 状态[-1:删除;0:禁用;1启用]
  20. * @property int $created_at 创建时间
  21. * @property int $updated_at 修改时间
  22. */
  23. class StoreCouponUseLog extends BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%addons_store_coupon_use_log}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'user_id', 'order_id', 'coupon_id', 'user_coupon_id', 'status', 'created_at', 'updated_at'], 'integer'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => '主键',
  48. 'mall_id' => 'mall_id',
  49. 'user_id' => 'user_id',
  50. 'order_id' => 'order_id',
  51. 'coupon_id' => 'coupon_id',
  52. 'user_coupon_id' => 'user_coupon_id',
  53. 'status' => '状态[-1:删除;0:禁用;1启用]',
  54. 'created_at' => '创建时间',
  55. 'updated_at' => '修改时间',
  56. ];
  57. }
  58. public static function setData($params)
  59. {
  60. Yii::$app->db->close();
  61. try {
  62. $order_detail_id_arr = array_keys($params['order_detail_id_arr']);
  63. $list = StoreOrderMarketing::lists(['where' => [['order_detail_id' => $order_detail_id_arr]], 'select' => 'user_coupon_id,order_detail_id']);
  64. $user_coupon_id_arr = [];
  65. $temp_arr = [];
  66. $user_coupon_arr = [];
  67. $user_coupon_order_detail_id_arr = [];
  68. foreach ($list as $item) {
  69. $user_coupon_id = json_decode($item['user_coupon_id'], 'true');
  70. if (!empty($user_coupon_id)) {
  71. foreach ($user_coupon_id as $key => $val) {
  72. if(isset($user_coupon_arr[$key])){
  73. $user_coupon_arr[$key] += $val;
  74. }else{
  75. $user_coupon_arr[$key] = $val;
  76. }
  77. $user_coupon_order_detail_id_arr[$key][] = $item['order_detail_id'];
  78. $user_coupon_id_arr[] = $key;
  79. $temp_arr[$key] = $item['order_detail_id'];
  80. }
  81. }
  82. }
  83. if (!empty($user_coupon_id_arr)) {
  84. $coupon_user_relate_list = StoreCouponUserRelate::lists(['where' => [['id' => $user_coupon_id_arr]], 'select' => 'id,coupon_id,store_id,from_type']);
  85. $field = ['mall_id','user_id', 'order_id', 'coupon_id', 'user_coupon_id', 'status', 'created_at', 'updated_at'];
  86. $time = time();
  87. $insert = [];
  88. foreach ($coupon_user_relate_list as $val) {
  89. $arr['mall_id'] = $params['mall_id'];
  90. $arr['user_id'] = $params['user_id'];
  91. $arr['order_id'] = $params['order_detail_id_arr'][$temp_arr[$val['id']]];
  92. $arr['coupon_id'] = $val['coupon_id'];
  93. $arr['user_coupon_id'] = $val['id'];
  94. $arr['status'] = StatusEnum::ENABLED;
  95. $arr['created_at'] = $time;
  96. $arr['updated_at'] = $time;
  97. $insert[] = $arr;
  98. $set_data = [
  99. 'mall_id'=>$params['mall_id'],
  100. 'coupon_id'=>$val['coupon_id'],
  101. 'used_number'=>1,'use_user_number'=>1,'use_order_number'=>$params['counts']??0,
  102. 'total_payed_amount'=>$params['total_payed_amount']??0,'total_use_coupon_payed_amount'=>$params['total_use_coupon_payed_amount']??0,
  103. 'total_discount_amount'=>$user_coupon_arr[$val['id']]??0,
  104. ];
  105. $set_goods_data = [
  106. 'mall_id'=>$params['mall_id'],
  107. 'coupon_id'=>$val['coupon_id'],
  108. 'payed_number'=>0,'buyed_user_number'=>1,
  109. ];
  110. if(!empty($user_coupon_order_detail_id_arr[$val['id']])){
  111. $order_detail_ids = $user_coupon_order_detail_id_arr[$val['id']];
  112. foreach ($order_detail_ids as $order_detail_id){
  113. if(!empty($params['order_detail_arr'][$order_detail_id])){
  114. $set_goods_data['good_id'] = $params['order_detail_arr'][$order_detail_id]['goods_id'];
  115. $set_goods_data['payed_number'] = $params['order_detail_arr'][$order_detail_id]['num'];
  116. if($val['from_type']==1){
  117. if(MallAddons::isInstall($params['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){
  118. AddonsCouponGoodStatistics::setData($set_goods_data);
  119. }
  120. }else{
  121. StoreCouponGoodStatistics::setData($set_goods_data);
  122. }
  123. }
  124. }
  125. }
  126. if($val['from_type']==1){
  127. if(MallAddons::isInstall($params['mall_id'],AddonsEnum::ADDONS_NAME_COUPON)){
  128. AddonsCouponStatistics::setData($set_data);
  129. }
  130. }else{
  131. $set_data['store_id'] = $val['store_id'];
  132. StoreCouponStatistics::setData($set_data);
  133. }
  134. }
  135. // 批量插入数据
  136. $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  137. if ($res == false) throw new \Exception('添加优惠券使用记录失败');
  138. }
  139. return true;
  140. }catch (\Exception $e) {
  141. self::$static_error = $e->getMessage();
  142. return false;
  143. }
  144. }
  145. }