Order.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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,limit_num")->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. if($kg['limit_num']>0){
  103. $zl = Db::name('order_log')->where(["unit_id"=>$id,"accountid"=>$this->userinfo['id']])->sum('num');
  104. // var_dump(Db::name('order_log')->getLastSql());
  105. if($zl+$num>$kg['limit_num']){
  106. $jf = $kg['limit_num']>=$zl ?$kg['limit_num']-$zl :0;
  107. return error_show(1004,"购买数量超过限额,可购数量{$jf}{$kg['name']}");
  108. }
  109. }
  110. $addrid = isset($this->post['addrid'])&&$this->post['addrid']!="" ? intval($this->post['addrid']):"";
  111. if($addrid==""){
  112. return error_show(1004,"参数addrid 不能为空");
  113. }
  114. $stock = Db::name("account_stock")->where(['is_del'=>0,"accountid"=>$this->userinfo['id']])->find();
  115. if(empty($stock) || $stock['stock_balance']<$count){
  116. return error_show(1004,"库存数量不足");
  117. }
  118. $addr =Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["a.accountid"=>$this->userinfo['id'],
  119. "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();
  120. if(empty($addr)){
  121. return error_show(1004,"地址数据未找到");
  122. }
  123. Db::startTrans();
  124. try{
  125. $ordersn =makeNo("FC");
  126. $data=[
  127. "order_sn"=>$ordersn,
  128. "accountid"=>$this->userinfo['id'],
  129. "order_num"=>$num,
  130. "unit"=>$kg['name'],
  131. "unit_weight"=>$kg['weight'],
  132. "is_del"=>0,
  133. "status"=>1,
  134. "addtime"=>date("Y-m-d H:i:s"),
  135. "updatetime"=>date("Y-m-d H:i:s"),
  136. "order_time"=>date("Y-m-d H:i:s"),
  137. ];
  138. $ordercreate= Db::name("order")->insert($data,false,true);
  139. if($ordercreate>0){
  140. $post =[
  141. "order_sn"=>$ordersn,
  142. "addrid"=>$addrid,
  143. "order_num"=>$num,
  144. 'post_code'=>"",
  145. 'post_name'=>"",
  146. "status"=>1,
  147. "is_del"=>0,
  148. "addtime"=>date("Y-m-d H:i:s"),
  149. "updatetime"=>date("Y-m-d H:i:s")
  150. ];
  151. $orderpost=Db::name("order_post")->insert($post);
  152. if($orderpost){
  153. $updatestock= Db::name("account_stock")->where($stock)->update
  154. (['stock_balance'=>$stock['stock_balance']-$count,
  155. "stock_delivery"=>$stock['stock_delivery']+$count,"updatetime"=>date("Y-m-d H:i:s")]);
  156. if($updatestock){
  157. $log=[
  158. "accountid"=>$this->userinfo['id'],
  159. "run_stock"=>$count,
  160. "type"=>3,
  161. "after_stock"=>$stock['stock_balance']-$count,
  162. "before_stock"=>$stock['stock_balance'],
  163. "action_uid"=>$this->userinfo['id'],
  164. "action_name"=>$this->userinfo['nickname'],
  165. "addtime"=>date("Y-m-d H:i:s"),
  166. ];
  167. $stocklog =Db::name("stock_log")->insert($log);
  168. $log=[
  169. "accountid"=>$this->userinfo['id'],
  170. "addtime"=>date("Y-m-d H:i:s"),
  171. "unit"=>$kg['name'],
  172. "unit_weight"=>$kg['weight'],
  173. "num"=>$num,
  174. "total_weight"=>$num*$kg['weight'],
  175. "unit_id"=>$kg['id']
  176. ];
  177. $st =Db::name("order_log")->insert($log);
  178. if($stocklog && $st){
  179. Db::commit();
  180. return app_show(0,"下单成功",['orderid'=>$ordercreate]);
  181. }
  182. }
  183. }
  184. }
  185. Db::rollback();
  186. return error_show(1006,"下单失败");
  187. }catch (\Exception $e){
  188. Db::rollback();
  189. return error_show(1005,$e->getMessage());
  190. }
  191. }
  192. public function Stock(){
  193. $stock = Db::name("account_stock")->where(["accountid"=>$this->userinfo['id'],'is_del'=>0])->find();
  194. $data=[];
  195. $data['stock'] = isset($stock['stock_balance']) ? $stock['stock_balance']:0;
  196. return app_show(0,"获取成功",$data);
  197. }
  198. }