Order.php 11 KB

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