FundLogModel.php 2.9 KB

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