Usercoupon.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class Usercoupon extends Model
  5. {
  6. // 表名
  7. protected $name = 'user_coupon';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'type_text',
  17. 'purpose_text',
  18. 'starttime_text',
  19. 'endtime_text',
  20. 'status_text'
  21. ];
  22. public function getTypeList()
  23. {
  24. return ['1' => __('Type 1'), '2' => __('Type 2')];
  25. }
  26. public function getPurposeList()
  27. {
  28. return ['0' => __('Purpose 0'), '1' => __('Purpose 1'), '2' => __('Purpose 2'), '3' => __('Purpose 3'), '4' => __('Purpose 4'), '5' => __('Purpose 5')];
  29. }
  30. public function getStatusList()
  31. {
  32. return ['0' => __('Status 0'), '1' => __('Status 1')];
  33. }
  34. public function getTypeTextAttr($value, $data)
  35. {
  36. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  37. $list = $this->getTypeList();
  38. return isset($list[$value]) ? $list[$value] : '';
  39. }
  40. public function getPurposeTextAttr($value, $data)
  41. {
  42. $value = $value ? $value : (isset($data['purpose']) ? $data['purpose'] : '');
  43. $list = $this->getPurposeList();
  44. return isset($list[$value]) ? $list[$value] : '';
  45. }
  46. public function getStarttimeTextAttr($value, $data)
  47. {
  48. $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
  49. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  50. }
  51. public function getEndtimeTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
  54. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  55. }
  56. public function getStatusTextAttr($value, $data)
  57. {
  58. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  59. $list = $this->getStatusList();
  60. return isset($list[$value]) ? $list[$value] : '';
  61. }
  62. protected function setStarttimeAttr($value)
  63. {
  64. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  65. }
  66. protected function setEndtimeAttr($value)
  67. {
  68. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  69. }
  70. public function coupon()
  71. {
  72. return $this->belongsTo('Coupon', 'coupon_id', 'id', [], 'LEFT')->setEagerlyType(0);
  73. }
  74. }