OrderGoods.php 3.9 KB

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