Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\ProductTransfer;
  5. use app\common\model\ProductOrder;
  6. use app\common\model\ProductPopular;
  7. use app\common\model\ProductArea;
  8. use app\common\model\ProductLists;
  9. use app\common\model\LedgerWalletModel;
  10. use app\common\model\UserArea;
  11. use app\common\model\UserModel;
  12. use app\common\model\OfflineRechargeRecordModel;
  13. use Exception;
  14. use fast\Asset;
  15. use think\Db;
  16. use think\Env;
  17. /**
  18. * 订单关联
  19. */
  20. class Order extends Api
  21. {
  22. /**
  23. * 创建订单
  24. */
  25. public function create(ProductPopular $productPopular, ProductOrder $productOrder, ProductArea $productArea, LedgerWalletModel $ledgerWalletModel)
  26. {
  27. $params = $this->request->post();
  28. $validate = \think\Loader::validate('Order');
  29. if(!$validate->scene('add')->check($params)) $this->error($validate->getError());
  30. $order_info = $productPopular->where('id', $params['order_id'])->find();
  31. if(empty($order_info)) $this->error(__("参数有误,无可用产品"));
  32. $areaArr = explode(',', $params['area_id']);
  33. $areaNum = count($areaArr);
  34. if(($order_info->num + $areaNum) > $order_info->stock) $this->error(__("库存不足"));
  35. // 启动事务
  36. Db::startTrans();
  37. try {
  38. $amount = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  39. $totalPrice = bcmul($order_info['price'], $areaNum, 2);
  40. if(bccomp($totalPrice, $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  41. if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束"));
  42. //批量地区添加
  43. foreach ($areaArr as $item) {
  44. $order_data['order_id'] = $params['order_id'];
  45. $order_data['product_id']= $params['product_id'];
  46. $order_data['price'] = $order_info['price'];
  47. $order_data['type_id'] = $productOrder::Popular;
  48. $order_data['area_id'] = $item;
  49. $order_data['order_no'] = getOrderSN('R');
  50. $order_data['user_id'] = $this->auth->id;
  51. $order_data['status'] = $productOrder::Paid;
  52. $order_data['num'] = 1;
  53. // 生成订单
  54. $productOrder->create($order_data);
  55. //修改区域状态
  56. $productArea->where('id', $item)->setField('status', ProductLists::STOP);
  57. }
  58. //余额记录
  59. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$totalPrice, $ledgerWalletModel::Popular, $this->auth->id);
  60. //直推收益: pv* ×10%
  61. if($order_info['pv'] > 0 && $this->auth->parent_id > 0 && $productOrder::getEffectiveOrder($this->auth->parent_id) > 0){
  62. $pv = bcmul($order_info['pv'], getConfig('pv_rate'), 2);
  63. $ledgerWalletModel->changeWalletAccount($this->auth->parent_id, Asset::TOKEN, $pv, $ledgerWalletModel::Direct, $this->auth->id);
  64. }
  65. //扣除库存
  66. if(($order_info->stock - $areaNum) == 0 || time() >= $order_info->end_time) $order_info->status= $productPopular::STOP;
  67. $order_info->num += $areaNum;
  68. $order_info->save();
  69. // 提交事务
  70. Db::commit();
  71. } catch (Exception $e) {
  72. // 回滚事务
  73. Db::rollback();
  74. $this->error($e->getMessage(), null, $e->getCode());
  75. }
  76. $this->success('ok');
  77. }
  78. /**
  79. * 提货订单
  80. */
  81. public function pickupOrder(UserArea $userArea, ProductOrder $productOrder)
  82. {
  83. $params = $this->request->post();
  84. $validate = \think\Loader::validate('Order');
  85. if(!$validate->scene('pick')->check($params)) $this->error($validate->getError());
  86. $order_info = $productOrder->where('id', $params['order_id'])->find();
  87. if(empty($order_info)) $this->error( __("参数有误,无可用产品"));
  88. $order_data['name'] = $params['name'];
  89. $order_data['phone'] = $params['phone'];
  90. $order_data['address'] = $params['address'];
  91. $order_data['order_id'] = $params['order_id'];
  92. // 启动事务
  93. Db::startTrans();
  94. try {
  95. // 生成订单
  96. $userArea->create($order_data);
  97. $order_info->status= $productOrder::Shipped;
  98. $order_info->save();
  99. // 提交事务
  100. Db::commit();
  101. } catch (Exception $e) {
  102. // 回滚事务
  103. Db::rollback();
  104. $this->error( $e->getMessage());
  105. }
  106. $this->success('ok');
  107. }
  108. /**
  109. * 订单转让
  110. * @return void
  111. */
  112. public function transfer(ProductOrder $productOrder, ProductTransfer $productTransfer)
  113. {
  114. $params = $this->request->post();
  115. $validate = \think\Loader::validate('Order');
  116. if(!$validate->scene('tran')->check($params)) $this->error($validate->getError());
  117. //启动事务
  118. Db::startTrans();
  119. try {
  120. $order_info = $productOrder->where('id', $params['order_id'])->where('status', $productOrder::Paid)->find();
  121. if(empty($order_info)) throw new Exception(__("订单不存在"));
  122. //转让订单
  123. $fee = getConfig('transfer_fee');
  124. $feeAmount = bcmul($params['price'], $fee, 2) ;
  125. $productTransfer::setTransferOrder($this->auth->id, $order_info['product_id'], $order_info['area_id'], $feeAmount, $params);
  126. //修改状态
  127. $order_info->status = $productOrder::Transferred;
  128. $order_info->save();
  129. Db::commit();
  130. } catch (Exception $e) {
  131. Db::rollback();
  132. $this->error('提交失败:' . $e->getMessage());
  133. }
  134. $this->success('ok');
  135. }
  136. /**
  137. * 转让订单购买
  138. * @return void
  139. */
  140. public function transferOrder(ProductOrder $productOrder, ProductTransfer $productTransfer, LedgerWalletModel $ledgerWalletModel)
  141. {
  142. $params = $this->request->post();
  143. $validate = \think\Loader::validate('Order');
  144. if(!$validate->scene('out')->check($params)) $this->error($validate->getError());
  145. //启动事务
  146. Db::startTrans();
  147. try {
  148. $order_info = $productTransfer->where('id', $params['order_id'])->where('status', $productTransfer::NORMAL)->find();
  149. if(empty($order_info)) throw new Exception(__("订单不存在"));
  150. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  151. if(bccomp($order_info['price'], $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  152. $order_data['order_id'] = $params['order_id'];
  153. $order_data['product_id']= $order_info['product_id'];
  154. $order_data['type_id'] = $productOrder::Transfer;
  155. $order_data['status'] = $productOrder::Paid;
  156. $order_data['area_id'] = $order_info['area_id'];
  157. $order_data['order_no'] = getOrderSN('Z');
  158. $order_data['user_id'] = $this->auth->id;
  159. $order_data['from_user'] = $order_info['user_id'];
  160. $order_data['price'] = $order_info['price'];
  161. $order_data['fees'] = $order_info['fees'];
  162. $order_data['num'] = 1;
  163. // 生成订单
  164. $productOrder->create($order_data);
  165. //扣除余额记录
  166. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], $ledgerWalletModel::Payment, $order_info['user_id']);
  167. //增加转让人余额
  168. $amount = bcsub($order_info['price'], $order_info['fees'], 2);
  169. $ledgerWalletModel->changeWalletAccount($order_info['user_id'], Asset::TOKEN, $amount, $ledgerWalletModel::Receive, $this->auth->id);
  170. //修改原订单状态
  171. $productOrder->where('id', $order_info['order_id'])->setField('status', $productOrder::Closure);
  172. //修改状态
  173. $order_info->status = $productTransfer::STOP;
  174. $order_info->save();
  175. Db::commit();
  176. } catch (Exception $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage(), null, $e->getCode());
  179. }
  180. $this->success('ok');
  181. }
  182. //赠送
  183. public function giveaway(ProductOrder $productOrder, UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  184. {
  185. $params = $this->request->post();
  186. $validate = \think\Loader::validate('Order');
  187. if(!$validate->scene('giv')->check($params)) $this->error($validate->getError());
  188. // 启动事务
  189. Db::startTrans();
  190. try {
  191. $order_info = $productOrder->where('id', $params['order_id'])->where('status', $productOrder::Paid)->find();
  192. if(empty($order_info)) throw new Exception(__("参数有误,无可用产品"));
  193. $user = $userModel->getByAddress($params['address']);
  194. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  195. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  196. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  197. $fees = bcmul($order_info['price'], getConfig('giveaway'), 2);
  198. if(bccomp($fees, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  199. //添加记录
  200. $order_data['order_id'] = $params['order_id'];
  201. $order_data['product_id']= $order_info['product_id'];
  202. $order_data['type_id'] = $productOrder::Giveaway;
  203. $order_data['status'] = $productOrder::Paid;
  204. $order_data['area_id'] = $order_info['area_id'];
  205. $order_data['order_no'] = getOrderSN('G');
  206. $order_data['user_id'] = $user['id'];
  207. $order_data['from_user'] = $this->auth->id;
  208. $order_data['price'] = $order_info['price'];
  209. $order_data['fees'] = $fees;
  210. $order_data['num'] = 1;
  211. $productOrder::create($order_data);
  212. //扣除手续费
  213. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$fees, $ledgerWalletModel::Giveaway, $user['id']);
  214. $order_info->status= $productOrder::Closure;
  215. $order_info->save();
  216. // 提交事务
  217. Db::commit();
  218. } catch (Exception $e) {
  219. // 回滚事务
  220. Db::rollback();
  221. $this->error( $e->getMessage(), null, $e->getCode());
  222. }
  223. $this->success('ok');
  224. }
  225. //取消转让
  226. public function cancel(ProductOrder $productOrder, ProductTransfer $productTransfer)
  227. {
  228. $params = $this->request->post();
  229. $validate = \think\Loader::validate('Order');
  230. if(!$validate->scene('out')->check($params)) $this->error($validate->getError());
  231. // 启动事务
  232. Db::startTrans();
  233. try {
  234. $order_info = $productOrder->where('id', $params['order_id'])->find();
  235. if(empty($order_info)) throw new Exception(__("参数有误,无可用产品"));
  236. //转让列表取消
  237. $productTransfer::where('order_id',$params['order_id'])->setField('status', $productTransfer::STOP);
  238. $order_info->status= $productOrder::Paid;
  239. $order_info->save();
  240. // 提交事务
  241. Db::commit();
  242. } catch (Exception $e) {
  243. // 回滚事务
  244. Db::rollback();
  245. $this->error( $e->getMessage());
  246. }
  247. $this->success('ok');
  248. }
  249. //查看快递信息
  250. public function getTracking(UserArea $userArea)
  251. {
  252. $order_id = $this->request->post('order_id'); // 订单id
  253. if(empty($order_id)) $this->error(__("参数有误,无可用产品"));
  254. $tracking_no = $userArea->where('order_id', $order_id)->value('tracking_no');
  255. if(empty($tracking_no)) $this->error(__("暂无物流信息"));
  256. $this->success('ok', $tracking_no);
  257. }
  258. /**
  259. * 更新订单hash
  260. * @return void
  261. */
  262. public function updateOrder()
  263. {
  264. $amount = $this->request->post('amount'); // 支付金额
  265. $tx_hash = $this->request->post('tx_hash'); // 交易hash
  266. if (empty($amount)) $this->error(__('交易金额不能为空'));
  267. if (empty($tx_hash)) $this->error(__('交易Hash不能为空'));
  268. //用户id、用户地址、hash、金额、状态、时间 address
  269. Db::startTrans();
  270. try {
  271. //更新订单支付状态为 待确认
  272. $order_update = OfflineRechargeRecordModel::create([
  273. 'user_id' => $this->auth->id,
  274. 'amount' => $amount,
  275. 'status' => OfflineRechargeRecordModel::StatusConfirm,
  276. 'tx_hash' => $tx_hash,
  277. 'from_address' => $this->auth->address,
  278. 'to_address' => Env::get('rental.pay_address'),
  279. 'cha_bao' => getConfig('chabao_rate') * $amount
  280. ]);
  281. // 提交事务
  282. Db::commit();
  283. } catch (Exception $e) {
  284. // 回滚事务
  285. Db::rollback();
  286. $this->error( $e->getMessage());
  287. }
  288. $this->success('ok');
  289. }
  290. }