services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic'); $award_proportion_settlement_type = $configs['award_proportion_settlement_type'] ?? 0; $is_upgrade_bag_give_goods = $configs['is_upgrade_bag_give_goods'] ?? 0; $is_equal_2_step = $configs['is_equal_2_step'] ?? 0; $award_proportion_reward_type = $configs['award_proportion_reward_type'] ?? 0; //清零方式 $award_proportion_clear_type = $configs['award_proportion_clear_type'] ?? 0; //计算方式 $award_proportion_calculate_type = $configs['award_proportion_calculate_type'] ?? 0; $cloud_stock_level_arr = []; $cloud_stock_bonus_level_arr = []; $award_proportion = []; if(empty($configs['team_reward_goods_id'])){ $team_reward_goods_id = []; }else{ $team_reward_goods_id = json_decode($configs['team_reward_goods_id'],true); } if (!empty($configs['award_proportion'])) { $award_proportion = json_decode($configs['award_proportion'], true); } if (!empty($configs['cloud_stock_level_arr'])) { $cloud_stock_level_arr = json_decode($configs['cloud_stock_level_arr'], true); } if (!empty($configs['cloud_stock_bonus_level_arr'])) { $cloud_stock_bonus_level_arr = json_decode($configs['cloud_stock_bonus_level_arr'], true); } $day = date('d', $time); if ($day != '01') return false; switch ($award_proportion_settlement_type) { case 0: if(!isset($start)||!isset($end)){ $res = DateHelper::lastMonth(); $start = $res['start']; $end = $res['end']; } break; case 1: $month = date('m', $time); if(!in_array($month,['01','04','07','10'])){ return false; } if(!isset($start)||!isset($end)) { $res = DateHelper::lastSeason(); $start = $res['start']; $end = $res['end']; } break; // case 2: // if(!isset($start)||!isset($end)){ // $res = DateHelper::lastYear(); // $start = $res['start']; // $end = $res['end']; // } // break; } if (empty($start) || empty($end)) return false; if($award_proportion_reward_type==1){ if($award_proportion_calculate_type == 1){ $select = 'user_id,sum(num * fill_unit_price) as num'; }else{ $select = 'user_id,sum(num) as num'; } $changes_type = [CloudStockEnum::CHANGE_TYPE_DEDUTION_FILL]; if ($is_upgrade_bag_give_goods) $changes_type[] = CloudStockEnum::STOCK_CHANGE_TYPE105; $list = CloudStockIncreaseByPlatformLog::lists([ 'where' => [ ['>=', 'created_at', $start], ['<=', 'created_at', $end], ['changes_type' => $changes_type], ['status' => StatusEnum::ENABLED], ['mall_id' => $mall_id], ['goods_id' => $team_reward_goods_id], ], 'group' => 'user_id', 'select' => $select, 'index' => 'user_id', ]); }else{ $changes_type = [CloudStockEnum::STOCK_CHANGE_TYPE100]; if ($is_upgrade_bag_give_goods) $changes_type[] = CloudStockEnum::STOCK_CHANGE_TYPE105; $list = CloudStockIncreaseDetail::lists([ 'where' => [ ['>=', 'created_at', $start], ['<=', 'created_at', $end], ['changes_type' => $changes_type], ['status' => StatusEnum::ENABLED], ['mall_id' => $mall_id], ['goods_id' => $team_reward_goods_id], ], 'group' => 'user_id', 'select' => 'user_id,sum(num) as num', 'index' => 'user_id', ]); } if (empty($list)) return false; //清零时间 $clear_time = false; $year_start = null; $year_end = null; if($award_proportion_reward_type == 1){ switch ($award_proportion_clear_type) { case 0: $year = date("Y",$time); $year_before = date("Y",$start); $month = date('m', $time); if($year != $year_before && $month != '01') $clear_time = true; $year_start = strtotime($year_before.'-01-01 00:00:00'); $year_end = strtotime($year_before.'-12-31 23:59:59'); break; } } $t = \Yii::$app->db->beginTransaction(); try { $user_ids = array_column($list, 'user_id'); $agent_list = CloudStockAgent::lists([ 'where' => [ ['status' => StatusEnum::ENABLED], ['mall_id' => $mall_id], ['user_id' => $user_ids], ], 'index' => 'user_id', ]); $bonus_pool_agent_list = CloudStockBonusPoolUser::lists([ 'where' => [ ['status' => StatusEnum::ENABLED], ['mall_id' => $mall_id], ['user_id' => $user_ids], ], 'index' => 'user_id', ]); $direct_list = []; $indirect_list = []; if (!empty($is_equal_2_step)) { $user_list = User::lists([ 'where' => [ ['status' => StatusEnum::ENABLED], ['mall_id' => $mall_id], ['id' => $user_ids], ], ]); foreach ($user_list as $value) { $direct_list[$value['parent_id']][] = $value['id']; $indirect_list[$value['second_parent_id']][] = $value['id']; } } foreach ($list as $item) { if(empty($agent_list[$item['user_id']])) continue; if(!in_array($agent_list[$item['user_id']]['level'],$cloud_stock_level_arr)) continue; if(!empty($cloud_stock_bonus_level_arr)){ if(empty($bonus_pool_agent_list[$item['user_id']])) continue; if(!in_array($bonus_pool_agent_list[$item['user_id']]['level'],$cloud_stock_bonus_level_arr)) continue; } $model = CloudStockFillStatistics::findOne([ 'mall_id' => $mall_id, 'user_id' => $item['user_id'], 'start' => $start, 'end' => $end, ]); if (!empty($model)) continue; $self_num = $item['num']; $direct_num = 0; $indirect_num = 0; $direct_money = 0; $indirect_money = 0; if (isset($direct_list[$item['user_id']])) { foreach ($direct_list[$item['user_id']] as $user_id) { if (empty($agent_list[$item['user_id']]) || empty($agent_list[$user_id])) { continue; } if ($agent_list[$item['user_id']]['level'] != $agent_list[$user_id]['level']) continue; $temp_num = $list[$user_id]['num'] ?? 0; $temp_money = self::getRewardMoney($temp_num, $award_proportion,$award_proportion_reward_type,$user_id,$award_proportion_calculate_type,$clear_time,$year_start,$year_end); $direct_money += $temp_money; $direct_num += $temp_num; } } if (isset($indirect_list[$item['user_id']])) { foreach ($indirect_list[$item['user_id']] as $user_id) { if (empty($agent_list[$item['user_id']]) || empty($agent_list[$user_id])) { continue; } if ($agent_list[$item['user_id']]['level'] != $agent_list[$user_id]['level']) continue; $temp_num = $list[$user_id]['num'] ?? 0; $temp_money = self::getRewardMoney($temp_num, $award_proportion,$award_proportion_reward_type,$user_id,$award_proportion_calculate_type,$clear_time,$year_start,$year_end); $indirect_money += $temp_money; $indirect_num += $temp_num; } } $total_num = $self_num + $direct_num + $indirect_num; $money = self::getRewardMoney($total_num, $award_proportion,$award_proportion_reward_type,$item['user_id'],$award_proportion_calculate_type,$clear_time,$year_start,$year_end); $money = $money - $direct_money - $indirect_money; $add_data = [ 'mall_id' => $mall_id, 'user_id' => $item['user_id'], 'level' => $agent_list[$item['user_id']]['level']??0, 'self_num' => $self_num, 'direct_num' => $direct_num, 'indirect_num' => $indirect_num, 'start' => $start, 'end' => $end, 'money' => $money, 'award_proportion_settlement_type' => $award_proportion_settlement_type, 'is_upgrade_bag_give_goods' => $is_upgrade_bag_give_goods, 'is_equal_2_step' => $is_equal_2_step, 'award_proportion' => json_encode($award_proportion), 'award_proportion_calculate_type' => $award_proportion_calculate_type, ]; $model = CloudStockFillStatistics::setData($add_data); if ($money > 0) { $money = bcadd($money, 0, 2); $insert_wallet[] = ['mall_id' => $mall_id, 'user_id' => $item['user_id'], 'is_frozen' => false, 'wallet_type' => 'commission', 'money' => $money, 'desc' => '云仓个人团队补货奖励', 'source_table' => CloudStockFillStatistics::tableName(), 'source_table_id' => $model['id'] ?? 0, 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'capital_type' => UserWalletEnum::CLOUD_STOCK_FILL_ACHIEVEMENT, ]; } } $t->commit(); if (!empty($insert_wallet)){ // var_dump('TeamReward:'.json_encode($insert_wallet)); CloudStockAgent::sendUserWalletQueue($insert_wallet); } return true; } catch (\Exception $e) { $t->rollBack(); var_dump("TeamReward::index" . $e->getMessage().$e->getLine().$e->getFile()); return false; } } protected static function getRewardMoney($num, $award_proportion,$award_proportion_reward_type,$user_id,$award_proportion_calculate_type,$clear_time,$year_start,$year_end) { $column = array_column($award_proportion, 'num'); array_multisort($column, SORT_DESC, $award_proportion);//升序 $money = 0;//奖励金额 //结算数量 $settle_num = $num; if(!empty($award_proportion_reward_type) && !$clear_time){ $before = CloudStockFillStatistics::find() ->select('sum(self_num + direct_num + indirect_num) as num') ->where(['user_id'=>$user_id,'award_proportion_calculate_type'=>$award_proportion_calculate_type]) ->andWhere(['>=','start',$year_start]) ->andWhere(['<=','end',$year_end]) ->asArray() ->one(); if(!empty($before['num'])){ $settle_num += $before['num']; } } foreach ($award_proportion as $k => $v) { if ($settle_num >= $v['num']) { $money = $v['money']; break; } } if($award_proportion_calculate_type == 1){ $money = $money / 100; } return $money*$num; } }