ProductPopular.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if ($time >= $item->start_time && $time < $item->end_time) {
  40. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::NORMAL]);
  41. }
  42. //已结束
  43. if($time >= $item->end_time){
  44. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::STOP]);
  45. }
  46. }
  47. return $count;
  48. }
  49. public function employees()//yewu为第一张表的字段,employee_num为第二张表的字段
  50. {
  51. return $this->belongsTo('app\admin\model\setting\Employees', 'yewu', 'employee_num', [], 'LEFT')->setEagerlyType(0);
  52. }
  53. protected static function init()
  54. {
  55. self::afterInsert(function ($row) {
  56. $pk = $row->getPk();
  57. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  58. });
  59. }
  60. public function getStartTimeTextAttr($value, $data)
  61. {
  62. $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
  63. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  64. }
  65. public function getEndTimeTextAttr($value, $data)
  66. {
  67. $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
  68. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  69. }
  70. public function getCreateTimeTextAttr($value, $data)
  71. {
  72. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  73. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  74. }
  75. public function getUpdateTimeTextAttr($value, $data)
  76. {
  77. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  78. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  79. }
  80. protected function setStartTimeAttr($value)
  81. {
  82. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  83. }
  84. protected function setEndTimeAttr($value)
  85. {
  86. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  87. }
  88. protected function setCreateTimeAttr($value)
  89. {
  90. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  91. }
  92. protected function setUpdateTimeAttr($value)
  93. {
  94. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  95. }
  96. }