Jackpot.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\admin\model\egg;
  3. use think\Model;
  4. use think\Db;
  5. class Jackpot extends Model
  6. {
  7. // 表名
  8. protected $name = 'egg_jackpot';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = false;
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'status_text',
  18. 'type_text'
  19. ];
  20. protected static function init()
  21. {
  22. self::afterInsert(function ($row) {
  23. $pk = $row->getPk();
  24. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  25. });
  26. self::afterUpdate(function ($row) {
  27. $changed = $row->getChangedData();
  28. if (isset($changed['status'])) {
  29. if ($changed['status'] == -1) {
  30. Db::name('egg_gift')->where('Jackpot_id',$row['id'])->update(['is_use'=>1]);
  31. }else{
  32. Db::name('egg_gift')->where('Jackpot_id',$row['id'])->update(['is_use'=>0,'updatetime'=>$row['updatetime']]);
  33. }
  34. }
  35. });
  36. }
  37. public function getStatusList()
  38. {
  39. return ['-1' => __('Status -1'), '1' => __('Status 1'), '0' => __('Status 0')];
  40. }
  41. public function getTypeList()
  42. {
  43. return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
  44. }
  45. public function getStatusTextAttr($value, $data)
  46. {
  47. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  48. $list = $this->getStatusList();
  49. return isset($list[$value]) ? $list[$value] : '';
  50. }
  51. public function getTypeTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  54. $list = $this->getTypeList();
  55. return isset($list[$value]) ? $list[$value] : '';
  56. }
  57. }