ChangeOrderPriceController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * 改价
  4. * Created by PhpStorm.
  5. * Author: 芸众商城 www.yunzshop.com
  6. * Date: 2017/3/18
  7. * Time: 上午10:00
  8. */
  9. namespace app\backend\modules\order\controllers;
  10. use app\backend\modules\order\models\Order;
  11. use app\common\components\BaseController;
  12. use app\common\models\order\OrderChangePriceLog;
  13. use app\common\models\OrderGoods;
  14. use app\frontend\modules\order\services\OrderService;
  15. use Illuminate\Support\Facades\DB;
  16. class ChangeOrderPriceController extends BaseController
  17. {
  18. /**
  19. * 展示
  20. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  21. */
  22. public function index()
  23. {
  24. $order_model = Order::find(\YunShop::request()->order_id);
  25. return view('order.change_price',[
  26. 'order_goods_model' => $order_model->hasManyOrderGoods,
  27. 'order_model' => $order_model,
  28. 'change_num' => 1//改价次数
  29. ]);
  30. }
  31. /**
  32. * 修改
  33. * @param \Request $request
  34. * @return \Illuminate\Http\JsonResponse
  35. */
  36. public function store(\Illuminate\Http\Request $request)
  37. {
  38. OrderService::changeOrderPrice($request);
  39. return $this->message('改价成功');
  40. }
  41. /**
  42. * 改价状态清空重置 todo 有bug
  43. * @param \Request $request
  44. */
  45. public function back(\Illuminate\Http\Request $request){
  46. $orderId = $request->input('order_id');
  47. $this->validate([
  48. 'order_id'=>'required'
  49. ]);
  50. $order = Order::find($orderId);
  51. $change_price = $order->orderChangePriceLogs->sum('change_price');
  52. $order->price -= $change_price;
  53. $order->order_goods_price -= $change_price;
  54. $order->dispatch -= $order->orderChangePriceLogs->sum('change_dispatch_price');
  55. DB::transaction(function ()use ($order){
  56. $order->hasManyOrderGoods->sum(function ($orderGoods){
  57. dd($orderGoods->hasManyChangeOrderGoodsPrcieLogs);
  58. exit;
  59. if(!isset($orderGoods->hasManyChangeOrderGoodsPrcieLogs)){
  60. return 0;
  61. }
  62. $result = $orderGoods->hasManyChangeOrderGoodsPrcieLogs->sum('change_price');
  63. /**
  64. * @var $orderGoods OrderGoods
  65. */
  66. $orderGoods->orderGoodsChangePriceLogs()->delete();
  67. return $result;
  68. });
  69. $order->orderChangePriceLogs()->delete();
  70. $order->push();
  71. });
  72. echo 'ok';
  73. }
  74. /**
  75. * 展示
  76. */
  77. public function indexApi()
  78. {
  79. $order_model = Order::with(['hasManyOrderGoods'=> function($query) {
  80. $query->with('hasOneGoods');
  81. },'address'])->find(request()->order_id);
  82. return $this->successJson('show',[
  83. 'order_goods_model' => $order_model->hasManyOrderGoods,
  84. 'order_model' => $order_model,
  85. 'change_num' => 1//改价次数
  86. ]);
  87. }
  88. /**
  89. * 修改价格保存
  90. * @return \Illuminate\Http\JsonResponse
  91. * @throws \app\common\exceptions\AppException
  92. */
  93. public function storeApi()
  94. {
  95. OrderService::changeOrderPrice(request()->input());
  96. return $this->successJson('改价成功');
  97. }
  98. }