User.php 5.7 KB

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