OrderGoods.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. ];
  20. protected static $config = [];
  21. protected static $tagCount = 0;
  22. protected static function init()
  23. {
  24. $config = get_addon_config('shop');
  25. self::$config = $config;
  26. }
  27. public function getGoodsImageAttr($value, $data)
  28. {
  29. $value = $value ?: ($data['image'] ?? '/assets/addons/shop/img/noimage.jpg');
  30. return cdnurl($value, true);
  31. }
  32. //销量增
  33. public static function setGoodsSalesInc($order_sn)
  34. {
  35. $list = (new self)->where('order_sn', $order_sn)->select();
  36. // 启动事务
  37. Db::startTrans();
  38. try {
  39. foreach ($list as $item) {
  40. $goods = $item->goods;
  41. $sku = $item->sku;
  42. if ($goods) {
  43. $goods->setInc('sales', $item->nums);
  44. }
  45. if ($sku) {
  46. $sku->setInc('sales', $item->nums);
  47. }
  48. }
  49. // 提交事务
  50. Db::commit();
  51. } catch (\Exception $e) {
  52. // 回滚事务
  53. Db::rollback();
  54. }
  55. return true;
  56. }
  57. //销量减
  58. public static function setGoodsSalesDec($order_sn)
  59. {
  60. $list = (new self)->where('order_sn', $order_sn)->select();
  61. // 启动事务
  62. Db::startTrans();
  63. try {
  64. foreach ($list as $item) {
  65. $goods = $item->goods;
  66. $sku = $item->sku;
  67. if ($goods) {
  68. $goods->setDec('sales', $item->nums);
  69. }
  70. if ($sku) {
  71. $sku->setDec('sales', $item->nums);
  72. }
  73. }
  74. // 提交事务
  75. Db::commit();
  76. } catch (\Exception $e) {
  77. // 回滚事务
  78. Db::rollback();
  79. }
  80. return true;
  81. }
  82. //库存增
  83. public static function setGoodsStocksInc($order_sn)
  84. {
  85. $list = (new self)->where('order_sn', $order_sn)->select();
  86. // 启动事务
  87. Db::startTrans();
  88. try {
  89. foreach ($list as $item) {
  90. $goods = $item->goods;
  91. $sku = $item->sku;
  92. if ($sku) {
  93. $sku->setInc('stocks', $item->nums);
  94. }
  95. if ($goods) {
  96. $goods->setInc('stocks', $item->nums);
  97. }
  98. }
  99. // 提交事务
  100. Db::commit();
  101. } catch (\Exception $e) {
  102. // 回滚事务
  103. Db::rollback();
  104. }
  105. return true;
  106. }
  107. //库存减
  108. public static function setGoodsStocksDec($order_sn)
  109. {
  110. $list = (new self)->where('order_sn', $order_sn)->select();
  111. // 启动事务
  112. Db::startTrans();
  113. try {
  114. foreach ($list as $item) {
  115. $goods = $item->goods;
  116. $sku = $item->sku;
  117. if ($sku) {
  118. $sku->setDec('stocks', $item->nums);
  119. }
  120. if ($goods) {
  121. $goods->setDec('stocks', $item->nums);
  122. }
  123. }
  124. // 提交事务
  125. Db::commit();
  126. } catch (\Exception $e) {
  127. // 回滚事务
  128. Db::rollback();
  129. }
  130. return true;
  131. }
  132. public function getGoodsSkuAttrAttr($value, $data)
  133. {
  134. return json_decode($value, true);
  135. }
  136. public function goods()
  137. {
  138. return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(1);
  139. }
  140. public function Sku()
  141. {
  142. return $this->belongsTo('Sku', 'goods_sku_id', 'id', [], 'LEFT');
  143. }
  144. public function Order()
  145. {
  146. return $this->hasOne('Order', 'order_sn', 'order_sn', [], 'LEFT');
  147. }
  148. }