AddonsRechargeActive.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace addons\Recharge\common\models;
  3. use addons\Recharge\common\enums\RechargeEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\common\RechargeOrders;
  6. use Yii;
  7. use yii\helpers\Json;
  8. /**
  9. * This is the model class for table "{{%addons_recharge_active}}".
  10. *
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property string $name 活动名称
  14. * @property int $start_at 开始时间
  15. * @property int $end_at 结束时间
  16. * @property int|null $already_give 已赠送人数
  17. * @property string $give_desc 赠送说明
  18. * @property int $give_cond 赠送条件 0/单次充值 1/累计充值(活动时间内)
  19. * @property string $give_info 赠送具体信息
  20. * @property int $active_status 0/未开始 1/进行中 2/已过期 3/已停用
  21. * @property int $status 状态[-1:删除;0:禁用;1启用]
  22. * @property int $created_at
  23. * @property int $updated_at
  24. */
  25. class AddonsRechargeActive extends \common\models\base\BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%addons_recharge_active}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id', 'start_at', 'end_at', 'already_give', 'give_cond', 'active_status', 'status', 'created_at', 'updated_at'], 'integer'],
  41. [['give_cond', 'give_info'], 'required'],
  42. [['give_info'], 'string'],
  43. [['name'], 'string', 'max' => 100],
  44. [['give_desc'], 'string', 'max' => 255],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => 'Mall ID',
  55. 'name' => '活动名称',
  56. 'start_at' => '开始时间',
  57. 'end_at' => '结束时间',
  58. 'already_give' => '已赠送人数',
  59. 'give_desc' => '赠送说明',
  60. 'give_cond' => '赠送条件 0/单次充值 1/累计充值(活动时间内)',
  61. 'give_info' => '赠送具体信息',
  62. 'active_status' => '0/未开始 1/进行中 2/已过期 3/已停用',
  63. 'status' => '状态[-1:删除;0:禁用;1启用]',
  64. 'created_at' => 'Created At',
  65. 'updated_at' => 'Updated At',
  66. ];
  67. }
  68. public static function setData($params, $mall_id): bool
  69. {
  70. try {
  71. if (isset($params['id']) && $params['id'] > 0) {
  72. $model = self::findOne(['id' => $params['id']]);
  73. } else {
  74. $model = new self();
  75. $model->mall_id = $mall_id;
  76. $model->loadDefaultValues();
  77. }
  78. $model->load($params, '');
  79. if (!$model->load($params, '') || !$model->save()) {
  80. throw new \Exception($model->getErrorMessage());
  81. }
  82. return true;
  83. } catch (\Exception $e) {
  84. self::$static_error = $e->getMessage();
  85. return false;
  86. }
  87. }
  88. public function getNowActive($mall_id)
  89. {
  90. $now = time();
  91. $where = [
  92. ['=', 'mall_id', $mall_id],
  93. ['<', 'start_at', $now],
  94. ['>', 'end_at', $now],
  95. ['=', 'active_status', RechargeEnum::ACTIVE_STATUS1],
  96. ['=', 'status', StatusEnum::ENABLED],
  97. ];
  98. $active_list = self::lists(['where' => $where], true);
  99. $data = [];
  100. if ($active_list) {
  101. foreach ($active_list as $key => $row) {
  102. $start_at[$key] = $row['start_at'];
  103. $create_at[$key] = $row['created_at'];
  104. }
  105. //start_at 优先 若相同 则根据created_at倒序
  106. array_multisort($start_at, SORT_DESC, $create_at, SORT_DESC, $active_list);
  107. $data = $active_list[0];
  108. $data['give_info'] = Json::decode($data['give_info']);
  109. }
  110. return $data;
  111. }
  112. public function getNowActiveList($mall_id)
  113. {
  114. $now = time();
  115. $where = [
  116. ['=', 'mall_id', $mall_id],
  117. ['<', 'start_at', $now],
  118. ['>', 'end_at', $now],
  119. ['=', 'active_status', RechargeEnum::ACTIVE_STATUS1],
  120. ['=', 'status', StatusEnum::ENABLED],
  121. ];
  122. $active_list = self::lists(['where' => $where,'order' => 'id desc'], true);
  123. if ($active_list) {
  124. foreach ($active_list as $key => $row) {
  125. $row['give_info'] = Json::decode($row['give_info']);
  126. $active_list[$key] = $row;
  127. }
  128. }
  129. return $active_list;
  130. }
  131. public static function check($params): bool
  132. {
  133. $order_no_arr = $params['order_no_arr'];
  134. $payment_order_list = $params['payment_order_list'];
  135. $counts = RechargeOrders::find()->where(['order_no'=>$order_no_arr,'is_pay'=>0])->count();
  136. if ($counts != count($payment_order_list)) {
  137. self::$static_error = '充值订单不一致';
  138. return false;
  139. }
  140. return true;
  141. }
  142. }