ProductPopular.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. //类型
  34. const TypeSingle = 1; //单品
  35. const TypeGroup = 2; //组合
  36. //刷新库存状态: status 状态0未开始 1进行中 2已结束
  37. public static function setUpdateStatus()
  38. {
  39. $count =0;
  40. $time = time();
  41. $list = self::where('status', '<', self::Stop)->select();
  42. foreach ($list as $item)
  43. {
  44. //未开始
  45. if ( $item->start_time > $time) {
  46. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::Notstop]);
  47. }
  48. //进行中
  49. if ($time >= $item->start_time && $time < $item->end_time) {
  50. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::Normal]);
  51. }
  52. //已结束
  53. if($time >= $item->end_time){
  54. $count += $item->allowField(true)->isUpdate(true)->save(['status'=>self::Stop]);
  55. }
  56. }
  57. return $count;
  58. }
  59. //获取热销详情
  60. public static function getProductInfo(int $product_id, int $ids, string $lan): object
  61. {
  62. $info = self::alias('a')
  63. ->join("product_list b", "a.product_id = b.id", "left")
  64. ->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.video,b.details')
  65. ->where('a.product_id', $product_id)
  66. ->where('a.status', self::Normal)
  67. ->find();
  68. if (empty($info)) {
  69. $info = self::alias('a')
  70. ->join("product_list b", "a.product_id = b.id", "left")
  71. ->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.video,b.details')
  72. ->where('a.id', $ids)
  73. ->order('a.weigh desc')
  74. ->find();
  75. }
  76. return $info;
  77. }
  78. //获取当前抢购产品
  79. public static function getPopularByTime( $productId, string $lan, $tim)
  80. {
  81. return self::alias('a')
  82. ->where('a.start_time', '<=', $tim)
  83. ->where('a.end_time', '>=', $tim)
  84. ->join('product_list b', "a.product_id = b.id", "left")
  85. ->field('a.*,b.is_area,'.$lan.'_name as name')
  86. ->where('a.product_id', $productId)->find();
  87. }
  88. //分类
  89. public function productsList()
  90. {
  91. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  92. }
  93. protected static function init()
  94. {
  95. self::afterInsert(function ($row) {
  96. $pk = $row->getPk();
  97. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  98. });
  99. }
  100. public function getStartTimeTextAttr($value, $data)
  101. {
  102. $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
  103. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  104. }
  105. public function getEndTimeTextAttr($value, $data)
  106. {
  107. $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
  108. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  109. }
  110. public function getCreateTimeTextAttr($value, $data)
  111. {
  112. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  113. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  114. }
  115. public function getUpdateTimeTextAttr($value, $data)
  116. {
  117. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  118. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  119. }
  120. protected function setStartTimeAttr($value)
  121. {
  122. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  123. }
  124. protected function setEndTimeAttr($value)
  125. {
  126. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  127. }
  128. protected function setCreateTimeAttr($value)
  129. {
  130. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  131. }
  132. protected function setUpdateTimeAttr($value)
  133. {
  134. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  135. }
  136. }