SosBill.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class SosBill extends Model
  5. {
  6. // 表名
  7. protected $name = 'sos_bill';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = false;
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'processtime_text',
  17. 'confirmtime_text',
  18. 'endtime_text',
  19. 'status_text'
  20. ];
  21. public function getStatusList()
  22. {
  23. return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '-1' => __('Status -1'), '-2' => __('Status -2')];
  24. }
  25. public function getProcesstimeTextAttr($value, $data)
  26. {
  27. $value = $value ? $value : (isset($data['processtime']) ? $data['processtime'] : '');
  28. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  29. }
  30. public function getConfirmtimeTextAttr($value, $data)
  31. {
  32. $value = $value ? $value : (isset($data['confirmtime']) ? $data['confirmtime'] : '');
  33. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  34. }
  35. public function getEndtimeTextAttr($value, $data)
  36. {
  37. $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
  38. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  39. }
  40. public function getStatusTextAttr($value, $data)
  41. {
  42. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  43. $list = $this->getStatusList();
  44. return isset($list[$value]) ? $list[$value] : '';
  45. }
  46. protected function setProcesstimeAttr($value)
  47. {
  48. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  49. }
  50. protected function setConfirmtimeAttr($value)
  51. {
  52. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  53. }
  54. protected function setEndtimeAttr($value)
  55. {
  56. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  57. }
  58. public static function income($cid,$user_id,$sos_id,$rescuers_id,$rescuers_user_id,$latitude,$longitude,$user_phone,$rescue_phone,$address,$status=0)
  59. {
  60. return self::create(compact('cid','user_id','user_phone','sos_id','rescuers_id','rescuers_user_id','rescue_phone','longitude','latitude','status','address'));
  61. }
  62. /**
  63. * 获取排序sql
  64. * @param $latitude
  65. * @param $longitude
  66. * @return mixed
  67. */
  68. public static function distanceSql($latitude, $longitude)
  69. {
  70. $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  71. return $field;
  72. }
  73. public static function getDistance($lat1, $lng1, $lat2, $lng2)
  74. {
  75. $earthRadius = 6367000; //approximate radius of earth in meters
  76. $lat1 = ($lat1 * pi() ) / 180;
  77. $lng1 = ($lng1 * pi() ) / 180;
  78. $lat2 = ($lat2 * pi() ) / 180;
  79. $lng2 = ($lng2 * pi() ) / 180;
  80. $calcLongitude = $lng2 - $lng1;
  81. $calcLatitude = $lat2 - $lat1;
  82. $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
  83. $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  84. $calculatedDistance = $earthRadius * $stepTwo;
  85. return round($calculatedDistance);
  86. }
  87. public static function lst($where)
  88. {
  89. $model = new self;
  90. if(isset($where['cid']) && $where['cid']>0)$model = $model->where('cid',$where['cid']);
  91. if(isset($where['sos_id']) && $where['sos_id']>0)$model = $model->where('sos_id',$where['sos_id']);
  92. if(isset($where['user_id']) && $where['user_id']>0)$model = $model->where('user_id',$where['user_id']);
  93. if(isset($where['rescuers_id']) && $where['rescuers_id']>0)$model = $model->where('rescuers_id',$where['rescuers_id']);
  94. if(isset($where['rescuers_user_id']) && $where['rescuers_user_id']>0)$model = $model->where('rescuers_user_id',$where['rescuers_user_id']);
  95. if(isset($where['status']) && $where['status']>-3)$model = $model->where('status',$where['status']);
  96. if (isset($where['latitude']) && $where['latitude']!='' && isset($where['longitude']) && $where['longitude']!='' ) {
  97. $model = $model->field(['*', self::distanceSql($where['latitude'], $where['longitude'])])->order('distance asc');
  98. }
  99. return $model->page($where['page'],$where['limit'])->select();
  100. }
  101. }