| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace App\Module\Fund\Service;
- use App\Module\Fund\Enums\LOG_TYPE;
- use App\Module\Fund\Models\FundLogModel;
- use App\Module\Fund\Models\FundModel;
- use UCore\Helper\Logger;
- /**
- * Class User 用户的资金操作
- *
- * @package App\Logic\Fund\Service
- */
- class User
- {
- /**
- * 列表
- *
- * @param $where
- * @param $page
- * @param int $rows
- */
- public static function fund_list($where, $page, $rows = 10)
- {
- $Model = new FundModel();
- $builder = $Model->get_list_builder($where);
- $paginator = new \Phalcon\Paginator\Adapter\QueryBuilder(
- [
- "builder" => $builder,
- "limit" => $rows,
- "page" => $page,
- ]
- );
- return $paginator->getPaginate();
- }
- /**
- * 用户资金修改日志
- *
- * @param $user_id 用户uid
- * @param $page 当前页数
- * @param int $rows 每页行数
- */
- public static function log_list($user_id, $fund_id, $where, $page, $rows = 10)
- {
- }
- /**
- *
- * 资金处理
- *
- * @param $user_id
- * @param $fund_id
- * @param $amount
- * @param $type
- * @param $id
- * @param $remark
- * @return bool|string
- */
- public static function handle($user_id, int $fund_id, int $amount, LOG_TYPE $type, $id, $remark)
- {
- # 读取信息
- $data_Model = self::get_account($user_id, $fund_id);
- if ($data_Model === false) {
- return "_don~t have this account";
- }
- if (!($data_Model instanceof FundModel)) {
- return $data_Model;
- }
- # 先写入日志
- $re26 = self::log($data_Model, $user_id, $fund_id, $amount, $type, $id, $remark);
- if (is_string($re26)) {
- return $re26;
- }
- # 在更新资金信息
- $data_Model->update_time = time();
- $data_Model->balance = $data_Model->balance + $amount;
- if ($data_Model->balance < 0) {
- // 可用资金小于0
- Logger::error("fund -handle $user_id - $fund_id $amount end < 0");
- return "not-sufficient-funds $user_id - $fund_id : $amount ; {$data_Model->balance}";
- }
- if ($data_Model->save() === false) {
- return $data_Model->getMessage();
- }
- return true;
- }
- /**
- * 获取账户模型
- *
- * @param $user_id
- * @param $fund_id
- * @return fund|string
- */
- public static function get_account($user_id, $fund_id, $create = false)
- {
- $Model = FundModel::getAccount($user_id, $fund_id);
- if ($Model === null) {
- if ($create) {
- # 当前账户不存在尝试创建
- $Model = self::create_account($user_id, $fund_id);
- if (is_string($Model)) {
- // 创建失败
- return $Model;
- }
- } else {
- throw new \Exception("账户不存在$user_id - $fund_id .");
- }
- return FundModel::getAccount($user_id, $fund_id);
- }
- return $Model;
- }
- /**
- * 创建账户 不会进行账户是否存在验证
- *
- * @param $user_id
- * @param $fund_id
- * @return \Fund\Model\Fund|string
- */
- private static function create_account($user_id, $fund_id)
- {
- $data = [
- 'user_id' => $user_id,
- 'fund_id' => $fund_id,
- 'balance' => 0,
- 'create_time' => time(),
- 'update_time' => time(),
- ];
- $fundModel = new FundModel();
- $fundModel->setData($data);
- if ($fundModel->save() === false) {
- return 'sys error';
- }
- return $fundModel;
- }
- /**
- * 写入更新日志
- *
- * @param $user_id
- * @param $fund_id
- * @param $amount
- * @param $type
- * @param $id
- * @param $remark
- */
- private static function log(
- FundModel $data_Model,
- $user_id,
- $fund_id,
- $amount,
- LOG_TYPE $type,
- $id,
- $remark,
- $create_ip = ''
- ) {
- // 读取最新的余额信息
- $later_balance = $data_Model->balance + $amount;
- $before_balance = $data_Model->balance;
- # 读取最后一条记录进行验证
- $lastLog = FundLogModel::findLastUserFund($user_id, $fund_id);
- // dump($data8);
- # 读取最新的余额信息
- if ($lastLog === null) {
- $data8_arr = [];
- # 没有日志判断是否为0
- if (bccomp(0, $data_Model->balance, 3) !== 0) {
- # 前后效验不通过,
- return '_fund-log-validation-fails-201';
- }
- } else {
- // dump($lastLog);
- if (bccomp($lastLog->later_balance, $data_Model->balance, 3) !== 0) {
- # 前后效验不通过,
- Logger::error('fund-error', [$lastLog->toArray(), $data_Model->toArray()]);
- return '_fund-log-validation-fails-210';
- }
- }
- return Log::create_log($user_id, $fund_id, $amount, $remark, $later_balance, $before_balance, $id,
- $type->value(), $lastLog);
- }
- /**
- * 获取账户信息
- *
- * @param $user_id
- * @param $fund_id
- */
- public static function info($user_id, $fund_id)
- {
- $ModelData = self::get_account($user_id, $fund_id);
- return $ModelData->toArray();
- }
- /**
- * @param $userId
- * @param $fundIds
- * @return mixed[]
- * 获取用户全部资金账户
- */
- public static function getAllAccount($userId,$fundIds)
- {
- $modelData = FundModel::getAllAccount($userId,$fundIds);
- return $modelData->toArray();
- }
- }
|