Laravel 5 如何更新最新数据并获取数据库的真实累积和
Posted
技术标签:
【中文标题】Laravel 5 如何更新最新数据并获取数据库的真实累积和【英文标题】:Laravel5 How to Update latest data and get true accumulative sum to database 【发布时间】:2017-10-30 09:38:04 【问题描述】:你好,我想问一些我两天前无法解决的问题,当我将数据导入数据库时遇到问题
示例:
我的 csv 文件中有这样的条目,我将导入
||===============================================|| || 批发商||产品 ||周_01 ||周_02 || ||===============================================|| ||迈克尔 ||番茄 || 90.000 || 60.000 || ||迈克尔 ||苹果|| 50.000 || 50.000 || ||桑蒂 ||芒果 || 23.000 || 32.000 || ||===============================================||当我导入那个文件时,一切正常,我在数据库中检查它会是这样的
||================================================= ====================================|| || 批发商||目标 ||期间 ||总交易 ||返利 || total_voucher || ||================================================= ====================================|| ||迈克尔 || 100.000 || 01.2017 || 140.000 || 2% || 2800 || ||迈克尔 || 100.000 || 02.2017 || 250.000 || 2% || 2200 || ||桑蒂 || 100.000 || 01.2017 || 23.000 || 2% || 0 || ||桑蒂 || 100.000 || 02.2017 || 55.000 || 2% || 0 || ||================================================= ====================================||但是当我像这样向我的文件添加新条目时
||===============================================|| || 批发商||产品 ||周_01 ||周_02 || ||===============================================|| ||迈克尔 ||番茄 || 90.000 || 60.000 || ||迈克尔 ||苹果|| 50.000 || 50.000 || ||桑蒂 ||芒果 || 23.000 || 32.000 || ||桑蒂 ||苹果|| 11.000 || 33.000 ||----> 新条目 ||===============================================||我导入文件,然后数据库出现错误
||================================================= ======================================|| || 批发商||目标 ||期间 ||总交易 ||返利 || total_voucher || ||================================================= ======================================|| ||迈克尔 || 100.000 || 01.2017 || 140.000 || 2% || 2800 || ||迈克尔 || 100.000 || 02.2017 || 250.000 || 2% || 2200 || ||桑蒂 || 100.000 || 01.2017 || 23.000 || 2% || 0 || ||桑蒂 || 100.000 || 02.2017 || 55.000 || 2% || 0 || ||桑蒂 || 100.000 || 01.2017 || 34.000 || 2% || 0 ||--> 这个total_transaction是正确的,基于总和23.000 + 11.000 ||桑蒂 || 100.000 || 02.2017 || 78.000 || 2% || 0 ||--> 但这不正确 ||================================================= ======================================||一定是这样的:
||================================================= ======================================|| || 批发商||目标 ||期间 ||总交易 ||返利 || total_voucher || ||================================================= ======================================|| ||迈克尔 || 100.000 || 01.2017 || 140.000 || 2% || 2800 || ||迈克尔 || 100.000 || 02.2017 || 250.000 || 2% || 2200 || ||桑蒂 || 100.000 || 01.2017 || 34.000 || 2% || 0 ||--> sum week_01 ||桑蒂 || 100.000 || 02.2017 || 99.000 || 2% || 0 ||--> sum week_01+sum week_02 ||================================================= ======================================||这是控制器中的代码:
$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++;
我希望你能帮助解决这个问题, 非常感谢。 祝你有美好的一天。
【问题讨论】:
您可能需要区分wholesaler
和 period
以删除重复记录
我希望 total_transaction 列是正确的累积和并更新数据库,而不是创建:)
to not get duplicated data
嗯,对不起,也许我必须更改标题
【参考方案1】:
在您的查询中使用->groupBy('wholesaler')
。
示例:
Wholesaler::find($wholesaler_id)->groupBy('wholesaler');
【讨论】:
导入时会出现错误:Undefined property: Illuminate\Database\Eloquent\Builder::$total_target @FaisalHilmi 你能告诉我们发生错误的那一行吗? @RahmaniSaif 没有错误,但问题是它在数据库中创建新条目,我想更新 :)【参考方案2】:终于解决了: 我将 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();
谢谢大家
【讨论】:
以上是关于Laravel 5 如何更新最新数据并获取数据库的真实累积和的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Laravel 5.5 中将数据插入表格并获取电子邮件通知?