OrderCoinExchangeCollection.php 903 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace app\frontend\modules\order\coinExchange;
  3. use app\framework\Database\Eloquent\Collection;
  4. use app\frontend\models\order\PreOrderCoinExchange;
  5. class OrderCoinExchangeCollection extends Collection
  6. {
  7. public function addAndGroupByCode(PreOrderCoinExchange $orderCoinExchange)
  8. {
  9. if(!$this->sumByCode($orderCoinExchange)){
  10. // 不存在新建
  11. $this->add($orderCoinExchange);
  12. }
  13. return $this;
  14. }
  15. private function sumByCode(PreOrderCoinExchange $orderCoinExchange)
  16. {
  17. // 存在相同类型累加金额和数量
  18. foreach ($this->items as &$item) {
  19. if ($item['code'] == $orderCoinExchange->code) {
  20. $item['amount'] += $orderCoinExchange->amount;
  21. $item['coin'] += $orderCoinExchange->coin;
  22. return true;
  23. }
  24. }
  25. return false;
  26. }
  27. }