ProductPopular.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class ProductPopular extends Model
  5. {
  6. // 表名
  7. protected $table = 'product_popular';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'create_time';
  12. protected $updateTime = 'update_time';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'start_time_text',
  17. 'end_time_text',
  18. 'create_time_text',
  19. 'update_time_text'
  20. ];
  21. //0未开始 1进行中 2已结束 Not started
  22. const NOTSTOP = 0;
  23. const NORMAL = 1;
  24. const STOP = 2;
  25. //状态
  26. public $status_list = [
  27. self::NOTSTOP => '未开始',
  28. self::STOP => '停用',
  29. self::NORMAL => '进行中'
  30. ];
  31. //刷新库存状态: status 状态0未开始 1进行中 2已结束
  32. public static function setUpdateStatus()
  33. {
  34. $count =0;
  35. $time = time();
  36. $list = self::where('status', '<', self::STOP)->select();
  37. foreach ($list as $item)
  38. {
  39. //未开始
  40. if ( $item->start_time > $time) {
  41. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::NOTSTOP]);
  42. }
  43. //进行中
  44. if ($time >= $item->start_time && $time < $item->end_time) {
  45. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::NORMAL]);
  46. }
  47. //已结束
  48. if($time >= $item->end_time){
  49. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::STOP]);
  50. }
  51. }
  52. return $count;
  53. }
  54. //分类
  55. public function productsList()
  56. {
  57. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  58. }
  59. protected static function init()
  60. {
  61. self::afterInsert(function ($row) {
  62. $pk = $row->getPk();
  63. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  64. });
  65. }
  66. public function getStartTimeTextAttr($value, $data)
  67. {
  68. $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
  69. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  70. }
  71. public function getEndTimeTextAttr($value, $data)
  72. {
  73. $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
  74. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  75. }
  76. public function getCreateTimeTextAttr($value, $data)
  77. {
  78. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  79. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  80. }
  81. public function getUpdateTimeTextAttr($value, $data)
  82. {
  83. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  84. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  85. }
  86. protected function setStartTimeAttr($value)
  87. {
  88. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  89. }
  90. protected function setEndTimeAttr($value)
  91. {
  92. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  93. }
  94. protected function setCreateTimeAttr($value)
  95. {
  96. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  97. }
  98. protected function setUpdateTimeAttr($value)
  99. {
  100. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  101. }
  102. }