OrderGoodsService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\common\Service\Order;
  3. use app\common\model\OrderGoods as OrderGoodsModel;
  4. use think\Db;
  5. class OrderGoodsService
  6. {
  7. //销量增
  8. public static function setGoodsSalesInc($order_sn)
  9. {
  10. $list = (new OrderGoodsModel())->where('order_sn', $order_sn)->select();
  11. // 启动事务
  12. Db::startTrans();
  13. try {
  14. foreach ($list as $item) {
  15. $goods = $item->goods;
  16. $sku = $item->sku;
  17. if ($goods) {
  18. $goods->setInc('sales', $item->nums);
  19. }
  20. if ($sku) {
  21. $sku->setInc('sales', $item->nums);
  22. }
  23. }
  24. // 提交事务
  25. Db::commit();
  26. } catch (\Exception $e) {
  27. // 回滚事务
  28. Db::rollback();
  29. }
  30. return true;
  31. }
  32. //销量减
  33. public static function setGoodsSalesDec($order_sn)
  34. {
  35. $list = (new OrderGoodsModel())->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->setDec('sales', $item->nums);
  44. }
  45. if ($sku) {
  46. $sku->setDec('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 setGoodsStocksInc($order_sn)
  59. {
  60. $list = (new OrderGoodsModel())->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 ($sku) {
  68. $sku->setInc('stocks', $item->nums);
  69. }
  70. if ($goods) {
  71. $goods->setInc('stocks', $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 setGoodsStocksDec($order_sn)
  84. {
  85. $list = (new OrderGoodsModel())->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->setDec('stocks', $item->nums);
  94. }
  95. if ($goods) {
  96. $goods->setDec('stocks', $item->nums);
  97. }
  98. }
  99. // 提交事务
  100. Db::commit();
  101. } catch (\Exception $e) {
  102. // 回滚事务
  103. Db::rollback();
  104. }
  105. return true;
  106. }
  107. }