PreOrderGoodsTrait.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * 未生成的订单商品类
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/2/28
  6. * Time: 下午1:44
  7. */
  8. namespace app\common\modules\orderGoods\models;
  9. use app\common\exceptions\AppException;
  10. use app\common\models\Goods;
  11. use app\frontend\modules\deduction\OrderGoodsDeductionCollection;
  12. use app\frontend\modules\orderGoods\price\option\NormalOrderGoodsPrice;
  13. trait PreOrderGoodsTrait
  14. {
  15. protected $priceCalculator;
  16. /**
  17. * todo 去除对执行顺序的依赖
  18. * 获取生成前的模型属性
  19. * @return array
  20. * @deprecated
  21. */
  22. public function getPreAttributes()
  23. {
  24. $attributes = array(
  25. 'goods_id' => $this->goods->id,
  26. 'goods_sn' => $this->goods->goods_sn,
  27. 'title' => $this->goods->title,
  28. 'thumb' => yz_tomedia($this->goods->thumb),
  29. 'goods_price' => $this->getGoodsPrice(),
  30. 'price' => $this->getPrice(),
  31. 'goods_cost_price' => $this->getGoodsCostPrice(),
  32. 'goods_market_price' => $this->getGoodsMarketPrice(),
  33. 'need_address' => $this->goods->need_address,
  34. );
  35. if ($this->isOption()) {
  36. $attributes += [
  37. 'goods_option_id' => $this->goodsOption->id,
  38. 'goods_option_title' => $this->goodsOption->title,
  39. ];
  40. }
  41. $attributes = array_merge($this->getAttributes(), $attributes);
  42. return $attributes;
  43. }
  44. /**
  45. * @return Goods
  46. * @throws AppException
  47. */
  48. public function getGoods()
  49. {
  50. if (!isset($this->goods)) {
  51. throw new AppException('调用顺序错误,Goods对象还没有载入');
  52. }
  53. return $this->goods;
  54. }
  55. /**
  56. * @return int
  57. * @throws AppException
  58. */
  59. public function getThumbAttribute()
  60. {
  61. if ($this->goodsOption->thumb){
  62. return yz_tomedia($this->goodsOption->thumb);
  63. }
  64. return yz_tomedia($this->getGoods()->thumb);
  65. }
  66. public function getNeedAddressAttribute()
  67. {
  68. return $this->goods->need_address;
  69. }
  70. /**
  71. * @return int
  72. * @throws AppException
  73. */
  74. public function getTotalAttribute()
  75. {
  76. if (!isset($this->attributes['total'])) {
  77. throw new AppException("orderGoods:{$this->orderGoods->goods_id}的total为null");
  78. }
  79. if ($this->attributes['total'] <= 0) {
  80. throw new AppException("orderGoods:{$this->orderGoods->goods_id}的total为{$this->attributes['total']}");
  81. }
  82. return $this->attributes['total'];
  83. }
  84. /**
  85. * @return string
  86. * @throws AppException
  87. */
  88. public function getGoodsSnAttribute()
  89. {
  90. if ($this->goodsOption->goods_sn){
  91. return $this->goodsOption->goods_sn;
  92. }
  93. return $this->getGoods()->goods_sn;
  94. }
  95. /**
  96. * @return string
  97. * @throws AppException
  98. */
  99. public function getProductSnAttribute()
  100. {
  101. if ($this->goodsOption->product_sn){
  102. return $this->goodsOption->product_sn;
  103. }
  104. return $this->getGoods()->product_sn;
  105. }
  106. /**
  107. * @return string
  108. * @throws AppException
  109. */
  110. public function getTitleAttribute()
  111. {
  112. return $this->getGoods()->title;
  113. }
  114. /**
  115. * @return float
  116. */
  117. public function getPriceAttribute()
  118. {
  119. return $this->getPrice();
  120. }
  121. /**
  122. * @return float
  123. */
  124. public function getGoodsCostPriceAttribute()
  125. {
  126. return $this->getGoodsCostPrice();
  127. }
  128. /**
  129. * @return float
  130. */
  131. public function getGoodsMarketPriceAttribute()
  132. {
  133. return $this->getGoodsMarketPrice();
  134. }
  135. public function getGoodsOptionTitleAttribute()
  136. {
  137. return $this->isOption() ? $this->goodsOption->title : '';
  138. }
  139. /**
  140. * 获取利润
  141. * @return mixed
  142. */
  143. public function getGoodsCostPrice()
  144. {
  145. return $this->getPriceCalculator()->getGoodsCostPrice();
  146. }
  147. /**
  148. * 市场价
  149. * @return mixed
  150. */
  151. public function getGoodsMarketPrice()
  152. {
  153. return $this->getPriceCalculator()->getGoodsMarketPrice();
  154. }
  155. /**
  156. * 订单商品抵扣集合
  157. * @return OrderGoodsDeductionCollection
  158. */
  159. public function getOrderGoodsDeductions()
  160. {
  161. return $this->orderGoodsDeductions;
  162. }
  163. /**
  164. * 获取重量
  165. * @return mixed
  166. */
  167. public function getWeight()
  168. {
  169. if ($this->goodsOption->weight
  170. ) {
  171. return $this->goodsOption->weight;
  172. }
  173. return $this->goods->weight;
  174. }
  175. /**
  176. * 成交价格
  177. * @return mixed
  178. */
  179. public function getPrice()
  180. {
  181. return $this->getPriceCalculator()->getPrice();
  182. }
  183. /**
  184. * 原始价格
  185. * @return mixed
  186. */
  187. public function getGoodsPrice()
  188. {
  189. return $this->getPriceCalculator()->getGoodsPrice();
  190. }
  191. /**
  192. * 获取价格计算者
  193. * @return NormalOrderGoodsPrice
  194. */
  195. public function getPriceCalculator()
  196. {
  197. if (!isset($this->priceCalculator)) {
  198. $this->priceCalculator = $this->_getPriceCalculator();
  199. }
  200. return $this->priceCalculator;
  201. }
  202. }