| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace App\Module\Fund\Models;
- use App\Module\Fund\Enums\FUND_TYPE;
- use App\Module\Fund\Enums\LOG_TYPE;
- use App\Module\Fund\Enums\OPERATE_TYPE;
- use App\Module\User\Models\User;
- use App\Module\User\Models\UserInfo;
- use UCore\ModelCore;
- /**
- * App\Module\Fund\Models\FundLogModel
- * 资金日志表
- * field start
- * @property int $id
- * @property int $user_id 用户ID
- * @property \App\Module\Fund\Enums\FUND_TYPE $fund_id 资金id
- * @property int $amount 操作金额,正值为收入,负值为支出
- * @property string $operate_id 上游操作id
- * @property \App\Module\Fund\Enums\LOG_TYPE $operate_type 上游操作类型
- * @property string $remark 备注
- * @property int $create_time 最后更新时间
- * @property string $create_ip 最后更新ip
- * @property int $later_balance 在此之后的余额
- * @property int $before_balance 在此之前的月
- * @property int $date_key 月份key
- * @property string $hash 防篡改哈希值
- * @property string $prev_hash 上一条记录的哈希值
- * field end
- */
- class FundLogModel extends ModelCore
- {
- protected $table = 'fund_logs';
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'fund_id',
- 'amount',
- 'operate_id',
- 'operate_type',
- 'remark',
- 'create_time',
- 'create_ip',
- 'later_balance',
- 'before_balance',
- 'date_key',
- 'hash',
- 'prev_hash',
- ];
- // attrlist end
- public $timestamps = false;
- protected $casts = [
- 'fund_id' => FUND_TYPE::class,
- 'operate_type' => LOG_TYPE::class
- ];
- public function user()
- {
- return $this->hasOne(\App\Module\User\Models\User::class, 'id', 'user_id');
- }
- /**
- * 生成记录的哈希值
- * SHA-256 输出为 64 位十六进制字符串
- */
- public function generateHash(): string
- {
- $data = [
- 'id' => $this->id,
- 'user_id' => $this->user_id,
- 'fund_id' => $this->fund_id,
- 'amount' => $this->amount,
- 'operate_id' => $this->operate_id,
- 'operate_type' => $this->operate_type,
- 'before_balance' => $this->before_balance,
- 'later_balance' => $this->later_balance,
- 'create_time' => $this->create_time,
- 'create_ip' => $this->create_ip,
- 'prev_hash' => $this->prev_hash,
- ];
- // 按键名排序,确保生成的哈希值一致
- ksort($data);
- // 生成 64 位十六进制哈希值
- return hash('sha256', json_encode($data, JSON_UNESCAPED_UNICODE));
- }
- /**
- * @param $user_id
- * @param $fund_id
- * @return static
- */
- public static function findLastUserFund($user_id, $fund_id)
- {
- return self::query()->where([
- 'user_id' => $user_id,
- 'fund_id' => $fund_id
- ])->orderBy('id', 'desc')->first();
- }
- public static function addLog($userId, $fundId, $amount)
- {
- }
- }
|