LedgerWalletModel.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace app\common\model;
  3. use app\admin\model\LedgerDeclarationChange;
  4. use Exception;
  5. use fast\Action;
  6. use fast\Asset;
  7. use think\Db;
  8. use think\exception\DbException;
  9. use think\Model;
  10. use think\Log;
  11. class LedgerWalletModel extends Model
  12. {
  13. protected $name = "ledger_wallet";
  14. // 自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'create_time';
  18. protected $updateTime = 'update_time';
  19. protected $deleteTime = false;
  20. public static function getWalletChaBao($userID)
  21. {
  22. return self::where('user_id', $userID)->value('token');
  23. }
  24. public static function getWalletFrozen($userID)
  25. {
  26. return self::where('user_id', $userID)->value('frozen');
  27. }
  28. public static function getWalletBuying($userID)
  29. {
  30. return self::where('user_id', $userID)->value('buying');
  31. }
  32. public static function getWalletTotalChaBao($userID)
  33. {
  34. return self::where('user_id', $userID)->value('token + frozen');
  35. }
  36. public static function getWallet($userID)
  37. {
  38. return self::where('user_id', $userID)->find();
  39. }
  40. /**
  41. * 仅事务内使用,会附带"for update"
  42. * @param $userID
  43. * @throws Exception
  44. */
  45. public function getWalletTX($userID)
  46. {
  47. return $this->lock(true)->where('user_id', $userID)->find();
  48. }
  49. /**
  50. * 更新钱包余额并添加账变记录
  51. * @param int $uid 用户ID
  52. * @param string $asset 资产类型
  53. * @param string $amount 金额 正:表示加 负:表示减
  54. * @param int $action 账变类型
  55. * @return void
  56. * @throws Exception
  57. */
  58. public function changeWalletAccount(int $uid, string $asset, string $amount, int $action, int $from_id = 0)
  59. {
  60. $available = $this->getWallet($uid);
  61. if (empty($available)) (new LedgerWalletModel())->insertGetId(['user_id' => $uid]); // 创建钱包
  62. $frozen = ''; //冻结金额
  63. // 账变资产模型
  64. switch ($asset) {
  65. case Asset::USDT:
  66. $changeModel = new LedgerUsdtChangeModel();
  67. break;
  68. case Asset::TOKEN:
  69. $changeModel = new LedgerTokenChangeModel();
  70. break;
  71. case Asset::TEAC:
  72. $changeModel = new LedgerTeacChangeModel();
  73. break;
  74. case Asset::SMH:
  75. $changeModel = new LedgerSmhChangeModel();
  76. break;
  77. case Asset::FROZEN:
  78. //扣掉赠送手续费
  79. $changeModel = new LedgerFrozenChangeModel();
  80. break;
  81. default:
  82. throw new Exception('币种错误:' . $asset);
  83. }
  84. // 更新钱包余额
  85. $newAmount = $this->changeWalletOnly($uid, $asset, $amount);
  86. // 创建账变记录
  87. $insertRs = $changeModel->insert([
  88. 'user_id' => $uid,
  89. 'from_id' => $from_id,
  90. 'change_amount' => $amount,
  91. 'present_amount' => $newAmount,
  92. 'create_time' => time(),
  93. 'action' => $action
  94. ]);
  95. if (empty($insertRs)) {
  96. throw new Exception('创建' . $asset . '账变记录失败');
  97. }
  98. }
  99. /**
  100. * 仅更新钱包余额,没有账变
  101. * @param int $uid 用户ID
  102. * @param string $asset 资产类型
  103. * @param string $amount 金额 正:表示加 负:表示减
  104. * @return string 变动后的金额
  105. * @throws Exception
  106. */
  107. public function changeWalletOnly(int $uid, string $asset, string $amount): string
  108. {
  109. $available = $this->getWalletTX($uid);
  110. if (empty($available)) {
  111. throw new Exception('用户资产信息不存在');
  112. }
  113. // 金额为0的判断
  114. $amount = bcadd($amount, 0, 6);
  115. if (bccomp($amount, '0', 6) == 0) {
  116. throw new Exception('金额变动为0');
  117. }
  118. // 余额不足的判断
  119. if ($amount < 0 && $available[$asset] < -$amount) {
  120. throw new Exception(Asset::getAssetName($asset) . '余额不足');
  121. }
  122. $newAmount = bcadd($available[$asset], $amount, 6); // 新余额
  123. $walletUpdate = [$asset => $newAmount, 'update_time' => time()];
  124. // 更新余额
  125. $changeRs = $this->save($walletUpdate, ['user_id' => $uid]);
  126. if (empty($changeRs)) {
  127. throw new Exception('更新' . $asset . '余额失败');
  128. }
  129. return $newAmount;
  130. }
  131. /**
  132. * 冻结茶宝
  133. * @param int $uid 用户ID
  134. * @param string $amount 报单金额
  135. * @return void
  136. * @throws Exception
  137. */
  138. public function setChangeFrozen(int $uid, string $amount, int $action, string $icn, int $from_id = 0)
  139. {
  140. $available = $this->getWallet($uid);
  141. $ledgerWalletModel = new LedgerWalletModel();
  142. if (empty($available)) $ledgerWalletModel->insertGetId(['user_id' => $uid]); // 创建钱包
  143. $walletUpdate = [];
  144. $result = false;
  145. //运费/手续费:扣除
  146. if($icn == '-'){
  147. $totalAmount = bcadd($available['token'], $available['frozen'], 6);
  148. //扣除冻结金额
  149. if($available['frozen'] > $amount) $walletUpdate['frozen'] = bcsub($available['frozen'], $amount, 6);
  150. if($available['frozen'] <= $amount) {
  151. $walletUpdate = ['token'=> bcadd($totalAmount, -$amount, 6), 'frozen' => 0];
  152. //添加账变记录
  153. $subAmount = bcsub($amount, $available['frozen'], 6);
  154. $this->changeWalletAccount($uid, Asset::TOKEN, -$subAmount, LedgerTokenChangeModel::Giveaway, $from_id);
  155. }
  156. //添加冻结
  157. $newAmount = $walletUpdate['frozen'];
  158. if($available['frozen'] > 0) $result = true;
  159. }
  160. //手续费
  161. if($icn == '+' && $available['frozen'] < $amount){
  162. //添加账变记录
  163. if($available->token < $amount) throw new Exception('茶宝不足');
  164. $sunAmount = bcsub($available->frozen, $amount, 6); //扣除的冻结金额
  165. $chabao = bcadd($available['token'], $sunAmount, 6);
  166. $newAmount = $amount; //剩余的冻结金额
  167. //添加冻结
  168. $walletUpdate =['token' => $chabao, 'frozen' =>$newAmount];
  169. $this->changeWalletAccount($uid, Asset::TOKEN, $sunAmount, LedgerTokenChangeModel::Super, $from_id);
  170. $result = true;
  171. }
  172. // 创建账变记录
  173. if (count($walletUpdate) > 0) $ledgerWalletModel->where('user_id', $uid)->update($walletUpdate);
  174. if($result){
  175. return (new LedgerFrozenChangeModel())->insert([
  176. 'user_id' => $uid,
  177. 'from_id' => $from_id,
  178. 'change_amount' => $icn.$amount,
  179. 'present_amount' => $newAmount,
  180. 'create_time' => time(),
  181. 'action' => $action
  182. ]);
  183. }
  184. }
  185. /**
  186. * 发放直推USDT收益
  187. * @param int $uid
  188. * @param string $power
  189. * @return void
  190. * @throws DbException
  191. */
  192. public function sendUsdtProfit(int $uid, string $amount)
  193. {
  194. $ratio = (new Config())->getValue('direct_income');//直推奖励
  195. $user = (new UserModel())->get($uid);
  196. if (empty($user) || $user['parent_id'] == 0) {
  197. return;
  198. }
  199. $parent = (new UserModel())->get($user['parent_id']);
  200. if (empty($parent)) {
  201. return;
  202. }
  203. $temp = ['拦截提醒', $uid, $amount, $amount * $ratio];
  204. //dump($temp);
  205. // 上级算力的账变
  206. $this->changeWalletAccount($user['parent_id'], Asset::USDT, $amount * $ratio, Action::UsdtShareBonus, $uid);
  207. }
  208. /**
  209. * 发放直推收益
  210. * @param int $uid
  211. * @param string $power
  212. * @return void
  213. * @throws DbException
  214. */
  215. public function sendDirectProfit(int $uid, string $power)
  216. {
  217. $user = (new UserModel())->get($uid);
  218. if (empty($user) || $user['parent_id'] == 0) {
  219. return;
  220. }
  221. $parent = (new UserModel())->get($user['parent_id']);
  222. if (empty($parent)) {
  223. return;
  224. }
  225. // 查询直推奖励比例
  226. $directProfit = (new Config())->getValue('direct_income');
  227. $directProfitFloat = floatval($directProfit);
  228. if (is_null($directProfit) || $directProfitFloat <= 0) {
  229. return;
  230. }
  231. $directPower = $power * $directProfitFloat; // 可获得的直推奖励(算力)
  232. if($directPower > 0){
  233. $this->changeWalletAccount($user['parent_id'], Asset::POWER, $directPower, Action::PowerDirectAward, $uid);
  234. }
  235. // 上级算力的账变
  236. }
  237. public function users()
  238. {
  239. return $this->hasOne(UserModel::class, 'id', 'user_id');
  240. }
  241. public function getCreateTimeTextAttr($value, $data)
  242. {
  243. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  244. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  245. }
  246. public function getUpdateTimeTextAttr($value, $data)
  247. {
  248. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  249. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  250. }
  251. protected function setCreateTimeAttr($value)
  252. {
  253. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  254. }
  255. protected function setUpdateTimeAttr($value)
  256. {
  257. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  258. }
  259. }