FundLogModel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Module\Fund\Models;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. use App\Module\Fund\Enums\LOG_TYPE;
  5. use App\Module\Fund\Enums\OPERATE_TYPE;
  6. use App\Module\User\Models\User;
  7. use App\Module\User\Models\UserInfo;
  8. use UCore\ModelCore;
  9. /**
  10. * App\Module\Fund\Models\FundLogModel
  11. * 资金日志表
  12. * field start
  13. * @property int $id
  14. * @property int $user_id 用户ID
  15. * @property \App\Module\Fund\Enums\FUND_TYPE $fund_id 资金id
  16. * @property int $amount 操作金额,正值为收入,负值为支出
  17. * @property string $operate_id 上游操作id
  18. * @property \App\Module\Fund\Enums\LOG_TYPE $operate_type 上游操作类型
  19. * @property string $remark 备注
  20. * @property int $create_time 最后更新时间
  21. * @property string $create_ip 最后更新ip
  22. * @property int $later_balance 在此之后的余额
  23. * @property int $before_balance 在此之前的月
  24. * @property int $date_key 月份key
  25. * @property string $hash 防篡改哈希值
  26. * @property string $prev_hash 上一条记录的哈希值
  27. * field end
  28. */
  29. class FundLogModel extends ModelCore
  30. {
  31. protected $table = 'fund_logs';
  32. // attrlist start
  33. protected $fillable = [
  34. 'id',
  35. 'user_id',
  36. 'fund_id',
  37. 'amount',
  38. 'operate_id',
  39. 'operate_type',
  40. 'remark',
  41. 'create_time',
  42. 'create_ip',
  43. 'later_balance',
  44. 'before_balance',
  45. 'date_key',
  46. 'hash',
  47. 'prev_hash',
  48. ];
  49. // attrlist end
  50. public $timestamps = false;
  51. protected $casts = [
  52. 'fund_id' => FUND_TYPE::class,
  53. 'operate_type' => LOG_TYPE::class
  54. ];
  55. public function user()
  56. {
  57. return $this->hasOne(\App\Module\User\Models\User::class, 'id', 'user_id');
  58. }
  59. /**
  60. * 生成记录的哈希值
  61. * SHA-256 输出为 64 位十六进制字符串
  62. */
  63. public function generateHash(): string
  64. {
  65. $data = [
  66. 'id' => $this->id,
  67. 'user_id' => $this->user_id,
  68. 'fund_id' => $this->fund_id,
  69. 'amount' => $this->amount,
  70. 'operate_id' => $this->operate_id,
  71. 'operate_type' => $this->operate_type,
  72. 'before_balance' => $this->before_balance,
  73. 'later_balance' => $this->later_balance,
  74. 'create_time' => $this->create_time,
  75. 'create_ip' => $this->create_ip,
  76. 'prev_hash' => $this->prev_hash,
  77. ];
  78. // 按键名排序,确保生成的哈希值一致
  79. ksort($data);
  80. // 生成 64 位十六进制哈希值
  81. return hash('sha256', json_encode($data, JSON_UNESCAPED_UNICODE));
  82. }
  83. /**
  84. * @param $user_id
  85. * @param $fund_id
  86. * @return static
  87. */
  88. public static function findLastUserFund($user_id, $fund_id)
  89. {
  90. return self::query()->where([
  91. 'user_id' => $user_id,
  92. 'fund_id' => $fund_id
  93. ])->orderBy('id', 'desc')->first();
  94. }
  95. public static function addLog($userId, $fundId, $amount)
  96. {
  97. }
  98. }