MoneyLog.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\common\model;
  3. use think\Exception;
  4. use think\Model;
  5. class MoneyLog extends Model
  6. {
  7. // 表名
  8. protected $name = 'money_log';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'create_time';
  13. protected $updateTime = false;
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'create_time_text'
  18. ];
  19. //订单状态: 0未支付 1完成 2冻结 3取消
  20. const Recharge = 1;
  21. const Withdraw = 2;
  22. const Pay = 3;
  23. const PayBack = 4;
  24. const OrderBonus = 5;
  25. const Commission = 6;
  26. const SystemChange = 7;
  27. protected function getStatusNamesArr(){
  28. $status_names = [
  29. '-1' => __('全部状态'),
  30. self::Recharge => __('充值'),
  31. self::Withdraw => __('提现'),
  32. self::Pay => __('交易'),
  33. self::PayBack => __('交易'),
  34. self::OrderBonus => __('佣金'),
  35. self::Commission => __('奖励'),
  36. self::SystemChange => __('系统'),
  37. ];
  38. return $status_names;
  39. }
  40. /*
  41. * 订单状态
  42. * 0未支付 1完成 2冻结 3取消
  43. */
  44. public function getStatusNames($type = -1){
  45. $status_names = $this->getStatusNamesArr();
  46. if($type == -1){
  47. return $status_names;
  48. }
  49. if(isset($status_names[$type])){
  50. return $status_names[$type];
  51. }
  52. return __('未定义');
  53. }
  54. /**
  55. * 变更会员余额
  56. * @param int $money 余额
  57. * @param int $user_id 会员ID
  58. * @param string $memo 备注
  59. */
  60. public function change($user_id, $amount, $action, $from_id, $memo)
  61. {
  62. $user = (new Users())->lock(true)->find($user_id);
  63. if(empty($user)){
  64. throw new Exception(__('会员信息不存在'));
  65. }
  66. if($amount == 0){
  67. throw new Exception(__('参数有误'));
  68. }
  69. if (!array_key_exists($action, $this->getStatusNamesArr())) {
  70. throw new Exception(__('账变类型不存在'));
  71. }
  72. $balance = function_exists('bcadd') ? bcadd($user->balance, $amount, 2) : $user->balance + $amount;
  73. //更新会员信息
  74. $user->save(['balance' => $balance]);
  75. //写入日志
  76. MoneyLog::create([
  77. 'user_id' => $user_id,
  78. 'amount' => $amount,
  79. 'balance' => $balance,
  80. 'action' => $action,
  81. 'from_id' => $from_id,
  82. 'note' => $memo
  83. ]);
  84. }
  85. //
  86. public function users()
  87. {
  88. return $this->hasOne('Users','id','user_id',[],'LEFT')->setEagerlyType(0);
  89. }
  90. public function fromuser()
  91. {
  92. return $this->hasOne('Users','id','from_id',[],'LEFT')->setEagerlyType(0);
  93. }
  94. public function getCreateTimeTextAttr($value, $data)
  95. {
  96. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  97. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  98. }
  99. protected function setCreateTimeAttr($value)
  100. {
  101. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  102. }
  103. }