SellController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Select;
  4. use App\Models\Sell;
  5. use App\Models\User;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. class SellController extends Controller
  11. {
  12. public function sell(Request $request){
  13. try{
  14. $id=Auth::user()->id;
  15. $code=urldecode($request->code);
  16. DB::beginTransaction();
  17. $re=Select::where('code',$code)->first();
  18. if ($re){
  19. if ($re->is_true==1){
  20. return $this->error(50024,'','当前货物是假货不能进行零售!');
  21. }
  22. if ($re->user_id!=$id){
  23. return $this->error(50024,'','当前货物绑定的代理信息不是你!');
  24. }
  25. if ($re->is_lowest!=1){
  26. return $this->error(50024,'','零售只能使用小码进行零售!');
  27. }
  28. if ($re->is_sell!=0){
  29. return $this->error(50024,'','该商品已经零售!');
  30. }
  31. $re->is_sell=1;
  32. if (!$re->save()){
  33. throw new \Exception('1');
  34. }
  35. $res=Select::where('sort',$re->pid)->first();
  36. $res->is_full=1;
  37. if (!$res->save()){
  38. throw new \Exception('1');
  39. }
  40. if ($res->level!=1){
  41. $ress=Select::where('sort',$re->pid)->first();
  42. $ress->is_full=1;
  43. if (!$ress->save()){
  44. throw new \Exception('1');
  45. }
  46. }
  47. $sell=new Sell();
  48. $sell->select_id=$re->id;
  49. $sell->select_good_id=$re->good_select_id;
  50. $sell->user_id=$re->user_id;
  51. if (!$sell->save()){
  52. throw new \Exception('1');
  53. }
  54. }
  55. DB::commit();
  56. return $this->success([]);
  57. }catch (\Exception $exception){
  58. DB::rollBack();
  59. Log::error('零售失败,原因是'.$exception);
  60. return $this->error(50021,'','操作失败,请重试!');
  61. }
  62. }
  63. /****/
  64. public function get_user(Request $request){
  65. }
  66. }