Order.php 13 KB

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