ProductLists.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, $lan = 'zh')
  29. {
  30. if(empty($product_id)) return [];
  31. $product_id = explode(',', $product_id);
  32. return self::whereIn('id', $product_id)->column('id,thum,'.$lan.'_name as name');
  33. }
  34. //地区
  35. public function productArea()
  36. {
  37. return $this->hasMany('ProductArea', 'product_id', 'id');
  38. }
  39. //分类
  40. public function products()
  41. {
  42. return $this->hasOne('ProductsModel', 'id', 'type_id', [], 'LEFT')->setEagerlyType(0);
  43. }
  44. //抢购 ProductPopular
  45. public function productpopular()
  46. {
  47. return $this->hasOne('ProductPopular', 'product_id', 'id', [], 'INNER')->order('start_time')->setEagerlyType(0);
  48. }
  49. //获取状态
  50. public function getStatusTextAttr($value, $data){
  51. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  52. }
  53. protected static function init()
  54. {
  55. self::afterInsert(function ($row) {
  56. $pk = $row->getPk();
  57. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  58. });
  59. }
  60. public function getCreateTimeTextAttr($value, $data)
  61. {
  62. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  63. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  64. }
  65. public function getUpdateTimeTextAttr($value, $data)
  66. {
  67. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  68. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  69. }
  70. protected function setCreateTimeAttr($value)
  71. {
  72. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  73. }
  74. protected function setUpdateTimeAttr($value)
  75. {
  76. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  77. }
  78. }