Coupon.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace addons\unishop\model;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. /**
  6. * 收货地址模型
  7. * Class Favorite
  8. * @package addons\unishop\model
  9. */
  10. class Coupon extends Model
  11. {
  12. use SoftDelete;
  13. //数据库
  14. protected $connection = 'database';
  15. // 表名
  16. protected $name = 'unishop_coupon';
  17. // 自动写入时间戳字段
  18. protected $autoWriteTimestamp = 'int';
  19. // 定义时间戳字段名
  20. protected $createTime = 'createtime';
  21. protected $updateTime = 'updatetime';
  22. protected $deleteTime = 'deletetime';
  23. // 追加属性
  24. protected $append = [
  25. 'starttime_text',
  26. 'endtime_text'
  27. ];
  28. // 是否上架?
  29. const SWITCH_ON = 1; //是
  30. const SWITCH_OFF = 0; //否
  31. protected static function init()
  32. {
  33. self::afterInsert(function ($row) {
  34. $pk = $row->getPk();
  35. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  36. });
  37. }
  38. public function getStarttimeTextAttr($value, $data)
  39. {
  40. $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
  41. return is_numeric($value) ? date("Y-m-d", $value) : $value;
  42. }
  43. public function getEndtimeTextAttr($value, $data)
  44. {
  45. $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
  46. return is_numeric($value) ? date("Y-m-d", $value) : $value;
  47. }
  48. protected function setStarttimeAttr($value)
  49. {
  50. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  51. }
  52. protected function setEndtimeAttr($value)
  53. {
  54. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  55. }
  56. }