ProductPopular.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\common\model;
  3. use fast\Arr;
  4. use Google\Service\Dfareporting\ObaIcon;
  5. use think\Model;
  6. class ProductPopular extends Model
  7. {
  8. // 表名
  9. protected $table = 'product_popular';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'create_time';
  14. protected $updateTime = 'update_time';
  15. protected $deleteTime = false;
  16. // 追加属性
  17. protected $append = [
  18. 'start_time_text',
  19. 'end_time_text',
  20. 'create_time_text',
  21. 'update_time_text'
  22. ];
  23. //0未开始 1进行中 2已结束 Not started
  24. const NOTSTOP = 0;
  25. const NORMAL = 1;
  26. const STOP = 2;
  27. //状态
  28. public $status_list = [
  29. self::NOTSTOP => '未开始',
  30. self::STOP => '停用',
  31. self::NORMAL => '进行中'
  32. ];
  33. //刷新库存状态: status 状态0未开始 1进行中 2已结束
  34. public static function setUpdateStatus()
  35. {
  36. $count =0;
  37. $time = time();
  38. $list = self::where('status', '<', self::STOP)->select();
  39. foreach ($list as $item)
  40. {
  41. //未开始
  42. if ( $item->start_time > $time) {
  43. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::NOTSTOP]);
  44. }
  45. //进行中
  46. if ($time >= $item->start_time && $time < $item->end_time) {
  47. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::NORMAL]);
  48. }
  49. //已结束
  50. if($time >= $item->end_time){
  51. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::STOP]);
  52. }
  53. }
  54. return $count;
  55. }
  56. //获取热销详情
  57. public static function getProductInfo(int $product_id, int $ids, string $lan): object
  58. {
  59. $info = self::alias('a')
  60. ->join("product_list b", "a.product_id = b.id", "left")
  61. ->field('a.id,a.product_id,'.'b.'.$lan.'_name as name,b.images as img_url,price,cost_price,stock,(num+init_num) as num,start_time,end_time,b.is_area,b.details')
  62. ->where('a.product_id', $product_id)
  63. ->where('a.status', self::NORMAL)
  64. ->find();
  65. if (empty($info)) {
  66. $info = self::alias('a')
  67. ->join("product_list b", "a.product_id = b.id", "left")
  68. ->field('a.id,a.product_id,'.'b.'.$lan.'_name as name,b.images as img_url,price,cost_price,stock,(num+init_num) as num,start_time,end_time,b.is_area,b.details')
  69. ->where('a.id', $ids)
  70. ->order('a.weigh desc')
  71. ->find();
  72. }
  73. return $info;
  74. }
  75. //获取当前抢购产品
  76. public static function getPopularByTime(int $productId, string $lan, $tim)
  77. {
  78. return self::alias('a')->where('a.start_time', '<=', $tim)->where('a.end_time', '>=', $tim)
  79. ->join('product_list b', "a.product_id = b.id", "left")
  80. ->field('a.*,b.is_area,'.$lan.'_name as name')
  81. ->where('a.product_id', $productId)->find();
  82. }
  83. //分类
  84. public function productsList()
  85. {
  86. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  87. }
  88. protected static function init()
  89. {
  90. self::afterInsert(function ($row) {
  91. $pk = $row->getPk();
  92. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  93. });
  94. }
  95. public function getStartTimeTextAttr($value, $data)
  96. {
  97. $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
  98. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  99. }
  100. public function getEndTimeTextAttr($value, $data)
  101. {
  102. $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
  103. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  104. }
  105. public function getCreateTimeTextAttr($value, $data)
  106. {
  107. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  108. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  109. }
  110. public function getUpdateTimeTextAttr($value, $data)
  111. {
  112. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  113. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  114. }
  115. protected function setStartTimeAttr($value)
  116. {
  117. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  118. }
  119. protected function setEndTimeAttr($value)
  120. {
  121. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  122. }
  123. protected function setCreateTimeAttr($value)
  124. {
  125. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  126. }
  127. protected function setUpdateTimeAttr($value)
  128. {
  129. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  130. }
  131. }