ProductLists.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use app\common\library\Auth;
  5. class ProductLists extends Model
  6. {
  7. // 表名
  8. protected $table = 'product_list';
  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. 'create_time_text',
  18. 'update_time_text'
  19. ];
  20. const Stop = 0;
  21. const Normal = 1;
  22. //状态
  23. public $status_list = [
  24. '-1' => '全部',
  25. self::Stop => '停用',
  26. self::Normal => '正常'
  27. ];
  28. //获取产品信息
  29. public static function getBySynthesisProduct(string $product_id, int $type_id, $lan = 'zh')
  30. {
  31. if(empty($product_id)) return [];
  32. $info = self::whereIn('id', $product_id);
  33. if($type_id == ProductPledges::Combin) $info = $info->orderRaw('field(id,'. $product_id.')');
  34. $info = $info->field('id,thum,'.$lan.'_name as name')->select();
  35. return $info ;
  36. }
  37. //地区
  38. public function productArea()
  39. {
  40. return $this->hasMany('ProductArea', 'product_id', 'id');
  41. }
  42. //分类
  43. public function products()
  44. {
  45. return $this->hasOne('ProductsModel', 'id', 'type_id', [], 'LEFT')->setEagerlyType(0);
  46. }
  47. //抢购 ProductPopular
  48. public function productpopular()
  49. {
  50. return $this->hasOne('ProductPopular', 'product_id', 'id', [], 'INNER')->order('start_time')->setEagerlyType(0);
  51. }
  52. //寄售
  53. public function producttransfer()
  54. {
  55. $map = [];
  56. $transfer = input('post.transfer_id/d', 0);
  57. if($transfer > 0) $map['id'] = $transfer;
  58. $order = 'id desc';
  59. if(input('post.sort') == 1) $order = 'price desc';
  60. if(input('post.sort') == 2) $order = 'price asc';
  61. return $this->hasMany('ProductTransfer', 'product_id', 'id', [], 'LEFT')->where($map)->where('status', self::Normal)->order($order);
  62. }
  63. //收藏
  64. public function collect()
  65. {
  66. return $this->hasOne('UserCollect', 'product_id', 'id', [], 'LEFT')->where('user_id', Auth::instance()->getUser()['id']);
  67. }
  68. //获取产品分类
  69. public static function getProductTypeById(int $productId)
  70. {
  71. return self::where('id', $productId)->value('type_id');
  72. }
  73. //获取状态
  74. public function getStatusTextAttr($value, $data){
  75. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  76. }
  77. protected static function init()
  78. {
  79. self::afterInsert(function ($row) {
  80. $pk = $row->getPk();
  81. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  82. });
  83. }
  84. public function getCreateTimeTextAttr($value, $data)
  85. {
  86. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  87. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  88. }
  89. public function getUpdateTimeTextAttr($value, $data)
  90. {
  91. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  92. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  93. }
  94. protected function setCreateTimeAttr($value)
  95. {
  96. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  97. }
  98. protected function setUpdateTimeAttr($value)
  99. {
  100. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  101. }
  102. }