ProductLists.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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')->field('id,product_id');
  41. }
  42. //已出售地区
  43. public function productareastop()
  44. {
  45. return $this->hasMany('ProductArea', 'product_id', 'id')->where('status', 0)->field('id,product_id');
  46. }
  47. //hold_num
  48. public function productorder()
  49. {
  50. return $this->hasMany('ProductOrder', 'product_id', 'id')->where('status', 'in', [ProductOrder::Paid, ProductOrder::Transferred, ProductOrder::Freeze])->field('id,product_id');
  51. }
  52. //分类
  53. public function products()
  54. {
  55. return $this->hasOne('ProductsModel', 'id', 'type_id', [], 'LEFT')->setEagerlyType(0);
  56. }
  57. //抢购 ProductPopular
  58. public function productpopular()
  59. {
  60. return $this->hasOne('ProductPopular', 'product_id', 'id', [], 'INNER')->order('start_time')->setEagerlyType(0);
  61. }
  62. //寄售
  63. public function producttransfer()
  64. {
  65. $map = [];
  66. $transfer = input('post.transfer_id/d', 0);
  67. if($transfer > 0) $map['id'] = $transfer;
  68. $order = 'id desc,price ASC';
  69. if(input('post.sort') == 1) $order = 'price desc';
  70. if(input('post.sort') == 2) $order = 'price asc';
  71. return $this->hasMany('ProductTransfer', 'product_id', 'id', [], 'LEFT')->where($map)->where('status', self::Normal)->order($order);
  72. }
  73. //收藏
  74. public function collect()
  75. {
  76. return $this->hasOne('UserCollect', 'product_id', 'id', [], 'LEFT')->where('user_id', Auth::instance()->getUser()['id']);
  77. }
  78. //获取产品分类
  79. public static function getProductTypeById(int $productId)
  80. {
  81. return self::where('id', $productId)->value('type_id');
  82. }
  83. //获取状态
  84. public function getStatusTextAttr($value, $data){
  85. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  86. }
  87. protected static function init()
  88. {
  89. self::afterInsert(function ($row) {
  90. $pk = $row->getPk();
  91. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  92. });
  93. }
  94. public function getCreateTimeTextAttr($value, $data)
  95. {
  96. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  97. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  98. }
  99. public function getUpdateTimeTextAttr($value, $data)
  100. {
  101. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  102. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  103. }
  104. protected function setCreateTimeAttr($value)
  105. {
  106. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  107. }
  108. protected function setUpdateTimeAttr($value)
  109. {
  110. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  111. }
  112. }