ProductLists.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class ProductLists extends Model
  5. {
  6. // 表名
  7. protected $table = 'product_list';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'create_time';
  12. protected $updateTime = 'update_time';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'create_time_text',
  17. 'update_time_text'
  18. ];
  19. const Stop = 0;
  20. const Normal = 1;
  21. //状态
  22. public $status_list = [
  23. '-1' => '全部',
  24. self::Stop => '停用',
  25. self::Normal => '正常'
  26. ];
  27. //获取产品信息
  28. public static function getBySynthesisProduct(string $product_id, int $type_id, $lan = 'zh')
  29. {
  30. if(empty($product_id)) return [];
  31. $info = self::whereIn('id', $product_id);
  32. if($type_id == ProductPledges::Combin) $info = $info->orderRaw('field(id,'. $product_id.')');
  33. $info = $info->field('id,thum,'.$lan.'_name as name')->select();
  34. return $info ;
  35. }
  36. //地区
  37. public function productArea()
  38. {
  39. return $this->hasMany('ProductArea', 'product_id', 'id');
  40. }
  41. //分类
  42. public function products()
  43. {
  44. return $this->hasOne('ProductsModel', 'id', 'type_id', [], 'LEFT')->setEagerlyType(0);
  45. }
  46. //抢购 ProductPopular
  47. public function productpopular()
  48. {
  49. return $this->hasOne('ProductPopular', 'product_id', 'id', [], 'INNER')->order('start_time')->setEagerlyType(0);
  50. }
  51. //获取产品分类
  52. public static function getProductTypeById(int $productId)
  53. {
  54. return self::where('id', $productId)->value('type_id');
  55. }
  56. //获取状态
  57. public function getStatusTextAttr($value, $data){
  58. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  59. }
  60. protected static function init()
  61. {
  62. self::afterInsert(function ($row) {
  63. $pk = $row->getPk();
  64. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  65. });
  66. }
  67. public function getCreateTimeTextAttr($value, $data)
  68. {
  69. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  70. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  71. }
  72. public function getUpdateTimeTextAttr($value, $data)
  73. {
  74. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  75. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  76. }
  77. protected function setCreateTimeAttr($value)
  78. {
  79. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  80. }
  81. protected function setUpdateTimeAttr($value)
  82. {
  83. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  84. }
  85. }