LedgerWalletModel.php 9.9 KB

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