Record.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\admin\model\vip;
  3. use addons\vip\library\Service;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class Record extends Model
  8. {
  9. // 表名
  10. protected $name = 'vip_record';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = false;
  16. protected $deleteTime = false;
  17. // 追加属性
  18. protected $append = [
  19. 'expiretime_text',
  20. 'status_text'
  21. ];
  22. public function getOriginData()
  23. {
  24. return $this->origin;
  25. }
  26. public static function init()
  27. {
  28. self::beforeInsert(function ($row) {
  29. $userVipInfo = Service::getVipInfo($row['user_id']);
  30. $vipInfo = Vip::get($row['vip_id']);
  31. if (in_array($row['status'], ['active'])) {
  32. if (isset($userVipInfo['level']) && $userVipInfo['level'] >= $vipInfo['level']) {
  33. throw new Exception("当前用户VIP等级过高,无需添加");
  34. }
  35. }
  36. if (!$row['days']) {
  37. throw new Exception("VIP天数不能为空");
  38. }
  39. $row['level'] = $vipInfo['level'];
  40. });
  41. self::afterInsert(function ($row) {
  42. $orderid = date("Ymdhis") . sprintf("%08d", $row['user_id']) . mt_rand(1000, 9999);
  43. $data = [
  44. 'orderid' => $orderid,
  45. 'user_id' => $row['user_id'],
  46. 'vip_id' => $row['vip_id'],
  47. 'record_id' => $row['id'],
  48. 'title' => '购买VIP',
  49. 'amount' => $row['amount'],
  50. 'method' => 'web',
  51. 'payamount' => 0,
  52. 'paytype' => 'system',
  53. 'ip' => request()->ip(),
  54. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  55. 'status' => 'created'
  56. ];
  57. $order = \addons\vip\model\Order::create($data);
  58. if ($row['status'] == 'active') {
  59. Db::name("vip_record")->where('id', $row['id'])->update(['status' => 'created']);
  60. \addons\vip\library\Order::settle($orderid, $row['amount']);
  61. }
  62. });
  63. self::beforeDelete(function ($row) {
  64. if ($row['status'] == 'active') {
  65. throw new Exception("请先设定为过期再进行删除");
  66. }
  67. });
  68. self::beforeUpdate(function ($row) {
  69. $changedData = $row->getChangedData();
  70. $originData = $row->getOriginData();
  71. if (isset($changedData['status'])) {
  72. if ($changedData['status'] == 'active') {
  73. $userVipInfo = Service::getVipInfo($row['user_id']);
  74. if ($userVipInfo && $userVipInfo['level'] > $row['level']) {
  75. throw new Exception("用户当前VIP等级过高,无法修改");
  76. }
  77. }
  78. }
  79. });
  80. self::afterUpdate(function ($row) {
  81. $changedData = $row->getChangedData();
  82. $originData = $row->getOriginData();
  83. if (isset($changedData['status'])) {
  84. $order = Order::where('user_id', $row['user_id'])->where('record_id', $row['id'])->find();
  85. if ($order) {
  86. if ($order->amount != $row['amount']) {
  87. $order->save(['amount' => $row['amount']]);
  88. }
  89. if ($changedData['status'] == 'active') {
  90. Db::name("vip_record")->where('id', $row['id'])->update(['status' => $originData['status']]);
  91. \addons\vip\library\Order::settle($order->orderid, $row['amount']);
  92. } elseif ($originData['status'] == 'active') {
  93. Db::name("vip_record")->where('id', $row['id'])->update(['status' => $originData['status']]);
  94. \addons\vip\library\Order::unsettle($order->orderid);
  95. }
  96. } else {
  97. throw new Exception("未找到关联订单");
  98. }
  99. }
  100. });
  101. }
  102. public function getStatusList()
  103. {
  104. return ['created' => __('Status created'), 'active' => __('Status active'), 'expired' => __('Status expired'), 'finished' => __('Status finished'), 'locked' => __('Status locked'), 'canceled' => __('Status canceled')];
  105. }
  106. public function getExpiretimeTextAttr($value, $data)
  107. {
  108. $value = $value ? $value : (isset($data['expiretime']) ? $data['expiretime'] : '');
  109. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  110. }
  111. public function getStatusTextAttr($value, $data)
  112. {
  113. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  114. $list = $this->getStatusList();
  115. return isset($list[$value]) ? $list[$value] : '';
  116. }
  117. protected function setExpiretimeAttr($value)
  118. {
  119. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  120. }
  121. public function user()
  122. {
  123. return $this->belongsTo('\app\common\model\User', "user_id", "id", [], 'LEFT')->setEagerlyType(0);
  124. }
  125. public function vip()
  126. {
  127. return $this->belongsTo('Vip', "vip_id", "id", [], 'LEFT')->setEagerlyType(0);
  128. }
  129. }