Order.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\Home\controller;
  3. use think\Db;
  4. class Order extends Base
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. /**
  11. * @param status 0/1/2
  12. * @param page
  13. * @param size
  14. */
  15. public function list(){
  16. $page = isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']):1;
  17. $size = isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']):10;
  18. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):0;
  19. $where=["is_del"=>0,"accountid"=>$this->userinfo['id']];
  20. if($status!=0 && in_array($status,[1,2])){
  21. $where['status']=$status;
  22. }
  23. $count =Db::name("order_info")->where($where)->count();
  24. $total = ceil($count/$size);
  25. $page = $page>$total?$total:$page;
  26. $list = Db::name("order_info")->where($where)->page($page,$size)->field("id,order_sn,order_num,order_time,delivery_time,post_code,post_name,status")
  27. ->select();
  28. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  29. }
  30. /**
  31. * @param id 订单id
  32. */
  33. public function info(){
  34. $id = isset($this->post['id'])&&$this->post['id']!="" ? intval($this->post['id']):"";
  35. if($id==""){
  36. return error_show(1004,"参数id 不能为空");
  37. }
  38. $order = Db::name("order_info")->where(["is_del"=>0,"accountid"=>$this->userinfo['id'],"id"=>$id])->field("id,
  39. order_sn,order_num,order_time,delivery_time,status, post_code,post_name,provice_name,city_name,area_name,addr,contector,contector_mobile,nickname")->find();
  40. if(empty($order)){
  41. return error_show(1005,"未找到订单数据");
  42. }
  43. return app_show(0,"获取成功",$order);
  44. }
  45. /**
  46. * @param num
  47. * @param addrid
  48. */
  49. public function add(){
  50. $num = isset($this->post['num'])&&$this->post['num']!="" ? intval($this->post['num']):"";
  51. if($num==""){
  52. return error_show(1004,"参数num 不能为空或0");
  53. }
  54. $addrid = isset($this->post['addrid'])&&$this->post['addrid']!="" ? intval($this->post['addrid']):"";
  55. if($addrid==""){
  56. return error_show(1004,"参数addrid 不能为空");
  57. }
  58. $stock = Db::name("account_stock")->where(['is_del'=>0,"accountid"=>$this->userinfo['id']])->find();
  59. if(empty($stock) || $stock['stock_balance']<$num){
  60. return error_show(1004,"库存数量不足");
  61. }
  62. $addr =Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["a.accountid"=>$this->userinfo['id'],
  63. "b.is_del"=>0,"b.id"=>$addrid])->field("b.id,b.provice,b.provice_name,b.city,b.city_name,b.area,b.area_name,b.addr,b.contector,b.mobile,b.addtime")->find();
  64. if(empty($addr)){
  65. return error_show(1004,"地址数据未找到");
  66. }
  67. Db::startTrans();
  68. try{
  69. $ordersn =makeNo("FC");
  70. $data=[
  71. "order_sn"=>$ordersn,
  72. "accountid"=>$this->userinfo['id'],
  73. "order_num"=>$num,
  74. "is_del"=>0,
  75. "status"=>1,
  76. "addtime"=>date("Y-m-d H:i:s"),
  77. "updatetime"=>date("Y-m-d H:i:s"),
  78. "order_time"=>date("Y-m-d H:i:s"),
  79. ];
  80. $ordercreate= Db::name("order")->insert($data,false,true);
  81. if($ordercreate>0){
  82. $post =[
  83. "order_sn"=>$ordersn,
  84. "addrid"=>$addrid,
  85. "order_num"=>$num,
  86. 'post_code'=>"",
  87. 'post_name'=>"",
  88. "status"=>1,
  89. "is_del"=>0,
  90. "addtime"=>date("Y-m-d H:i:s"),
  91. "updatetime"=>date("Y-m-d H:i:s")
  92. ];
  93. $orderpost=Db::name("order_post")->insert($post);
  94. if($orderpost){
  95. $updatestock= Db::name("account_stock")->where($stock)->update
  96. (['stock_balance'=>$stock['stock_balance']-$num,
  97. "stock_delivery"=>$stock['stock_delivery']+$num,"updatetime"=>date("Y-m-d H:i:s")]);
  98. if($updatestock){
  99. $log=[
  100. "accountid"=>$this->userinfo['id'],
  101. "run_stock"=>$num,
  102. "type"=>3,
  103. "after_stock"=>$stock['stock_balance']-$num,
  104. "before_stock"=>$stock['stock_balance'],
  105. "action_uid"=>$this->userinfo['id'],
  106. "action_name"=>$this->userinfo['nickname'],
  107. "addtime"=>date("Y-m-d H:i:s")
  108. ];
  109. $stocklog =Db::name("stock_log")->insert($log);
  110. if($stocklog){
  111. Db::commit();
  112. return app_show(0,"下单成功",['orderid'=>$ordercreate]);
  113. }
  114. }
  115. }
  116. }
  117. Db::rollback();
  118. return error_show(1006,"下单失败");
  119. }catch (\Exception $e){
  120. Db::rollback();
  121. return error_show(1005,$e->getMessage());
  122. }
  123. }
  124. public function Stock(){
  125. $stock = Db::name("account_stock")->where(["accountid"=>$this->userinfo['id'],'is_del'=>0])->find();
  126. $data=[];
  127. $data['stock'] = isset($stock['stock_balance']) ? $stock['stock_balance']:0;
  128. return app_show(0,"获取成功",$data);
  129. }
  130. }