private function divide()
{
//计算昨日单份投资收益
$yesterday_per_profit = round(($this->yesterday_total_profit * ($this->project->profit_percent / 100) / $this->project->portion), 2);
$subscription = SubscriptionModel::where(['project_id' => $this->project->id])->select();
foreach ($subscription as $s){
//计算本月度内持续个人日收益总和,如果大于个人月收益最高总额,停止放发当月收益
$date_result = $this->caclPeriod($this->yesterday);
$begin_date = $date_result['begin_date'];
$end_date = $date_result['end_date'];
$sum = ProfitModel::where([
'project_id' => $this->project->id,
'investor_id' => $s->investor_id,
])->where('date', 'between time', [$begin_date, $end_date])->sum('actual_amount');
//昨日个人总收益 = 昨日单份投资收益 * 投资人购买的份数
$profit = round(($yesterday_per_profit * $s->copies), 2);
//未签到的总收益 = 昨日单份投资收益 * 投资人购买的份数 * 未签到的损失比例
if($this->project->personal_profit_lost_percent > 0){
$no_sign_profit = round($yesterday_per_profit * $s->copies * (1 - ($this->project->personal_profit_lost_percent / 100)), 2);
}else{
$no_sign_profit = $profit;
}
//获取投资者的支付宝用户
$user = UserModel::hasWhere('sign', ['date' => $this->yesterday])
->where(['User.investor_id' => $s->investor_id])
->find();
$profit_data = [
'project_id' => $this->project->id,
'investor_id' => $s->investor_id,
'date' => $this->yesterday,
'month' => date('Y-m', strtotime($this->yesterday)),
'year' => date('Y', strtotime($this->yesterday)),
'lost_percent' => $this->project->personal_profit_lost_percent,
];
//用户未签到或未绑定账号,收益要打折扣
if(empty($user) || empty($user->sign)){
$profit_data['has_sign'] = 0;
$profit_data['total_amount'] = $profit;
$profit_data['actual_amount'] = $no_sign_profit;
$profit_data['lost_amount'] = $profit - $no_sign_profit;
}else{
$profit_data['has_sign'] = 1;
$profit_data['total_amount'] = $profit;
$profit_data['actual_amount'] = $profit;
$profit_data['add_amount'] = $profit - $no_sign_profit;
}
Db::startTrans();
//保存收益数据以及增加累计收入
try{
$mth_limit = $s->mth_profit_limit_amount;
//如果设置了personal_max_profit 收益上限按这个算 否则按个人月收益上限算
if($this->project->personal_max_profit > 0){
$mth_limit = $this->project->personal_max_profit * 30 * $s->copies;
}
//本月度内持续个人日收益总和>=个人月收益上限,停止放发当月收益,到下个月开始之日再次开启收益
if($sum + $profit_data['actual_amount'] > $mth_limit){
$profit_data['lost_amount'] = 0;
$profit_data['actual_amount'] = $mth_limit - $sum;
$profit_data['has_mth_limit'] = 1;
$s->has_mth_limit = 1;
}
$s->cumulative_income += $profit_data['actual_amount'];
$s->save();
(new ProfitModel())->save($profit_data);
Db::commit();
}catch (\Exception $e){
Db::rollback();
Log::write('【增加收益时出错:】' . json_encode($profit_data, JSON_UNESCAPED_UNICODE . '【当前时间:】' . date('Y-m-d H:i:s')));
}
}
}
