ProductMarket.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Request;
  5. use app\common\library\Auth;
  6. class ProductMarket extends Model
  7. {
  8. // 表名
  9. protected $table = 'product_market';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'create_time';
  14. protected $updateTime = 'update_time';
  15. protected $deleteTime = false;
  16. // 追加属性
  17. protected $append = [
  18. 'create_time_text',
  19. 'update_time_text'
  20. ];
  21. //状态 normal, hidden
  22. const Hidden = 0;
  23. const Normal = 1;
  24. //收藏
  25. public function collect()
  26. {
  27. return $this->hasOne('UserCollect', 'market_id', 'id', [], 'LEFT')->where('user_id', Auth::instance()->getUser()['id']);
  28. }
  29. //产品
  30. public function products()
  31. {
  32. $header = Request::instance()->header('Accept-Language');
  33. $lan = !empty($header)? substr($header, 0, 2):'zh' ;
  34. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->field('id,'.$lan .'_name as name,thum,details');
  35. }
  36. //寄售
  37. public function producttransfer()
  38. {
  39. $map = [];
  40. $transfer = input('post.transfer_id/d', 0);
  41. if($transfer > 0) $map['id'] = $transfer;
  42. $order = 'price asc';
  43. if(input('post.sort') == 1) $order = 'price desc';
  44. return $this->hasMany('ProductTransfer', 'product_id', 'product_id', [], 'LEFT')->where($map)->where('status', self::Normal)->order($order);
  45. }
  46. public function getCreateTimeTextAttr($value, $data)
  47. {
  48. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  49. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  50. }
  51. public function getUpdateTimeTextAttr($value, $data)
  52. {
  53. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  54. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  55. }
  56. protected function setCreateTimeAttr($value)
  57. {
  58. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  59. }
  60. protected function setUpdateTimeAttr($value)
  61. {
  62. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  63. }
  64. }