OrderGoods.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. /**
  6. * 模型
  7. */
  8. class OrderGoods extends Model
  9. {
  10. // 表名
  11. protected $name = 'shop_order_goods';
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = '';
  17. // 追加属性
  18. protected $append = [
  19. 'url'
  20. ];
  21. protected static $config = [];
  22. protected static $tagCount = 0;
  23. protected static function init()
  24. {
  25. $config = get_addon_config('shop');
  26. self::$config = $config;
  27. }
  28. public function getUrlAttr($value, $data)
  29. {
  30. $suffix = static::$config['moduleurlsuffix']['goods'] ?? static::$config['urlsuffix'];
  31. return $this->goods ? $this->goods->url : addon_url('shop/goods/index', ['id' => $data['goods_id']], $suffix);
  32. }
  33. public function getFullurlAttr($value, $data)
  34. {
  35. $suffix = static::$config['moduleurlsuffix']['goods'] ?? static::$config['urlsuffix'];
  36. return $this->goods ? $this->goods->fullurl : addon_url('shop/goods/index', ['id' => $data['goods_id']], $suffix, true);
  37. }
  38. public function getImageAttr($value, $data)
  39. {
  40. $value = $value ?: ($data['image'] ?? '/assets/addons/shop/img/noimage.jpg');
  41. return cdnurl($value, true);
  42. }
  43. //销量增
  44. public static function setGoodsSalesInc($order_sn)
  45. {
  46. $list = (new self)->where('order_sn', $order_sn)->select();
  47. // 启动事务
  48. Db::startTrans();
  49. try {
  50. foreach ($list as $item) {
  51. $goods = $item->goods;
  52. $sku = $item->sku;
  53. if ($goods) {
  54. $goods->setInc('sales', $item->nums);
  55. }
  56. if ($sku) {
  57. $sku->setInc('sales', $item->nums);
  58. }
  59. }
  60. // 提交事务
  61. Db::commit();
  62. } catch (\Exception $e) {
  63. // 回滚事务
  64. Db::rollback();
  65. }
  66. return true;
  67. }
  68. //销量减
  69. public static function setGoodsSalesDec($order_sn)
  70. {
  71. $list = (new self)->where('order_sn', $order_sn)->select();
  72. // 启动事务
  73. Db::startTrans();
  74. try {
  75. foreach ($list as $item) {
  76. $goods = $item->goods;
  77. $sku = $item->sku;
  78. if ($goods) {
  79. $goods->setDec('sales', $item->nums);
  80. }
  81. if ($sku) {
  82. $sku->setDec('sales', $item->nums);
  83. }
  84. }
  85. // 提交事务
  86. Db::commit();
  87. } catch (\Exception $e) {
  88. // 回滚事务
  89. Db::rollback();
  90. }
  91. return true;
  92. }
  93. //库存增
  94. public static function setGoodsStocksInc($order_sn)
  95. {
  96. $list = (new self)->where('order_sn', $order_sn)->select();
  97. // 启动事务
  98. Db::startTrans();
  99. try {
  100. foreach ($list as $item) {
  101. $goods = $item->goods;
  102. $sku = $item->sku;
  103. if ($sku) {
  104. $sku->setInc('stocks', $item->nums);
  105. }
  106. if ($goods) {
  107. $goods->setInc('stocks', $item->nums);
  108. }
  109. }
  110. // 提交事务
  111. Db::commit();
  112. } catch (\Exception $e) {
  113. // 回滚事务
  114. Db::rollback();
  115. }
  116. return true;
  117. }
  118. //库存减
  119. public static function setGoodsStocksDec($order_sn)
  120. {
  121. $list = (new self)->where('order_sn', $order_sn)->select();
  122. // 启动事务
  123. Db::startTrans();
  124. try {
  125. foreach ($list as $item) {
  126. $goods = $item->goods;
  127. $sku = $item->sku;
  128. if ($sku) {
  129. $sku->setDec('stocks', $item->nums);
  130. }
  131. if ($goods) {
  132. $goods->setDec('stocks', $item->nums);
  133. }
  134. }
  135. // 提交事务
  136. Db::commit();
  137. } catch (\Exception $e) {
  138. // 回滚事务
  139. Db::rollback();
  140. }
  141. return true;
  142. }
  143. public function goods()
  144. {
  145. return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(1);
  146. }
  147. public function Sku()
  148. {
  149. return $this->belongsTo('Sku', 'goods_sku_id', 'id', [], 'LEFT');
  150. }
  151. public function Order()
  152. {
  153. return $this->hasOne('Order', 'order_sn', 'order_sn', [], 'LEFT');
  154. }
  155. }