Example #1
0
 /**
  * 发布资金活动
  * @return boolean
  */
 public function publish()
 {
     //生成随机数据
     $total = $this->gift_price;
     //红包总金额
     $num = $this->gift_nums;
     // 分成10个红包,支持10人随机领取
     $moneyArray = [];
     $time = time();
     $addip = \Yii::$app->request->userIP;
     for ($i = 0; $i < $num; $i++) {
         $money = $this->gift_price;
         $moneyArray[$i] = [0, $this->activity_id, $this->gift_name, $money, 0, $time, 0, 0, $addip];
     }
     \Yii::$app->db->createCommand()->batchInsert(Gift::tableName(), ['user_id', 'activity_id', 'gift_name', 'gift_price', 'gift_status', 'addtime', 'updatetime', 'fittime', 'addip'], $moneyArray)->execute();
     Activity::updateAll(['ac_status' => 1], 'id=' . $this->activity_id);
     return true;
 }
Example #2
0
 /**
  * 发布资金活动
  * @return boolean
  */
 public function publish()
 {
     //生成随机数据
     $total = $this->gift_price;
     //红包总金额
     $num = $this->gift_nums;
     // 分成10个红包,支持10人随机领取
     $min = $this->gift_min;
     //每个人最少能收到0.01元
     $max = $this->gift_max;
     $moneyArray = [];
     $time = time();
     $addip = \Yii::$app->request->userIP;
     for ($i = 1; $i < $num; $i++) {
         $safe_total = ($total - ($num - $i) * $min) / ($num - $i);
         //随机安全上限
         if ($max < $safe_total) {
             $safe_total = $max;
         }
         $money = mt_rand($min * 100, $safe_total * 100) / 100;
         $moneyArray[$i] = [0, $this->activity_id, $this->gift_name, $money, 0, $time, 0, 0, $addip];
         $total = $total - $money;
     }
     $moneyArray[$num] = [0, $this->activity_id, $this->gift_name, $total, 0, $time, 0, 0, $addip];
     \Yii::$app->db->createCommand()->batchInsert(Gift::tableName(), ['user_id', 'activity_id', 'gift_name', 'gift_price', 'gift_status', 'addtime', 'updatetime', 'fittime', 'addip'], $moneyArray)->execute();
     Activity::updateAll(['ac_status' => 1], 'id=' . $this->activity_id);
     return true;
 }