Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\ProductOrder;
  5. use app\common\model\ProductPopular;
  6. use app\common\model\LedgerFrozenChangeModel;
  7. use app\common\model\LedgerWalletModel;
  8. use app\common\model\UserArea;
  9. use app\common\model\UserModel;
  10. use app\common\model\LedgerTokenChangeModel;
  11. use app\common\model\ProductTransfer;
  12. use app\common\model\OfflineRechargeRecordModel;
  13. use app\api\logic\OrderLogic;
  14. use app\api\logic\MarketLogic;
  15. use app\api\logic\WelfareLoginc;
  16. use Exception;
  17. use fast\Asset;
  18. use think\Db;
  19. use think\Env;
  20. /**
  21. * 订单关联
  22. */
  23. class Order extends Api
  24. {
  25. /**
  26. * 创建订单
  27. */
  28. public function create(ProductPopular $productPopular, OrderLogic $orderLogic, LedgerWalletModel $ledgerWalletModel, UserModel $userModel)
  29. {
  30. $params = $this->request->post();
  31. $validate = \think\Loader::validate('Order');
  32. if(!$validate->scene('add')->check($params)) $this->error($validate->getError());
  33. $order_info = $productPopular->where('id', $params['order_id'])->find();
  34. if(empty($order_info)) $this->error(__("参数有误,无可用产品"));
  35. $areaArr = ($params['type'] == 1)? explode(',', $params['area_id']): [];
  36. $areaNum = ($params['type'] == 1)? count($areaArr): $params['num'];
  37. if(($order_info->num +$order_info->init_num+ $areaNum) > $order_info->stock) $this->error(__("库存不足"));
  38. $result = false;
  39. Db::startTrans();
  40. try {
  41. $amount = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  42. $totalPrice = bcmul($order_info['price'], $areaNum, 2);
  43. if(bccomp($totalPrice, $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  44. if($order_info->start_time > time()) throw new Exception(__("抢购未开始"));
  45. if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束"));
  46. //批量地区添加 1选择地区 2未选择地区
  47. $result = $orderLogic::createOrderByType($params['type'], $areaArr, $params['order_id'], $order_info->price, $params['product_id'], $this->auth->id, $areaNum);
  48. //余额记录
  49. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$totalPrice, LedgerTokenChangeModel::Popular, $this->auth->id);
  50. //直推收益: pv* ×10%
  51. if($order_info['pv'] > 0 && $this->auth->parent_id > 0 && $userModel::getUserRwaNum($this->auth->parent_id) > 0){
  52. $pv = bcmul(($order_info['pv'] * $areaNum), getConfig('pv_rate'), 2);
  53. if($pv > 0) $ledgerWalletModel->changeWalletAccount($this->auth->parent_id, Asset::TOKEN, $pv, LedgerTokenChangeModel::Direct, $this->auth->id);
  54. //社区奖励
  55. $pvs = bcmul(($order_info['pv'] * $areaNum), config('community_ratio'), 2);
  56. if($pvs > 0)$userModel::setCommunityRewards($this->auth->id, $pvs, Asset::TOKEN);
  57. }
  58. //生态节点标识
  59. WelfareLoginc::setUserEcologyAirdrop($this->auth->id, $params['product_id'], $this->auth->parent_id);
  60. //更新Rwa持有数量
  61. if(!empty($order_info->is_show) && $order_info->price > config('min_rwa_price')) $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, $areaNum, '+');
  62. //扣除库存
  63. if(($order_info->stock - $areaNum) == 0 || time() >= $order_info->end_time) $order_info->status= $productPopular::Stop;
  64. $order_info->num += $areaNum;
  65. $order_info->save();
  66. if (false === $result) $this->error(__('No rows were updated'));
  67. // 提交事务
  68. Db::commit();
  69. } catch (Exception $e) {
  70. // 回滚事务
  71. Db::rollback();
  72. $this->error($e->getMessage(), null, $e->getCode());
  73. }
  74. $this->success('ok');
  75. }
  76. /**
  77. * 提货订单
  78. */
  79. public function pickupOrder(UserArea $userArea, ProductOrder $productOrder, UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  80. {
  81. $params = $this->request->post();
  82. $validate = \think\Loader::validate('Order');
  83. if(!$validate->scene('pick')->check($params)) $this->error($validate->getError());
  84. $order_info = $productOrder::getProductOrder($params['order_id'], productOrder::Paid, 'b.freight,b.is_freight,b.is_show');
  85. if(empty($order_info) || empty($order_info->is_freight)) $this->error( __("参数有误,无可用产品"));
  86. // 启动事务
  87. Db::startTrans();
  88. try {
  89. $amount = $ledgerWalletModel::getWalletTotalChaBao($this->auth->id);
  90. if(bccomp($order_info->freight, $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  91. // 生成订单
  92. $userArea->create(['name'=>$params['name'], 'type_id'=>$userArea::TakeAdders, 'phone'=>$params['phone'],'address'=>$params['address'],'order_id'=>$params['order_id']]);
  93. //扣除Rwa数量
  94. if(!empty($order_info->is_show) && $order_info->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($order_info->user_id, $userModel::getByParentId($order_info->user_id), 1, '-');
  95. $order_info->status= $productOrder::Shipped;
  96. $order_info->save();
  97. // 提交事务
  98. Db::commit();
  99. } catch (Exception $e) {
  100. // 回滚事务
  101. Db::rollback();
  102. $this->error( $e->getMessage());
  103. }
  104. $this->success('ok');
  105. }
  106. /**
  107. * 添加订单寄售转让
  108. * @return void
  109. */
  110. public function transfer(ProductOrder $productOrder, MarketLogic $marketLogic)
  111. {
  112. $params = $this->request->post();
  113. $validate = \think\Loader::validate('Order');
  114. if(!$validate->scene('tran')->check($params)) $this->error($validate->getError());
  115. //启动事务
  116. Db::startTrans();
  117. try {
  118. $order_info = $productOrder::getProductOrder($params['order_id'], productOrder::Paid, 'b.is_transfer,b.min_transfer_fee,b.max_transfer_fee');
  119. if(empty($order_info) || empty($order_info->is_transfer)) throw new Exception(__("订单不存在"));
  120. if($params['price'] < $order_info->min_transfer_fee || $params['price'] > $order_info->max_transfer_fee) throw new Exception(__("当前订单转让金额为").$order_info->min_transfer_fee.'~'.$order_info->max_transfer_fee);
  121. //转让订单
  122. $marketLogic::createTransferOrder($params['price'], $order_info['product_id'], $order_info['area_id'], $order_info['order_no'], $this->auth->id, $params);
  123. //修改 类型状态
  124. $order_info->type_id = $productOrder::Transfer;
  125. $order_info->status = $productOrder::Transferred;
  126. $order_info->save();
  127. Db::commit();
  128. } catch (Exception $e) {
  129. Db::rollback();
  130. $this->error($e->getMessage());
  131. }
  132. $this->success('ok');
  133. }
  134. /**
  135. * 转让订单购买
  136. * @return void
  137. */
  138. public function transferOrder(OrderLogic $orderLogic)
  139. {
  140. $params = $this->request->post();
  141. $validate = \think\Loader::validate('Order');
  142. if(!$validate->scene('out')->check($params)) $this->error($validate->getError());
  143. //启动事务
  144. Db::startTrans();
  145. try {
  146. $orderLogic::getProductTransferOrder($params['order_id'], $this->auth->id, $this->auth->parent_id);
  147. Db::commit();
  148. } catch (Exception $e) {
  149. Db::rollback();
  150. $this->error($e->getMessage());
  151. }
  152. $this->success('ok');
  153. }
  154. //赠送
  155. public function giveaway(ProductOrder $productOrder, UserModel $userModel, LedgerWalletModel $ledgerWalletModel, ProductTransfer $productTransfer)
  156. {
  157. $params = $this->request->post();
  158. $validate = \think\Loader::validate('Order');
  159. if(!$validate->scene('giv')->check($params)) $this->error($validate->getError());
  160. // 启动事务
  161. Db::startTrans();
  162. try {
  163. $order_info = $productOrder::getProductOrder($params['order_id'], $productOrder::Paid, 'b.is_gift,b.gift_fee,b.is_show');
  164. if(empty($order_info) || empty($order_info->is_gift)) throw new Exception(__("参数有误,无可用产品"));
  165. $user = $userModel->getByAddress($params['address']);
  166. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  167. if($user['id'] == $order_info['user_id']) throw new Exception(__("赠送用户不能是自己"));
  168. //手续费
  169. $price = $order_info['price'] > 0? $order_info['price']: $productTransfer::getTransferMinPrice($order_info->product_id);
  170. $fees = bcmul($price, bcdiv($order_info->gift_fee, 100, 3), 2);
  171. //余额记录
  172. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  173. if(bccomp($fees, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  174. //添加记录
  175. $productOrder::setCreateOrder($params['order_id'], $order_info, $productOrder::Giveaway, $user['id'], $this->auth->id, $order_info->order_no, $fees, $order_info->popular_price);
  176. //对方Rwa+1
  177. if(!empty($order_info->is_show) && $order_info->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($user['id'], $userModel::getByParentId($user['id']), 1, '+');
  178. //扣除手续费
  179. if($fees > 0) $ledgerWalletModel->setChangeFrozen($this->auth->id, $fees, LedgerFrozenChangeModel::Giveaway, '-');
  180. //扣除Rwa有效-1
  181. if(!empty($order_info->is_show) && $order_info->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, 1, '-');
  182. //修改:类型状态
  183. $order_info->type_id= $productOrder::Giveaway;
  184. $order_info->status = $productOrder::Closure;
  185. $order_info->save();
  186. // 提交事务
  187. Db::commit();
  188. } catch (Exception $e) {
  189. // 回滚事务
  190. Db::rollback();
  191. $this->error( $e->getMessage());
  192. }
  193. $this->success('ok');
  194. }
  195. //取消转让
  196. public function cancel(ProductOrder $productOrder, MarketLogic $marketLogic)
  197. {
  198. $params = $this->request->post();
  199. $validate = \think\Loader::validate('Order');
  200. if(!$validate->scene('out')->check($params)) $this->error($validate->getError());
  201. // 启动事务
  202. Db::startTrans();
  203. try {
  204. $order_info = $productOrder->where('id', $params['order_id'])->find();
  205. if(empty($order_info)) throw new Exception(__("参数有误,无可用产品"));
  206. //取消转让订单
  207. $marketLogic::cancelTransferOrder($params['order_id'], $order_info['user_id'], $order_info);
  208. //修改:类型状态
  209. $order_info->type_id= $productOrder::Transfer;
  210. $order_info->status = $productOrder::Cancelled;
  211. $order_info->save();
  212. // 提交事务
  213. Db::commit();
  214. } catch (Exception $e) {
  215. // 回滚事务
  216. Db::rollback();
  217. $this->error( $e->getMessage());
  218. }
  219. $this->success('ok');
  220. }
  221. //查看快递信息
  222. public function getTracking(UserArea $userArea)
  223. {
  224. $order_id = $this->request->post('order_id'); // 订单id
  225. if(empty($order_id)) $this->error(__("参数有误,无可用产品"));
  226. $tracking_no = $userArea->where('order_id', $order_id)->where('type_id', $userArea::TakeAdders)->value('tracking_no');
  227. if(empty($tracking_no)) $this->error(__("暂无物流信息"));
  228. $this->success('ok', $tracking_no);
  229. }
  230. /**
  231. * 更新订单hash
  232. * @return void
  233. */
  234. public function updateOrder()
  235. {
  236. $amount = $this->request->post('amount'); // 支付金额
  237. $tx_hash = $this->request->post('tx_hash'); // 交易hash
  238. if (empty($amount)) $this->error(__('交易金额不能为空'));
  239. if (empty($tx_hash)) $this->error(__('交易Hash不能为空'));
  240. //用户id、用户地址、hash、金额、状态、时间 address
  241. Db::startTrans();
  242. try {
  243. //更新订单支付状态为 待确认
  244. OfflineRechargeRecordModel::create([
  245. 'order_no' => $this->auth->id . substr((string)time(), -8),//会员ID+时间戳后8位
  246. 'user_id' => $this->auth->id,
  247. 'amount' => $amount,
  248. 'symbol' => Asset::USDT,
  249. 'status' => OfflineRechargeRecordModel::StatusConfirm,
  250. 'tx_hash' => $tx_hash,
  251. 'from_address' => $this->auth->address,
  252. 'to_address' => Env::get('rental.pay_address'),
  253. 'cha_bao' => getConfig('chabao_rate') * $amount
  254. ]);
  255. // 提交事务
  256. Db::commit();
  257. } catch (Exception $e) {
  258. // 回滚事务
  259. Db::rollback();
  260. $this->error( $e->getMessage());
  261. }
  262. $this->success('ok');
  263. }
  264. }