admin/app/frontend/modules/deduction/OrderGoodsDeductManager.php

111 lines
3.3 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: shenyang
* Date: 2019/1/9
* Time: 10:50 AM
*/
namespace app\frontend\modules\deduction;
use app\common\modules\orderGoods\models\PreOrderGoods;
use app\framework\Database\Eloquent\Collection;
use app\frontend\modules\deduction\models\Deduction;
use app\frontend\modules\deduction\orderGoods\PreOrderGoodsDeduction;
class OrderGoodsDeductManager
{
/**
* @var PreOrderGoods
*/
private $orderGoods;
/**
* @var OrderGoodsDeductionCollection
*/
private $orderGoodsDeductions;
public function __construct(PreOrderGoods $orderGoods)
{
$this->orderGoods = $orderGoods;
}
/**
* @return OrderGoodsDeductionCollection
*/
public function getOrderGoodsDeductions()
{
if (!isset($this->orderGoodsDeductions)) {
$this->orderGoodsDeductions = $this->_getOrderGoodsDeductions();
}
return $this->orderGoodsDeductions;
}
/**
* 获取并订单抵扣项并载入到订单模型中
* @return OrderGoodsDeductionCollection
*/
private function _getOrderGoodsDeductions()
{
$orderDeductions = $this->getEnableDeductions()->map(function (Deduction $deduction) {
if (app('CoinManager')->bound('pre_goods_'.$deduction->getCode())) {
$preOrderGoodsDeduction = app('CoinManager')->make('pre_goods_'.$deduction->getCode(),[
'name' => $deduction->getName(),
'code' => $deduction->getCode()
]);
} else {
$preOrderGoodsDeduction = new PreOrderGoodsDeduction([
'name' => $deduction->getName(),
'code' => $deduction->getCode()
]);
}
$preOrderGoodsDeduction->setOrderGoods($this->orderGoods);
return $preOrderGoodsDeduction;
});
return new OrderGoodsDeductionCollection($orderDeductions->all());
}
/**
* 开启的抵扣项
* @return Collection
*/
private function getEnableDeductions()
{
//由于获取开启抵扣都是相同的所以这里把这部分代码提取出来
return \app\frontend\modules\deduction\EnableDeductionService::getInstance()->getEnableDeductions($this->orderGoods->order);
//blank not deduction
if ($this->orderGoods->order->isDeductionDisable()) {
trace_log()->deduction('订单商品关闭的抵扣类型','');
return collect();
}
/**
* 商城开启的抵扣
* @var Collection $deductions
*/
$deductions = Deduction::where('enable', 1)->get();
trace_log()->deduction('订单商品开启的抵扣类型', $deductions->pluck('code')->toJson());
if ($deductions->isEmpty()) {
return collect();
}
// 过滤调无效的
$deductions = $deductions->filter(function (Deduction $deduction) {
/**
* @var Deduction $deduction
*/
return $deduction->valid();
});
// 按照用户勾选顺序排序
$sort = array_flip($this->orderGoods->order->getParams('deduction_ids'));
$deductions = $deductions->sortBy(function ($deduction) use ($sort) {
return array_get($sort, $deduction->code, 999);
});
return $deductions;
}
}