Order.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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=["a.is_del"=>0,"a.accountid"=>$this->userinfo['id']];
  20. if($status!=0 && in_array($status,[1,2])){
  21. $where['a.status']=$status;
  22. }
  23. $count =Db::name("order a")->where($where)->count();
  24. $total = ceil($count/$size);
  25. $page = $page>$total?$total:$page;
  26. $list = Db::name("order a")->where($where)->page($page,$size)
  27. ->join("fc_order_post b","a.`order_sn` = `b`.`order_sn`","left")
  28. ->join("fc_addr c","`b`.`addrid` = `c`.`id`","left")
  29. ->join("fc_account e","a.`accountid` = `e`.`id`","left")
  30. ->join("fc_rela_account f","a.`accountid` = `f`.`accountid`","left")
  31. ->join("fc_account_info g","`f`.`account_info` = `g`.`id`","left")
  32. ->field("a.id AS id,
  33. a.`order_sn` AS `order_sn`,
  34. a.`accountid` AS `accountid`,
  35. a.`order_num` AS `order_num`,
  36. a.`status` AS `status`,
  37. a.`is_del` AS `is_del`,
  38. a.`delivery_time` AS `delivery_time`,
  39. a.`order_time` AS `order_time`,
  40. `b`.`addrid` AS `addrid`,
  41. `b`.`order_num` AS `border_num`,
  42. `b`.`post_code` AS `post_code`,
  43. `b`.`post_name` AS `post_name`,
  44. `b`.`status` AS `bstatus`,
  45. `c`.`addr` AS `addr`,
  46. `c`.`provice` AS `provice`,
  47. `c`.`city` AS `city`,
  48. `c`.`provice_name` AS `provice_name`,
  49. `c`.`city_name` AS `city_name`,
  50. `c`.`area` AS `area`,
  51. `c`.`area_name` AS `area_name`,
  52. `c`.`contector` AS `contector`,
  53. `c`.`mobile` AS `contector_mobile`,
  54. `e`.`username` AS `username`,
  55. `g`.`nickname` AS `nickname`,
  56. `g`.`mobile` AS `mobile`,
  57. a.`unit_weight` AS `unit_weight`,
  58. a.`unit` AS `unit`")->select();
  59. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  60. }
  61. /**
  62. * @param id 订单id
  63. */
  64. public function info(){
  65. $id = isset($this->post['id'])&&$this->post['id']!="" ? intval($this->post['id']):"";
  66. if($id==""){
  67. return error_show(1004,"参数id 不能为空");
  68. }
  69. $order = Db::name("order_info")->where(["is_del"=>0,"accountid"=>$this->userinfo['id'],"id"=>$id])->field("id,
  70. order_sn,order_num,order_time,delivery_time,status, post_code,post_name,provice_name,city_name,area_name,addr,contector,contector_mobile,nickname,unit,unit_weight")->find();
  71. if(empty($order)){
  72. return error_show(1005,"未找到订单数据");
  73. }
  74. return app_show(0,"获取成功",$order);
  75. }
  76. /**
  77. * @param num
  78. * @param addrid
  79. */
  80. public function ist(){
  81. $ist = Db::name('unit')->where(["is_del" => 0,"status"=>1])->field("id,name,weight")->select();
  82. return app_show(0,"获取成功",["list"=>$ist]);
  83. }
  84. public function add(){
  85. $num = isset($this->post['num'])&&$this->post['num']!="" ? intval($this->post['num']):"";
  86. if($num==""){
  87. return error_show(1004,"参数num 不能为空或0");
  88. }
  89. $id = isset($this->post['unit_id'])&&$this->post['unit_id']!=""? intval($this->post['unit_id']): "";
  90. if($id==""){
  91. return error_show(1004,"unit_id不能为空");
  92. }
  93. $kg= Db::name('unit')->where(["is_del" => 0,"id"=>$id,"status"=>1])->field("id,name,weight")->find();
  94. // var_dump(Db::name('unit')->where(["is_del" => 0,"id"=>$id,"status"=>1])->getLastSql());
  95. if(empty($kg)){
  96. return error_show(1004,"单位不能为空");
  97. }
  98. $count=$num*$kg['weight'];
  99. if($count==""){
  100. return error_show(1004,"购买数量不能为空");
  101. }
  102. $addrid = isset($this->post['addrid'])&&$this->post['addrid']!="" ? intval($this->post['addrid']):"";
  103. if($addrid==""){
  104. return error_show(1004,"参数addrid 不能为空");
  105. }
  106. $stock = Db::name("account_stock")->where(['is_del'=>0,"accountid"=>$this->userinfo['id']])->find();
  107. if(empty($stock) || $stock['stock_balance']<$count){
  108. return error_show(1004,"库存数量不足");
  109. }
  110. $addr =Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["a.accountid"=>$this->userinfo['id'],
  111. "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();
  112. if(empty($addr)){
  113. return error_show(1004,"地址数据未找到");
  114. }
  115. Db::startTrans();
  116. try{
  117. $ordersn =makeNo("FC");
  118. $data=[
  119. "order_sn"=>$ordersn,
  120. "accountid"=>$this->userinfo['id'],
  121. "order_num"=>$num,
  122. "unit"=>$kg['name'],
  123. "unit_weight"=>$kg['weight'],
  124. "is_del"=>0,
  125. "status"=>1,
  126. "addtime"=>date("Y-m-d H:i:s"),
  127. "updatetime"=>date("Y-m-d H:i:s"),
  128. "order_time"=>date("Y-m-d H:i:s"),
  129. ];
  130. $ordercreate= Db::name("order")->insert($data,false,true);
  131. if($ordercreate>0){
  132. $post =[
  133. "order_sn"=>$ordersn,
  134. "addrid"=>$addrid,
  135. "order_num"=>$num,
  136. 'post_code'=>"",
  137. 'post_name'=>"",
  138. "status"=>1,
  139. "is_del"=>0,
  140. "addtime"=>date("Y-m-d H:i:s"),
  141. "updatetime"=>date("Y-m-d H:i:s")
  142. ];
  143. $orderpost=Db::name("order_post")->insert($post);
  144. if($orderpost){
  145. $updatestock= Db::name("account_stock")->where($stock)->update
  146. (['stock_balance'=>$stock['stock_balance']-$num,
  147. "stock_delivery"=>$stock['stock_delivery']+$num,"updatetime"=>date("Y-m-d H:i:s")]);
  148. if($updatestock){
  149. $log=[
  150. "accountid"=>$this->userinfo['id'],
  151. "run_stock"=>$num,
  152. "type"=>3,
  153. "after_stock"=>$stock['stock_balance']-$num,
  154. "before_stock"=>$stock['stock_balance'],
  155. "action_uid"=>$this->userinfo['id'],
  156. "action_name"=>$this->userinfo['nickname'],
  157. "addtime"=>date("Y-m-d H:i:s")
  158. ];
  159. $stocklog =Db::name("stock_log")->insert($log);
  160. if($stocklog){
  161. Db::commit();
  162. return app_show(0,"下单成功",['orderid'=>$ordercreate]);
  163. }
  164. }
  165. }
  166. }
  167. Db::rollback();
  168. return error_show(1006,"下单失败");
  169. }catch (\Exception $e){
  170. Db::rollback();
  171. return error_show(1005,$e->getMessage());
  172. }
  173. }
  174. public function Stock(){
  175. $stock = Db::name("account_stock")->where(["accountid"=>$this->userinfo['id'],'is_del'=>0])->find();
  176. $data=[];
  177. $data['stock'] = isset($stock['stock_balance']) ? $stock['stock_balance']:0;
  178. return app_show(0,"获取成功",$data);
  179. }
  180. }