| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- /**
- * Created by PhpStorm.
- * User: 宝佳
- * Date: 2018/1/17
- * Time: 16:24
- */
- namespace app\common\services\finance;
- use app\common\events\order\AfterOrderReceivedEvent;
- use app\common\exceptions\ShopException;
- use app\common\services\credit\ConstService;
- class BalanceAwardService
- {
- private $orderModel;
- public function awardBalance(AfterOrderReceivedEvent $event)
- {
- $this->orderModel = $event->getOrderModel();
- $change_value = $this->getChangeValue();
- if ($change_value > 0) {
- $data = $this->getChangeData();
- $data['change_value'] = $change_value;
- $result = (new BalanceChange())->award($data);
- if ($result !== true) {
- throw new ShopException('购物赠送余额失败,请重试!');
- }
- }
- }
- private function getChangeData()
- {
- return [
- 'member_id' => $this->getUid(),
- 'remark' => "购物赠送余额",
- 'relation' => $this->orderModel->order_sn,
- 'operator' => ConstService::OPERATOR_MEMBER,
- 'operator_id' => $this->orderModel->uid,
- //'change_value' => $this->getChangeValue(),
- ];
- }
- private function getChangeValue()
- {
- $orderGoods = $this->orderModel->hasManyOrderGoods;
- $change_value = 0;
- foreach ($orderGoods as $goodsModel)
- {
- $goodsSaleModel = $goodsModel->hasOneGoods->hasOneSale;
- if (!$goodsSaleModel || empty($goodsSaleModel->award_balance)) {
- continue;
- }
- $change_value += $this->proportionMath($goodsModel->payment_amount, $goodsSaleModel->award_balance, $goodsModel->total);
- }
- return $change_value;
- }
- private function getUid()
- {
- $orderGoods = $this->orderModel->hasManyOrderGoods;
- foreach ($orderGoods as $goodsModel)
- {
- $uid = $goodsModel->uid ?: $this->orderModel->uid;
- }
- return $uid;
- }
- private function proportionMath($price, $proportion, $total)
- {
- if (strexists($proportion, '%')) {
- $proportion = str_replace('%', '', $proportion);
- return bcdiv(bcmul($price,$proportion,4),100,2);
- }
- return bcmul($proportion,$total,2);
- }
- }
|