ShopOrders.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\common\model;
  3. use think\db;
  4. use think\Exception;
  5. class ShopOrders extends Base {
  6. public function addOrder($json,$addressid,$orgid,$userid){
  7. $tlist = Db::name('shop_day')->where('org_id',$orgid)->select();
  8. if(!$tlist){
  9. $this->error = "未设置开业时间";
  10. return false;
  11. }
  12. // 检查当前时间是否能点餐
  13. $flag = false;
  14. $curTime = time();
  15. foreach ($tlist as $k=>$v){
  16. $start = strtotime(date('Y-m-d').' '.$v['start']);
  17. $end = strtotime(date('Y-m-d').' '.$v['end']);
  18. if($curTime >= $start && $curTime <= $end){
  19. $flag = true;
  20. }
  21. }
  22. if(!$flag){
  23. $this->error = "未在开业时间范围内,无法下单";
  24. return false;
  25. }
  26. $arr = json_decode($json,true);
  27. $alltotal = 0;
  28. foreach ($arr as $k=>$v){
  29. $ginfo = Db::name('shop_goods')
  30. ->where('id',$v['id'])
  31. ->where('del',0)
  32. ->where('enable',1)
  33. ->find();
  34. if(!$ginfo){
  35. $this->error = "选择的商品已下架";
  36. return false;
  37. }
  38. $alltotal += $v['nums'] * $ginfo['price'];
  39. $arr[$k]['price'] = $ginfo['price'];
  40. }
  41. $alltotal = round($alltotal,2);
  42. //获取地址信息
  43. $address = Db::name('shop_address')
  44. ->where('id',$addressid)->where('user_id',$userid)->find();
  45. if(!$address){
  46. $this->error = "地址不存在";
  47. return false;
  48. }
  49. $addrs = $address['building'].$address['unit'].$address['content'];
  50. Db::startTrans();
  51. try {
  52. $data=[
  53. 'order_sn' => get_unique_id(),
  54. 'amount' => $alltotal,
  55. 'status' => 1,
  56. 'address_id' => $addressid,
  57. 'org_id' => $orgid,
  58. 'user_id' => $userid,
  59. 'name' => $address['name'],
  60. 'phone' => $address['phone'],
  61. 'address' => $addrs,
  62. 'create_time' => date('Y-m-d H:i:s'),
  63. ];
  64. $orderId = Db::name('shop_orders')->insertGetId($data);
  65. if(!$orderId){
  66. throw new Exception('操作失败');
  67. }
  68. foreach ($arr as $k=>$v){
  69. $gdata=[
  70. 'goods_id'=>$v['id'],
  71. 'nums'=>$v['nums'],
  72. 'price'=>$v['price'],
  73. 'order_id'=>$orderId,
  74. ];
  75. $ret = Db::name('shop_order_goods')->insert($gdata);
  76. if(!$ret){
  77. throw new Exception('操作失败');
  78. }
  79. }
  80. Db::commit();
  81. }catch (Exception $e){
  82. Db::rollback();
  83. $this->error = "操作失败";
  84. return false;
  85. }
  86. return true;
  87. }
  88. public function addOrder_old($json,$addressid,$orgid,$userid,$delivery_time,$type){
  89. $tinfo = Db::name('shop_day')->where('id',$type)->find();
  90. if(!$tinfo){
  91. $this->error = "未查询到排餐记录";
  92. return false;
  93. }
  94. if($tinfo['day'] != $delivery_time){
  95. $this->error = "参数错误";
  96. return false;
  97. }
  98. $goods = explode(',',$tinfo['goods']);
  99. $curDay = date('Y-m-d');
  100. if($tinfo['day'] == $curDay){ //当天需要检查售卖时间
  101. if(!$tinfo['start'] || !$tinfo['end']){
  102. $this->error = "商品未设置售卖时间,无法下单";
  103. return false;
  104. }else{
  105. $endTime = strtotime($tinfo['day'].' '.$tinfo['end']);
  106. if(time() > $endTime){
  107. $this->error = "已超过售卖时间,无法下单";
  108. return false;
  109. }
  110. }
  111. }
  112. $arr = json_decode($json,true);
  113. $alltotal = 0;
  114. foreach ($arr as $k=>$v){
  115. $ginfo = Db::name('shop_goods')
  116. ->where('id',$v['id'])
  117. ->where('del',0)
  118. ->where('enable',1)
  119. ->find();
  120. if(!$ginfo){
  121. $this->error = "选择的商品已下架";
  122. return false;
  123. }
  124. if(!in_array($v['id'],$goods)){
  125. $this->error = "选择的商品已下架";
  126. return false;
  127. }
  128. $alltotal += $v['nums'] * $ginfo['price'];
  129. $arr[$k]['price'] = $ginfo['price'];
  130. }
  131. $alltotal = round($alltotal,2);
  132. //获取地址信息
  133. $address = Db::name('shop_address')->where('id',$addressid)->where('user_id',$userid)->find();
  134. if(!$address){
  135. $this->error = "地址不存在";
  136. return false;
  137. }
  138. $addrs = $address['building'].'号楼'.$address['unit'].'单元'.$address['content'];
  139. Db::startTrans();
  140. try {
  141. $data=[
  142. 'order_sn' => get_unique_id(),
  143. 'amount' => $alltotal,
  144. 'status' => 1,
  145. 'address_id' => $addressid,
  146. 'org_id' => $orgid,
  147. 'user_id' => $userid,
  148. 'name' => $address['name'],
  149. 'phone' => $address['phone'],
  150. 'address' => $addrs,
  151. 'create_time' => date('Y-m-d H:i:s'),
  152. 'delivery_time' => $delivery_time,
  153. 'cate' => $tinfo['cate'],
  154. 'shop_day_id' => $type
  155. ];
  156. $orderId = Db::name('shop_orders')->insertGetId($data);
  157. if(!$orderId){
  158. throw new Exception('操作失败');
  159. }
  160. foreach ($arr as $k=>$v){
  161. $gdata=[
  162. 'goods_id'=>$v['id'],
  163. 'nums'=>$v['nums'],
  164. 'price'=>$v['price'],
  165. 'order_id'=>$orderId,
  166. ];
  167. $ret = Db::name('shop_order_goods')->insert($gdata);
  168. if(!$ret){
  169. throw new Exception('操作失败');
  170. }
  171. }
  172. Db::commit();
  173. }catch (Exception $e){
  174. Db::rollback();
  175. $this->error = "操作失败";
  176. return false;
  177. }
  178. return true;
  179. }
  180. public function list($page,$size,$userid,$orgid){
  181. $ret = Db::name('shop_orders')
  182. ->alias('so')
  183. ->join('org o','o.id = so.org_id')
  184. ->field('o.name,so.id,so.status,so.create_time,so.cate,so.shop_day_id')
  185. ->where([
  186. 'so.org_id'=>$orgid,
  187. 'so.del'=>0,
  188. 'so.user_id'=>$userid
  189. ])
  190. ->order('so.id desc')
  191. ->page($page,$size)
  192. ->select();
  193. $ret = $ret?$ret:[];
  194. foreach ($ret as $k=>$v){
  195. $orderGoods = Db::name('shop_order_goods')->where('order_id',$v['id'])->field('goods_id')->find();
  196. $goods = Db::name('shop_goods')->where('id',$orderGoods['goods_id'])->field('img')->find();
  197. $ret[$k]['goods_img'] = $goods?$goods['img']:'';
  198. }
  199. return $ret;
  200. }
  201. public function details($id){
  202. $ret = Db::name('shop_orders')
  203. ->alias('so')
  204. ->field('o.name as org_name,so.*')
  205. ->join('org o','o.id=so.org_id')
  206. ->where('so.id',$id)
  207. ->find();
  208. //获取商品信息
  209. $orderGoods = Db::name('shop_order_goods')->where('order_id',$ret['id'])->select();
  210. foreach ($orderGoods as $k=>$v){
  211. $goods = Db::name('shop_goods')->field('title,img')->where('id',$v['goods_id'])->find();
  212. $orderGoods[$k]['goods_name'] = $goods?$goods['title']:'';
  213. $orderGoods[$k]['goods_img'] = $goods?$goods['img']:'';
  214. }
  215. $ret['goods'] = $orderGoods;
  216. // 检查订单是否可以取消订单
  217. $ret['cancel'] = 1;
  218. if($ret['status'] == 1){
  219. $tinfo = Db::name('shop_day')->where('id',$ret['shop_day_id'])->find();
  220. $endTime = strtotime($tinfo['day'].' '.$tinfo['end']);
  221. if(time() > $endTime){
  222. $ret['cancel'] = 0;
  223. }
  224. }
  225. if($ret['status'] == 2){
  226. $ret['cancel'] = 0;
  227. }
  228. return $ret;
  229. }
  230. public function cancel($id,$userId){
  231. $info = Db::name('shop_orders')
  232. ->where('id',$id)
  233. ->where('user_id',$userId)
  234. ->find();
  235. if(!$info){
  236. $this->error = "订单不存在";
  237. return false;
  238. }
  239. if($info['status'] != 1){
  240. $this->error = "无权限取消";
  241. return false;
  242. }
  243. $tinfo = Db::name('shop_day')->where('id',$info['shop_day_id'])->find();
  244. $endTime = strtotime($tinfo['day'].' '.$tinfo['end']);
  245. if(time() > $endTime){
  246. $this->error = "已超过可取消的时间";
  247. return false;
  248. }
  249. $ret = Db::name('shop_orders')->where('id',$id)->update([
  250. 'status' => 2,
  251. 'cancel_time' => date('Y-m-d H:i:s')
  252. ]);
  253. if(!$ret){
  254. $this->error = "操作失败";
  255. return false;
  256. }
  257. return true;
  258. }
  259. }