ProductPopular.php 4.6 KB

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