| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- /**
- * 未生成的订单商品类
- * Author: 芸众商城 www.yunzshop.com
- * Date: 2017/2/28
- * Time: 下午1:44
- */
- namespace app\common\modules\orderGoods\models;
- use app\common\exceptions\AppException;
- use app\common\models\Goods;
- use app\frontend\modules\deduction\OrderGoodsDeductionCollection;
- use app\frontend\modules\orderGoods\price\option\NormalOrderGoodsPrice;
- trait PreOrderGoodsTrait
- {
- protected $priceCalculator;
- /**
- * todo 去除对执行顺序的依赖
- * 获取生成前的模型属性
- * @return array
- * @deprecated
- */
- public function getPreAttributes()
- {
- $attributes = array(
- 'goods_id' => $this->goods->id,
- 'goods_sn' => $this->goods->goods_sn,
- 'title' => $this->goods->title,
- 'thumb' => yz_tomedia($this->goods->thumb),
- 'goods_price' => $this->getGoodsPrice(),
- 'price' => $this->getPrice(),
- 'goods_cost_price' => $this->getGoodsCostPrice(),
- 'goods_market_price' => $this->getGoodsMarketPrice(),
- 'need_address' => $this->goods->need_address,
- );
- if ($this->isOption()) {
- $attributes += [
- 'goods_option_id' => $this->goodsOption->id,
- 'goods_option_title' => $this->goodsOption->title,
- ];
- }
- $attributes = array_merge($this->getAttributes(), $attributes);
- return $attributes;
- }
- /**
- * @return Goods
- * @throws AppException
- */
- public function getGoods()
- {
- if (!isset($this->goods)) {
- throw new AppException('调用顺序错误,Goods对象还没有载入');
- }
- return $this->goods;
- }
- /**
- * @return int
- * @throws AppException
- */
- public function getThumbAttribute()
- {
- if ($this->goodsOption->thumb){
- return yz_tomedia($this->goodsOption->thumb);
- }
- return yz_tomedia($this->getGoods()->thumb);
- }
- public function getNeedAddressAttribute()
- {
- return $this->goods->need_address;
- }
- /**
- * @return int
- * @throws AppException
- */
- public function getTotalAttribute()
- {
- if (!isset($this->attributes['total'])) {
- throw new AppException("orderGoods:{$this->orderGoods->goods_id}的total为null");
- }
- if ($this->attributes['total'] <= 0) {
- throw new AppException("orderGoods:{$this->orderGoods->goods_id}的total为{$this->attributes['total']}");
- }
- return $this->attributes['total'];
- }
- /**
- * @return string
- * @throws AppException
- */
- public function getGoodsSnAttribute()
- {
- if ($this->goodsOption->goods_sn){
- return $this->goodsOption->goods_sn;
- }
- return $this->getGoods()->goods_sn;
- }
- /**
- * @return string
- * @throws AppException
- */
- public function getProductSnAttribute()
- {
- if ($this->goodsOption->product_sn){
- return $this->goodsOption->product_sn;
- }
- return $this->getGoods()->product_sn;
- }
- /**
- * @return string
- * @throws AppException
- */
- public function getTitleAttribute()
- {
- return $this->getGoods()->title;
- }
- /**
- * @return float
- */
- public function getPriceAttribute()
- {
- return $this->getPrice();
- }
- /**
- * @return float
- */
- public function getGoodsCostPriceAttribute()
- {
- return $this->getGoodsCostPrice();
- }
- /**
- * @return float
- */
- public function getGoodsMarketPriceAttribute()
- {
- return $this->getGoodsMarketPrice();
- }
- public function getGoodsOptionTitleAttribute()
- {
- return $this->isOption() ? $this->goodsOption->title : '';
- }
- /**
- * 获取利润
- * @return mixed
- */
- public function getGoodsCostPrice()
- {
- return $this->getPriceCalculator()->getGoodsCostPrice();
- }
- /**
- * 市场价
- * @return mixed
- */
- public function getGoodsMarketPrice()
- {
- return $this->getPriceCalculator()->getGoodsMarketPrice();
- }
- /**
- * 订单商品抵扣集合
- * @return OrderGoodsDeductionCollection
- */
- public function getOrderGoodsDeductions()
- {
- return $this->orderGoodsDeductions;
- }
- /**
- * 获取重量
- * @return mixed
- */
- public function getWeight()
- {
- if ($this->goodsOption->weight
- ) {
- return $this->goodsOption->weight;
- }
- return $this->goods->weight;
- }
- /**
- * 成交价格
- * @return mixed
- */
- public function getPrice()
- {
- return $this->getPriceCalculator()->getPrice();
- }
- /**
- * 原始价格
- * @return mixed
- */
- public function getGoodsPrice()
- {
- return $this->getPriceCalculator()->getGoodsPrice();
- }
- /**
- * 获取价格计算者
- * @return NormalOrderGoodsPrice
- */
- public function getPriceCalculator()
- {
- if (!isset($this->priceCalculator)) {
- $this->priceCalculator = $this->_getPriceCalculator();
- }
- return $this->priceCalculator;
- }
- }
|