TeacLogin.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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('price', $price)->where('status', ProductTeac::Normal)->find();
  29. $fee = config('teac_trade.'. self::$feeList[$typeId]);
  30. if($rows) {
  31. $total_price = bcmul($price, $stock, 4);
  32. $rows->fee += bcmul($total_price, $fee, 4);
  33. $rows->frozen += $frozen;
  34. $rows->stock += $stock;
  35. return $rows->save();
  36. }else{
  37. //添加订单信息
  38. return ProductTeac::setUserCreateOrder($uid, $typeId, $price, $stock, $fee, $frozen);
  39. }
  40. }
  41. /**
  42. * 购买出售
  43. * @$uid 用户人id
  44. * @$fromUid 来源订单id
  45. */
  46. public static function setCreateSellOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
  47. {
  48. $ledgerWalletModel = Loader::model('LedgerWalletModel');
  49. $total_price = bcmul($chabao, $num, 4); //总价
  50. $fee = bcmul($total_price, $fee, 4); //手续费
  51. //扣除用户茶宝
  52. $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, -$total_price, LedgerTokenChangeModel::BuySellg, $fromUid);
  53. //添加用户Teac
  54. $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, $num, LedgerTeacChangeModel::SellBuy, $fromUid);
  55. //添加来源茶宝
  56. $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TOKEN, bcsub($total_price, $fee, 4), LedgerTokenChangeModel::BuySellg, $uid);
  57. return true;
  58. }
  59. /**
  60. * 求购出售
  61. * @$uid 用户人id
  62. * @$fromUid 来源订单id
  63. */
  64. public static function setCreateBuyingOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
  65. {
  66. $ledgerWalletModel = Loader::model('LedgerWalletModel');
  67. $total_price = bcmul($chabao, $num, 4); //总价
  68. $fee = bcmul($total_price, $fee, 4); //手续费
  69. //添加用户茶宝
  70. $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, bcsub($total_price, $fee, 4), LedgerTokenChangeModel::BuySellg, $fromUid);
  71. //扣除用户Teac
  72. $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, -$num, LedgerTeacChangeModel::BuySell, $fromUid);
  73. //添加Teac
  74. $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TEAC, $num, LedgerTeacChangeModel::Buying, $uid);
  75. return true;
  76. }
  77. /**
  78. * 退回相应茶宝/Teac
  79. * @param $uid 用户id
  80. * @param $typeId 订单类型
  81. * @param $stock 剩余数
  82. * @return void
  83. */
  84. public static function setUserReturnOrder(int $uid, int $typeId, float $stock)
  85. {
  86. if($typeId == ProductTeac::Sell) {
  87. $asset = Asset::TEAC ;
  88. $action = LedgerTeacChangeModel::SellCancel;
  89. }else{
  90. $asset = Asset::TOKEN ;
  91. $action = LedgerTokenChangeModel::BuyCancel;
  92. }
  93. return Loader::model('LedgerWalletModel')->changeWalletAccount($uid, $asset, $stock, $action, $uid);
  94. }
  95. }