FundLogModel.php 3.8 KB

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