FundLogModel.php 3.1 KB

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