Order.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.`order_type` AS `order_type`,
  37. a.`status` AS `status`,
  38. a.`is_del` AS `is_del`,
  39. a.`delivery_time` AS `delivery_time`,
  40. a.`order_time` AS `order_time`,
  41. `b`.`addrid` AS `addrid`,
  42. `b`.`order_num` AS `border_num`,
  43. `b`.`post_code` AS `post_code`,
  44. `b`.`post_name` AS `post_name`,
  45. `b`.`status` AS `bstatus`,
  46. `c`.`addr` AS `addr`,
  47. `c`.`provice` AS `provice`,
  48. `c`.`city` AS `city`,
  49. `c`.`provice_name` AS `provice_name`,
  50. `c`.`city_name` AS `city_name`,
  51. `c`.`area` AS `area`,
  52. `c`.`area_name` AS `area_name`,
  53. `c`.`contector` AS `contector`,
  54. `c`.`mobile` AS `contector_mobile`,
  55. `e`.`username` AS `username`,
  56. `g`.`nickname` AS `nickname`,
  57. `g`.`mobile` AS `mobile`,
  58. a.`unit_weight` AS `unit_weight`,
  59. a.`unit` AS `unit`")->select();
  60. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  61. }
  62. /**
  63. * @param id 订单id
  64. */
  65. public function info(){
  66. $id = isset($this->post['id'])&&$this->post['id']!="" ? intval($this->post['id']):"";
  67. if($id==""){
  68. return error_show(1004,"参数id 不能为空");
  69. }
  70. $order = Db::name("order")->alias('a')->where(["a.is_del"=>0,"a.accountid"=>$this->userinfo['id'],"a.id"=>$id])
  71. ->join("fc_order_post b","a.order_sn =b.order_sn","left")
  72. ->join("fc_addr c","b.addrid= c.id","left")
  73. ->join("fc_account e","a.accountid= e.id","left")
  74. ->join("fc_rela_account f","a.accountid = f.accountid","left")
  75. ->join("fc_account_info g","f.account_info = g.id","left")
  76. ->field("`a`.`id` AS `id`,
  77. `a`.`order_sn` AS `order_sn`,
  78. `a`.`accountid` AS `accountid`,
  79. `a`.`order_type` AS `order_type`,
  80. `a`.`order_num` AS `order_num`,
  81. `a`.`status` AS `status`,
  82. `a`.`is_del` AS `is_del`,
  83. `a`.`delivery_time` AS `delivery_time`,
  84. `a`.`order_time` AS `order_time`,
  85. `b`.`addrid` AS `addrid`,
  86. `b`.`order_num` AS `border_num`,
  87. `b`.`post_code` AS `post_code`,
  88. `b`.`post_name` AS `post_name`,
  89. `b`.`status` AS `bstatus`,
  90. `c`.`addr` AS `addr`,
  91. `c`.`provice` AS `provice`,
  92. `c`.`city` AS `city`,
  93. `c`.`provice_name` AS `provice_name`,
  94. `c`.`city_name` AS `city_name`,
  95. `c`.`area` AS `area`,
  96. `c`.`area_name` AS `area_name`,
  97. `c`.`contector` AS `contector`,
  98. `c`.`mobile` AS `contector_mobile`,
  99. `e`.`username` AS `username`,
  100. `g`.`nickname` AS `nickname`,
  101. `g`.`mobile` AS `mobile`,
  102. `a`.`unit_weight` AS `unit_weight`,
  103. `a`.`unit` AS `unit` ")
  104. ->find();
  105. //var_dump($order);
  106. if(empty($order)){
  107. return error_show(1005,"未找到订单数据");
  108. }
  109. return app_show(0,"获取成功",$order);
  110. }
  111. /**
  112. * @param num
  113. * @param addrid
  114. */
  115. public function ist(){
  116. $ist = Db::name('unit')->where(["is_del" => 0,"status"=>1])->field("id,name,weight")->select();
  117. return app_show(0,"获取成功",["list"=>$ist]);
  118. }
  119. public function add(){
  120. $num = isset($this->post['num'])&&$this->post['num']!="" ? intval($this->post['num']):"";
  121. if($num==""){
  122. return error_show(1004,"参数num 不能为空或0");
  123. }
  124. // $id = isset($this->post['unit_id'])&&$this->post['unit_id']!=""? intval($this->post['unit_id']): "";
  125. // if($id==""){
  126. // return error_show(1004,"unit_id不能为空");
  127. // }
  128. // $kg= Db::name('unit')->where(["is_del" => 0,"id"=>$id,"status"=>1])->field("id,name,weight,limit_num")->find();
  129. // // var_dump(Db::name('unit')->where(["is_del" => 0,"id"=>$id,"status"=>1])->getLastSql());
  130. // if(empty($kg)){
  131. // return error_show(1004,"单位不能为空");
  132. // }
  133. // $count=$num*$kg['weight'];
  134. // if($count==""){
  135. // return error_show(1004,"购买数量不能为空");
  136. // }
  137. // if($kg['limit_num']>0){
  138. // $zl = Db::name('order_log')->where(["unit_id"=>$id,"accountid"=>$this->userinfo['id']])->sum('num');
  139. // // var_dump(Db::name('order_log')->getLastSql());
  140. //
  141. // if($zl+$num>$kg['limit_num']){
  142. // $jf = $kg['limit_num']>=$zl ?$kg['limit_num']-$zl :0;
  143. // return error_show(1004,"购买数量超过限额,可购数量{$jf}{$kg['name']}");
  144. // }
  145. //
  146. // }
  147. $addrid = isset($this->post['addrid'])&&$this->post['addrid']!="" ? intval($this->post['addrid']):"";
  148. if($addrid==""){
  149. return error_show(1004,"参数addrid 不能为空");
  150. }
  151. $stock = Db::name("account_stock")->where(['is_del'=>0,"accountid"=>$this->userinfo['id']])->find();
  152. if(empty($stock) || $stock['stock_balance']<$num){
  153. return error_show(1004,"库存数量不足");
  154. }
  155. $addr =Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["a.accountid"=>$this->userinfo['id'],
  156. "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();
  157. if(empty($addr)){
  158. return error_show(1004,"地址数据未找到");
  159. }
  160. Db::startTrans();
  161. try{
  162. $ordersn =makeNo("FC");
  163. $data=[
  164. "order_sn"=>$ordersn,
  165. "accountid"=>$this->userinfo['id'],
  166. "order_num"=>$num,
  167. "order_type"=>$this->userinfo['type'],
  168. "is_del"=>0,
  169. "status"=>1,
  170. "addtime"=>date("Y-m-d H:i:s"),
  171. "updatetime"=>date("Y-m-d H:i:s"),
  172. "order_time"=>date("Y-m-d H:i:s"),
  173. ];
  174. $ordercreate= Db::name("order")->insert($data,false,true);
  175. if($ordercreate>0){
  176. $post =[
  177. "order_sn"=>$ordersn,
  178. "addrid"=>$addrid,
  179. "order_num"=>$num,
  180. 'post_code'=>"",
  181. 'post_name'=>"",
  182. "status"=>1,
  183. "is_del"=>0,
  184. "addtime"=>date("Y-m-d H:i:s"),
  185. "updatetime"=>date("Y-m-d H:i:s")
  186. ];
  187. $orderpost=Db::name("order_post")->insert($post);
  188. if($orderpost){
  189. $updatestock= Db::name("account_stock")->where($stock)->update
  190. (['stock_balance'=>$stock['stock_balance']-$num,
  191. "stock_delivery"=>$stock['stock_delivery']+$num,"updatetime"=>date("Y-m-d H:i:s")]);
  192. if($updatestock){
  193. $log=[
  194. "accountid"=>$this->userinfo['id'],
  195. "run_stock"=>$num,
  196. "type"=>3,
  197. "after_stock"=>$stock['stock_balance']-$num,
  198. "before_stock"=>$stock['stock_balance'],
  199. "action_uid"=>$this->userinfo['id'],
  200. "action_name"=>$this->userinfo['nickname'],
  201. "addtime"=>date("Y-m-d H:i:s"),
  202. ];
  203. $stocklog =Db::name("stock_log")->insert($log);
  204. $log=[
  205. "accountid"=>$this->userinfo['id'],
  206. "addtime"=>date("Y-m-d H:i:s"),
  207. "num"=>$num,
  208. ];
  209. $st =Db::name("order_log")->insert($log);
  210. if($stocklog && $st){
  211. Db::commit();
  212. return app_show(0,"下单成功",['orderid'=>$ordercreate]);
  213. }
  214. }
  215. }
  216. }
  217. Db::rollback();
  218. return error_show(1006,"下单失败");
  219. }catch (\Exception $e){
  220. Db::rollback();
  221. return error_show(1005,$e->getMessage());
  222. }
  223. }
  224. public function Stock(){
  225. $stock = Db::name("account_stock")->where(["accountid"=>$this->userinfo['id'],'is_del'=>0])->find();
  226. $data=[];
  227. $data['stock'] = isset($stock['stock_balance']) ? $stock['stock_balance']:0;
  228. return app_show(0,"获取成功",$data);
  229. }
  230. }