User.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace App\Module\Fund\Service;
  3. use App\Module\Fund\CoreService;
  4. use App\Module\Fund\Enums\FUND_TYPE;
  5. use App\Module\Fund\Enums\LOG_TYPE;
  6. use App\Module\Fund\Models\FundLogModel;
  7. use App\Module\Fund\Models\FundModel;
  8. use App\Module\Fund\Services\FundService;
  9. use App\Module\Ulogic\Model\WalletAddress;
  10. use UCore\Helper\Logger;
  11. /**
  12. * Class User 用户的资金操作
  13. *
  14. * @package App\Logic\Fund\Service
  15. */
  16. class User extends CoreService
  17. {
  18. /**
  19. * 列表
  20. *
  21. * @param $where
  22. * @param $page
  23. * @param int $rows
  24. */
  25. public static function fund_list($where, $page, $rows = 10)
  26. {
  27. $Model = new FundModel();
  28. $builder = $Model->get_list_builder($where);
  29. $paginator = new \Phalcon\Paginator\Adapter\QueryBuilder(
  30. [
  31. "builder" => $builder,
  32. "limit" => $rows,
  33. "page" => $page,
  34. ]
  35. );
  36. return $paginator->getPaginate();
  37. }
  38. /**
  39. * 用户资金修改日志
  40. *
  41. * @param $user_id 用户uid
  42. * @param $page 当前页数
  43. * @param int $rows 每页行数
  44. */
  45. public static function log_list($user_id, $fund_id, $where, $page, $rows = 10)
  46. {
  47. }
  48. /**
  49. *
  50. * 资金处理
  51. *
  52. * @param $user_id
  53. * @param $fund_id
  54. * @param $amount
  55. * @param $type
  56. * @param $id
  57. * @param $remark
  58. * @return bool|string
  59. */
  60. public static function handle($user_id, int $fund_id, int $amount, LOG_TYPE $type, $id, $remark)
  61. {
  62. # 读取信息
  63. $data_Model = self::get_account($user_id, $fund_id);
  64. if ($data_Model === false) {
  65. return "_don~t have this account";
  66. }
  67. if (!($data_Model instanceof FundModel)) {
  68. return $data_Model;
  69. }
  70. # 先写入日志
  71. $re26 = self::log($data_Model, $user_id, $fund_id, $amount, $type, $id, $remark);
  72. if (is_string($re26)) {
  73. return $re26;
  74. }
  75. # 在更新资金信息
  76. $data_Model->update_time = time();
  77. $data_Model->balance = $data_Model->balance + $amount;
  78. if ($data_Model->balance < 0) {
  79. // 可用资金小于0
  80. Logger::error("fund -handle $user_id - $fund_id $amount end < 0");
  81. return "not-sufficient-funds $user_id - $fund_id : $amount ; {$data_Model->balance}";
  82. }
  83. if ($data_Model->save() === false) {
  84. return $data_Model->getMessage();
  85. }
  86. return true;
  87. }
  88. /**
  89. * 获取账户模型
  90. *
  91. * @param $user_id
  92. * @param $fund_id
  93. * @return fund|string
  94. */
  95. public static function get_account($user_id, $fund_id, $create = false)
  96. {
  97. $Model = FundModel::getAccount($user_id, $fund_id);
  98. if ($Model === null) {
  99. if ($create) {
  100. # 当前账户不存在尝试创建
  101. $Model = self::create_account($user_id, $fund_id);
  102. if (is_string($Model)) {
  103. // 创建失败
  104. return $Model;
  105. }
  106. } else {
  107. throw new \Exception("账户不存在$user_id - $fund_id .");
  108. }
  109. return FundModel::getAccount($user_id, $fund_id);
  110. }
  111. return $Model;
  112. }
  113. /**
  114. * 创建账户 不会进行账户是否存在验证
  115. *
  116. * @param $user_id
  117. * @param $fund_id
  118. * @return \Fund\Model\Fund|string
  119. */
  120. private static function create_account($user_id, $fund_id)
  121. {
  122. $data = [
  123. 'user_id' => $user_id,
  124. 'fund_id' => $fund_id,
  125. 'balance' => 0,
  126. 'create_time' => time(),
  127. 'update_time' => time(),
  128. ];
  129. $fundModel = new FundModel();
  130. $fundModel->setData($data);
  131. if ($fundModel->save() === false) {
  132. return 'sys error';
  133. }
  134. return $fundModel;
  135. }
  136. /**
  137. * 写入更新日志
  138. *
  139. * @param $user_id
  140. * @param $fund_id
  141. * @param $amount
  142. * @param $type
  143. * @param $id
  144. * @param $remark
  145. */
  146. private static function log(
  147. FundModel $data_Model,
  148. $user_id,
  149. $fund_id,
  150. $amount,
  151. LOG_TYPE $type,
  152. $id,
  153. $remark,
  154. $create_ip = ''
  155. ) {
  156. // 读取最新的余额信息
  157. $later_balance = $data_Model->balance + $amount;
  158. $before_balance = $data_Model->balance;
  159. # 读取最后一条记录进行验证
  160. $lastLog = FundLogModel::findLastUserFund($user_id, $fund_id);
  161. // dump($data8);
  162. # 读取最新的余额信息
  163. if ($lastLog === null) {
  164. $data8_arr = [];
  165. # 没有日志判断是否为0
  166. if (bccomp(0, $data_Model->balance, 3) !== 0) {
  167. # 前后效验不通过,
  168. return '_fund-log-validation-fails-201';
  169. }
  170. } else {
  171. // dump($lastLog);
  172. if (bccomp($lastLog->later_balance, $data_Model->balance, 3) !== 0) {
  173. # 前后效验不通过,
  174. Logger::error('fund-error', [$lastLog->toArray(), $data_Model->toArray()]);
  175. return '_fund-log-validation-fails-210';
  176. }
  177. }
  178. return Log::create_log($user_id, $fund_id, $amount, $remark, $later_balance, $before_balance, $id,
  179. $type->value(), $lastLog);
  180. }
  181. /**
  182. * 获取账户信息
  183. *
  184. * @param $user_id
  185. * @param $fund_id
  186. */
  187. public static function info($user_id, $fund_id)
  188. {
  189. $ModelData = self::get_account($user_id, $fund_id);
  190. return $ModelData->toArray();
  191. }
  192. /**
  193. * @param $userId
  194. * @param $fundIds
  195. * @return mixed[]
  196. * 获取用户全部资金账户
  197. */
  198. public static function getAllAccount($userId,$fundIds)
  199. {
  200. $modelData = FundModel::getAllAccount($userId,$fundIds);
  201. return $modelData->toArray();
  202. }
  203. }