ProductPopular.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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( $productId, string $lan, $tim)
  77. {
  78. return self::alias('a')
  79. ->where('a.start_time', '<=', $tim)
  80. ->where('a.end_time', '>=', $tim)
  81. ->join('product_list b', "a.product_id = b.id", "left")
  82. ->field('a.*,b.is_area,'.$lan.'_name as name')
  83. ->where('a.product_id', $productId)->find();
  84. }
  85. //分类
  86. public function productsList()
  87. {
  88. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  89. }
  90. protected static function init()
  91. {
  92. self::afterInsert(function ($row) {
  93. $pk = $row->getPk();
  94. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  95. });
  96. }
  97. public function getStartTimeTextAttr($value, $data)
  98. {
  99. $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
  100. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  101. }
  102. public function getEndTimeTextAttr($value, $data)
  103. {
  104. $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
  105. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  106. }
  107. public function getCreateTimeTextAttr($value, $data)
  108. {
  109. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  110. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  111. }
  112. public function getUpdateTimeTextAttr($value, $data)
  113. {
  114. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  115. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  116. }
  117. protected function setStartTimeAttr($value)
  118. {
  119. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  120. }
  121. protected function setEndTimeAttr($value)
  122. {
  123. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  124. }
  125. protected function setCreateTimeAttr($value)
  126. {
  127. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  128. }
  129. protected function setUpdateTimeAttr($value)
  130. {
  131. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  132. }
  133. }