祝你有个美好的一天,我想问一下我能在2天前解决这个问题,我将数据导入数据库时遇到问题
示例:
我的csv文件中有这样的条目,我将导入
||=============================================|| || wholesaler || product || week_01 || week_02 || ||=============================================|| || Michael || Tomato || 90.000 || 60.000 || || Michael || Apple || 50.000 || 50.000 || || Santi || Mango || 23.000 || 32.000 || ||=============================================||
当我导入该文件时,一切正常,我在数据库中检查它看起来像这样
||==================================================================================|| || wholesaler || target || period || total_transaction || rebate || total_voucher || ||==================================================================================|| || Michael || 100.000 || 01.2017 || 140.000 || 2% || 2800 || || Michael || 100.000 || 02.2017 || 250.000 || 2% || 2200 || || Santi || 100.000 || 01.2017 || 23.000 || 2% || 0 || || Santi || 100.000 || 02.2017 || 55.000 || 2% || 0 || ||==================================================================================||
但是当我像这样添加新文件
时||=============================================|| || wholesaler || product || week_01 || week_02 || ||=============================================|| || Michael || Tomato || 90.000 || 60.000 || || Michael || Apple || 50.000 || 50.000 || || Santi || Mango || 23.000 || 32.000 || || Santi || Apple || 11.000 || 33.000 ||----> NEW ENTRY ||=============================================||
我导入文件,然后数据库中出现错误
||=====================================================================================|| || wholesaler || target || period || total_transaction || rebate || total_voucher || ||=====================================================================================|| || Michael || 100.000 || 01.2017 || 140.000 || 2% || 2800 || || Michael || 100.000 || 02.2017 || 250.000 || 2% || 2200 || || Santi || 100.000 || 01.2017 || 23.000 || 2% || 0 || || Santi || 100.000 || 02.2017 || 55.000 || 2% || 0 || || Santi || 100.000 || 01.2017 || 34.000 || 2% || 0 ||--> this total_transaction is correct, base on sum 23.000 + 11.000 || Santi || 100.000 || 02.2017 || 78.000 || 2% || 0 ||--> but this is not correct ||=====================================================================================||
必须是这样的:
||=====================================================================================|| || wholesaler || target || period || total_transaction || rebate || total_voucher || ||=====================================================================================|| || Michael || 100.000 || 01.2017 || 140.000 || 2% || 2800 || || Michael || 100.000 || 02.2017 || 250.000 || 2% || 2200 || || Santi || 100.000 || 01.2017 || 34.000 || 2% || 0 ||--> sum week_01 || Santi || 100.000 || 02.2017 || 99.000 || 2% || 0 ||--> sum week_01+sum week_02 ||=====================================================================================||
这是Controller中的代码:
$columns = Schema::getColumnListing('leveredge');
$now = $request->input('week');
for($x = 13; $x <= $now + 12; $x++)
{
$total_transaction = Leveredge::groupBy('distributor', 'outlet')->selectRaw('distributor, outlet, sum(week_'. str_pad($x-12, 2, '0', STR_PAD_LEFT) .') AS sum')->get();
$y = 0;
foreach($total_transaction as $row)
{
$wholesaler_id = $row->outlet;
$wholesaler = Wholesaler::find($wholesaler_id);
$target = '';
if( $wholesaler && ($wholesaler->total_target != -1 && $wholesaler->q1 != -1 && $wholesaler->q2 != -1 && $wholesaler->q3 != -1 && $wholesaler->q4 != -1) )
{
$wholesaler_type = WholesalerType::find($wholesaler->wholesaler_type_id);
$week = substr($columns[$x], -2);
if($week <= 13){
$target = is_null($wholesaler->q1)? 0 : $wholesaler->q1;
}else if($week <= 26 && $week >= 14){
$target = is_null($wholesaler->q2)? 0 : $wholesaler->q2;
}else if($week <= 39 && $week >= 27){
$target = is_null($wholesaler->q3)? 0 : $wholesaler->q3;
}else if($week >= 28){
$target = is_null($wholesaler->q4)? 0 : $wholesaler->q4;
}else{
$target = 0;
}
$match = ['wholesaler_id' => $wholesaler_id, 'week' => str_pad($week-1, 2, '0', STR_PAD_LEFT) . '.' . date("Y")];
$last_transaction = Voucher::where($match)->first();
$curr_sum = $row->sum;
$sum = $row->sum;
$total_voucher = 0;
if(!in_array($week, [1, 14, 27, 52])){
$sum += $last_transaction->total_transaction;
if($sum >= $target){
if($last_transaction->total_transaction == $sum){
$total_voucher = 0;
}else if($last_transaction->total_voucher == 0){
$total_voucher = ($wholesaler_type->rebate_percentage/100 * $sum);
}else{
$total_voucher = ($wholesaler_type->rebate_percentage/100 * $curr_sum);
}
}
}else{
if($sum >= $target){
$total_voucher = ($wholesaler_type->rebate_percentage/100 * $curr_sum);
}else{
$total_voucher = 0;
}
}
$voucher = Voucher::firstOrCreate(array(
'wholesaler_id' => $wholesaler_id,
'target' => $target,
'week' => $week . '.' . date("Y"),
'total_transaction' => $sum,
'rebate' => $wholesaler_type->rebate_percentage,
'total_voucher' => $total_voucher
));
$leveredgenotification = LeveredgeNotification::firstOrCreate(array(
'wholesaler_id' => $voucher->wholesaler_id,
'value' => $voucher->total_voucher,
'target' => '0',
'type' => 'cashback_voucher',
'status' => 99,
));
}
$y++;
}
}
我希望你能帮忙解决这个问题,
非常感谢你。
祝你有愉快的一天。
答案 0 :(得分:1)
finnaly它解决了:
我修改firstorCreate是这样的:
$voucher = Voucher::firstOrNew(array(
'wholesaler_id' => $wholesaler_id,
'target' => $target,
'week' => $week . '.' . date("Y")
));
$voucher->wholesaler_id = $wholesaler_id;
$voucher->target = $target;
$voucher->week = $week . '.' . date("Y");
$voucher->total_transaction = $sum;
$voucher->rebate = $wholesaler_type->rebate_percentage;
$voucher->total_voucher = $total_voucher;
$voucher->save();
谢谢大家
答案 1 :(得分:0)
在查询中使用->groupBy('wholesaler')
。
Wholesaler::find($wholesaler_id)->groupBy('wholesaler');