LedgerWalletModel.php 10 KB

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