TeacLogin.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\model\ProductTeac;
  4. use fast\Http;
  5. use think\Loader;
  6. use Exception;
  7. use finfo;
  8. use think\Cache;
  9. use fast\Asset;
  10. use think\Log;
  11. use think\Lang;
  12. use app\common\model\LedgerTeacChangeModel;
  13. use app\common\model\LedgerTokenChangeModel;
  14. /**
  15. * Teac交易
  16. */
  17. class TeacLogin
  18. {
  19. protected static $feeList = [
  20. 1 => 'sell_serve_fee' , //出售服务费
  21. 2 => 'buy_serve_fee' , //求购服务费
  22. ];
  23. /**
  24. * 发布求购出售信息
  25. */
  26. public static function setCreateTrade(int $uid, float $price, int $stock, int $typeId, float $frozen):object
  27. {
  28. $rows = ProductTeac::where('user_id', $uid)->where('type_id', $typeId)->where('status', ProductTeac::Normal)->find();
  29. if($rows) throw new Exception(__("你有未完成的求购订单、不能重复发布"));
  30. //添加订单信息
  31. return ProductTeac::setUserCreateOrder($uid, $typeId, $price, $stock, config('teac_trade.'. self::$feeList[$typeId]), $frozen);
  32. }
  33. /**
  34. * 购买出售
  35. * @$uid 用户人id
  36. * @$fromUid 来源订单id
  37. */
  38. public static function setCreateSellOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
  39. {
  40. $ledgerWalletModel = Loader::model('LedgerWalletModel');
  41. $total_price = bcmul($chabao, $num, 2); //总价
  42. $fee = bcmul($total_price, $fee, 2); //手续费
  43. //扣除用户茶宝
  44. $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, -$total_price, LedgerTokenChangeModel::BuySellg, $fromUid);
  45. //添加用户Teac
  46. $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, $num, LedgerTeacChangeModel::SellBuy, $fromUid);
  47. //添加来源茶宝
  48. $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TOKEN, bcsub($total_price, $fee, 2), LedgerTokenChangeModel::BuySellg, $uid);
  49. return true;
  50. }
  51. /**
  52. * 求购出售
  53. * @$uid 用户人id
  54. * @$fromUid 来源订单id
  55. */
  56. public static function setCreateBuyingOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
  57. {
  58. $ledgerWalletModel = Loader::model('LedgerWalletModel');
  59. $total_price = bcmul($chabao, $num, 2); //总价
  60. $fee = bcmul($total_price, $fee, 2); //手续费
  61. //添加用户茶宝
  62. $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, bcsub($total_price, $fee, 2), LedgerTokenChangeModel::BuySellg, $fromUid);
  63. //扣除用户Teac
  64. $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, -$num, LedgerTeacChangeModel::BuySell, $fromUid);
  65. //添加Teac
  66. $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TEAC, $num, LedgerTeacChangeModel::Buying, $uid);
  67. return true;
  68. }
  69. /**
  70. * 退回相应茶宝/Teac
  71. * @param $uid 用户id
  72. * @param $typeId 订单类型
  73. * @param $stock 剩余数
  74. * @return void
  75. */
  76. public static function setUserReturnOrder(int $uid, int $typeId, int $stock)
  77. {
  78. if($typeId == ProductTeac::Sell) {
  79. $asset = Asset::TEAC ;
  80. $action = LedgerTeacChangeModel::SellCancel;
  81. }else{
  82. $asset = Asset::TOKEN ;
  83. $action = LedgerTokenChangeModel::BuyCancel;
  84. }
  85. return Loader::model('LedgerWalletModel')->changeWalletAccount($uid, $asset, $stock, $action, $uid);
  86. }
  87. }