GOrders.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use EasyWeChat\Factory;
  5. class GOrders extends Base
  6. {
  7. public function add($cart_Id,$addressId,$userId,$orgId,$delivery_type,$delivery_date){
  8. $addressInfo=Db::name('shop_address')->where('id',$addressId)->find();
  9. if(!$addressInfo){
  10. $this->error = "未选择地址";
  11. return false;
  12. }
  13. $newDate = strtotime(date('Y-m-d H:i:s'))-60*5;
  14. if($delivery_type==1){
  15. if(empty($delivery_date)){
  16. $this->error = "送货时间不能为空";
  17. return false;
  18. }
  19. if($delivery_date < date('Y-m-d H:i:s',$newDate)){
  20. $this->error = "送货时间不能小于当前时间";
  21. return false;
  22. }
  23. }
  24. $map[]=['c.user_id','=',$userId];
  25. $map[]=['c.id','in',$cart_Id];
  26. $map[]=['g.enable','=',1];
  27. $map[]=['g.del','=',0];
  28. $goodsInfo=Db::name('g_cart')
  29. ->alias('c')
  30. ->join('g_goods g','c.goods_id=g.id')
  31. ->field('c.nums,g.id,g.title,g.price,g.sale_price,g.type,g.stock')
  32. ->where($map)
  33. ->select();
  34. if(!$goodsInfo){
  35. $this->error = "未选择商品";
  36. return false;
  37. }
  38. foreach ($goodsInfo as $k=>$v){
  39. if($v['type'] == 1){
  40. $goodsInfo[$k]['price']=$v['sale_price'];
  41. }
  42. if($v['stock'] < $v['nums']){
  43. $this->error = "库存不足";
  44. return false;
  45. }
  46. }
  47. $totalPrice=0;
  48. foreach ($goodsInfo as $k=>$v){
  49. $sum=$v['nums'] * $v['price'];
  50. $totalPrice+=$sum;
  51. }
  52. Db::startTrans();
  53. try {
  54. $data=[
  55. 'order_sn'=>get_unique_id(),
  56. 'org_id'=>$orgId,
  57. 'user_id'=>$userId,
  58. 'amount'=>round($totalPrice,2),
  59. 'status'=>0,
  60. 'create_time'=>date('Y-m-d H:i:s'),
  61. 'name'=>$addressInfo['name'],
  62. 'phone'=>$addressInfo['phone'],
  63. 'delivery_type'=>$delivery_type,
  64. 'delivery_date'=>$delivery_date,
  65. 'address'=>$addressInfo['content'],
  66. 'aid' => $addressId
  67. ];
  68. if($delivery_type==0){
  69. unset($data['delivery_date']);
  70. }
  71. $addOrder=Db::name('g_orders')->insertGetId($data);
  72. if(!$addOrder){
  73. exception('操作失败');
  74. }
  75. foreach ($goodsInfo as $kay=>$val){
  76. $gdata=[
  77. 'order_id'=>$addOrder,
  78. 'goods_id'=>$val['id'],
  79. 'nums'=>$val['nums'],
  80. 'price'=>$val['price'],
  81. 'type'=>$val['type'],
  82. ];
  83. $addOrderGoods=Db::name('g_order_goods')->insert($gdata);
  84. if(!$addOrderGoods){
  85. exception('操作失败');
  86. }
  87. //减少库存
  88. $dec= Db::name('g_goods')
  89. ->where('id',$val['id'])
  90. ->dec('stock', $val['nums'])
  91. ->update();
  92. if(!$dec){
  93. exception('操作失败');
  94. }
  95. }
  96. //删除用户购物车信息
  97. $delCart=Db::name('g_cart')->where('user_id',$userId)->whereIn('id',$cart_Id)->delete();
  98. if(!$delCart){
  99. exception('操作失败');
  100. }
  101. Db::commit();
  102. return $addOrder;
  103. } catch (\Exception $e) {
  104. // 回滚事务
  105. $this->error = $e->getMessage();
  106. Db::rollback();
  107. return false;
  108. }
  109. }
  110. public function addOne($goodsId,$addressId,$nums,$userId,$orgId,$delivery_type,$delivery_date){
  111. $addressInfo=Db::name('shop_address')->where('id',$addressId)->find();
  112. if(!$addressInfo){
  113. $this->error = "未选择地址";
  114. return false;
  115. }
  116. $newDate = strtotime(date('Y-m-d H:i:s'))-60*5;
  117. if($delivery_type==1){
  118. if(empty($delivery_date)){
  119. $this->error = "送货时间不能为空";
  120. return false;
  121. }
  122. if($delivery_date < date('Y-m-d H:i:s',$newDate)){
  123. $this->error = "送货时间不能小于当前时间";
  124. return false;
  125. }
  126. }
  127. $map[]=['id','=',$goodsId];
  128. $map[]=['enable','=',1];
  129. $map[]=['del','=',0];
  130. $goodsInfo=Db::name('g_goods')->where($map)->find();
  131. if(!$goodsInfo){
  132. $this->error = "未选择商品";
  133. return false;
  134. }
  135. if($goodsInfo['stock'] < $nums){
  136. $this->error = "库存不足";
  137. return false;
  138. }
  139. if($goodsInfo['type']==1){
  140. $goodsInfo['price']=$goodsInfo['sale_price'];
  141. }
  142. unset($goodsInfo['sale_price']);
  143. Db::startTrans();
  144. try {
  145. $data=[
  146. 'order_sn'=>get_unique_id(),
  147. 'org_id'=>$orgId,
  148. 'user_id'=>$userId,
  149. 'amount'=>round($goodsInfo['price'] * $nums,2),
  150. 'status'=>0,
  151. 'create_time'=>date('Y-m-d H:i:s'),
  152. 'name'=>$addressInfo['name'],
  153. 'phone'=>$addressInfo['phone'],
  154. 'address'=>$addressInfo['content'],
  155. 'aid' => $addressId,
  156. 'delivery_type'=>$delivery_type,
  157. 'delivery_date'=>$delivery_date,
  158. ];
  159. if($delivery_type==0){
  160. unset($data['delivery_date']);
  161. }
  162. $addOrder=Db::name('g_orders')->insertGetId($data);
  163. if(!$addOrder){
  164. exception('操作失败');
  165. }
  166. $gdata=[
  167. 'order_id'=>$addOrder,
  168. 'goods_id'=>$goodsInfo['id'],
  169. 'nums'=>$nums,
  170. 'price'=>$goodsInfo['price'],
  171. 'type'=>$goodsInfo['type'],
  172. ];
  173. $addOrderGoods=Db::name('g_order_goods')->insert($gdata);
  174. if(!$addOrderGoods){
  175. exception('操作失败');
  176. }
  177. //减少库存
  178. $dec = Db::name('g_goods')
  179. ->where('id',$goodsInfo['id'])
  180. ->dec('stock', $nums)
  181. ->update();
  182. if(!$dec){
  183. exception('操作失败');
  184. }
  185. Db::commit();
  186. return $addOrder;
  187. } catch (\Exception $e) {
  188. // 回滚事务
  189. $this->error = $e->getMessage();
  190. Db::rollback();
  191. return false;
  192. }
  193. }
  194. public function lists($page,$size,$userId,$orgId){
  195. $map[]=['user_id','=',$userId];
  196. $map[]=['org_id','=',$orgId];
  197. $ret=Db::name('g_orders')
  198. ->field('id,status,amount')
  199. ->where($map)
  200. ->order('id','desc')
  201. ->page($page,$size)
  202. ->select();
  203. foreach ($ret as $k=>$v){
  204. $orderGoods=Db::name('g_order_goods')
  205. ->alias('og')
  206. ->field('og.nums,og.price,og.goods_id,g.title,g.img')
  207. ->join('g_goods g','g.id=og.goods_id')
  208. ->where('og.order_id',$v['id'])
  209. ->select();
  210. $nums=0;
  211. foreach ($orderGoods as $kk=>$vv){
  212. $nums+=$vv['nums'];
  213. }
  214. $ret[$k]['num']=$nums;
  215. $ret[$k]['order_goods']=$orderGoods;
  216. }
  217. return $ret?$ret:[];
  218. }
  219. public function details($ordersId){
  220. $ret = Db::name('g_orders')->where('id', $ordersId)->find();
  221. $ret['goods'] = Db::name('g_order_goods')
  222. ->alias('og')
  223. ->field('og.nums,og.price,og.goods_id,g.title,g.img')
  224. ->join('g_goods g', 'g.id=og.goods_id')
  225. ->where('og.order_id', $ret['id'])
  226. ->select();
  227. $nums=0;
  228. foreach ($ret['goods'] as $k=>$v){
  229. $nums+=$v['nums'];
  230. }
  231. $ret['sum']=$nums;
  232. return $ret;
  233. }
  234. public function accomplish($ordersId){
  235. $data=[
  236. 'status'=>2,
  237. 'finish_time'=>date('Y-m-d H:i:s'),
  238. ];
  239. $ret = Db::name('g_orders')->where('id', $ordersId)->update($data);
  240. return $ret;
  241. }
  242. public function refundOrder($userId){
  243. $data = [
  244. 'money' => input('money/f',0),
  245. 'order_id' => input('id/d',0),
  246. 'user_id' => $userId,
  247. 'remark' => input('remark','','trim'),
  248. 'create_time' => date('Y-m-d H:i:s'),
  249. ];
  250. if($data['money'] <= 0){
  251. $this->error = '金额必须大于0';
  252. return false;
  253. }
  254. $pay = Db::name('g_orders')
  255. ->where('id',$data['order_id'])->find();
  256. if(!$pay||$pay['status'] != 1){
  257. $this->error = '订单不存在';
  258. return false;
  259. }
  260. $data['sn'] = get_unique_id('TPH');
  261. $data['org_id'] = $pay['org_id'];
  262. if($data['money'] > $pay['amount']){
  263. $this->error = '退款金额过大';
  264. return false;
  265. }
  266. Db::startTrans();
  267. try{
  268. $ret = Db::name('g_order_refund')->insert($data);
  269. if(!$ret){
  270. \exception('操作失败');
  271. }
  272. $order = Db::name('g_orders')
  273. ->where('id',$pay['id'])
  274. ->lock(true)
  275. ->find();
  276. if(!$order){
  277. \exception('订单不存在');
  278. }
  279. $ret = Db::name('g_orders')
  280. ->where('id',$pay['id'])->update([
  281. 'refund_money' => $data['money'],
  282. 'status' => 4
  283. ]);
  284. if(!$ret){
  285. \exception('操作失败');
  286. }
  287. $config = config('app.wx_config');
  288. $app = \EasyWeChat\Factory::payment($config);
  289. $ret = $app->refund->byOutTradeNumber($pay['order_sn'], $data['sn'], $pay['amount']*100, $data['money']*100);
  290. if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
  291. \exception('退款申请提交失败');
  292. }
  293. Db::commit();
  294. return true;
  295. }catch (\Exception $e){
  296. Db::rollback();
  297. trace($e->getMessage());
  298. $this->error = $e->getMessage();
  299. return false;
  300. }
  301. }
  302. }