Order.php 11 KB

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