Order.php 11 KB

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