Order.php 14 KB

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