ActivityRebateOrderN.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\ActiveRecord;
  11. /**
  12. * This is the model class for table "{{%activity_rebate_order_n}}".
  13. *
  14. * @property integer $id
  15. */
  16. class ActivityRebateOrderN extends \yii\db\ActiveRecord
  17. {
  18. /**
  19. * @inheritdoc
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%activity_rebate_order_n}}';
  24. }
  25. public function behaviors()
  26. {
  27. return [
  28. [
  29. 'class' => TimestampBehavior::class,
  30. ]
  31. ];
  32. }
  33. public function beforeSave($insert) {
  34. if(parent::beforeSave($insert)){
  35. $has = self::find()->andWhere([
  36. 'store_id' => $this->store_id,
  37. 'goods_id' => $this->goods_id,
  38. 'is_delete' => 0,
  39. 'status' => 1,
  40. ])->andWhere([
  41. '!=', 'id', (int)$this->id
  42. ])->one();
  43. if($has){
  44. $this->addError('goods_id', '操作失败,商品已存在活动信息,活动id:' . $has->id);
  45. return false;
  46. }
  47. return true;
  48. }
  49. return false;
  50. }
  51. public static function decodeRules($rules) {
  52. if(is_string($rules)){
  53. $rules = json_decode($rules, true);
  54. }
  55. if(!is_array($rules)){
  56. return null;
  57. }
  58. if(empty($rules['list'])){
  59. //默认值
  60. $rules = [
  61. 'type' => '0', //0固定值,1百分比
  62. 'list' => [
  63. [
  64. 'order_count' => '0',
  65. 'price' => '0',
  66. ]
  67. ],
  68. ];
  69. }
  70. $rules['total_price'] = 0;
  71. foreach($rules['list'] as &$item){
  72. $rules['total_price'] += $item['price'];
  73. if($item['user_price_order']){
  74. $item['user_price_order_no'] = Order::find()->where(['id' => $item['user_price_order']])->select('order_no')->asArray()->column();
  75. }
  76. }
  77. $rules['total_price'] = sprintf("%.2f", $rules['total_price']);
  78. return $rules;
  79. }
  80. public static function encodeRules($rules) {
  81. return json_encode($rules);
  82. }
  83. public static function activityAt($store_id = 0, $goods_id = 0) {
  84. $query = self::find();
  85. $query->andWhere([
  86. 'store_id' => $store_id,
  87. 'goods_id' => $goods_id,
  88. 'is_delete' => 0,
  89. 'status' => 1,
  90. ]);
  91. $info = $query->one();
  92. return $info;
  93. }
  94. //店铺进行中活动
  95. public static function activityAtList($store_id = 0, $asArray = false) {
  96. $query = self::find();
  97. $query->andWhere([
  98. 'is_delete' => 0,
  99. 'status' => 1,
  100. ]);
  101. if($store_id > -1){
  102. $query->andWhere(['store_id' => $store_id]);
  103. }
  104. $asArray && $query->asArray();
  105. $list = $query->all();
  106. return $list;
  107. }
  108. public static function passBaseRebate($store_id = 0, $goods_id = 0, $parent_id = 0) {
  109. $activity = self::activityAt($store_id, $goods_id);
  110. if($activity){
  111. if($activity->base_rebate == 0){
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. }