<?php

namespace app\common\model;

use think\Model;


class SosBill extends Model
{





    // 表名
    protected $name = 'sos_bill';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'processtime_text',
        'confirmtime_text',
        'endtime_text',
        'status_text'
    ];



    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '-1' => __('Status -1'), '-2' => __('Status -2')];
    }


    public function getProcesstimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['processtime']) ? $data['processtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getConfirmtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['confirmtime']) ? $data['confirmtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setProcesstimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setConfirmtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    public static function income($cid,$user_id,$sos_id,$rescuers_id,$rescuers_user_id,$latitude,$longitude,$user_phone,$rescue_phone,$address,$status=0)
    {
        return self::create(compact('cid','user_id','user_phone','sos_id','rescuers_id','rescuers_user_id','rescue_phone','longitude','latitude','status','address'));
    }
    /**
     * 获取排序sql
     * @param $latitude
     * @param $longitude
     * @return mixed
     */
    public static function distanceSql($latitude, $longitude)
    {
        $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";
        return $field;
    }
    public static function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters
        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;
        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;
        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;
        return round($calculatedDistance);
    }
    public static function lst($where)
    {
        $model = new self;
        if(isset($where['cid']) && $where['cid']>0)$model = $model->where('cid',$where['cid']);
        if(isset($where['sos_id']) && $where['sos_id']>0)$model = $model->where('sos_id',$where['sos_id']);
        if(isset($where['user_id']) && $where['user_id']>0)$model = $model->where('user_id',$where['user_id']);
        if(isset($where['rescuers_id']) && $where['rescuers_id']>0)$model = $model->where('rescuers_id',$where['rescuers_id']);
        if(isset($where['rescuers_user_id']) && $where['rescuers_user_id']>0)$model = $model->where('rescuers_user_id',$where['rescuers_user_id']);
        if(isset($where['status']) && $where['status']>-3)$model = $model->where('status',$where['status']);
        if (isset($where['latitude']) && $where['latitude']!='' &&  isset($where['longitude']) && $where['longitude']!='' ) {
            $model = $model->field(['*', self::distanceSql($where['latitude'], $where['longitude'])])->order('distance asc');
        }
        return $model->page($where['page'],$where['limit'])->select();
    }

}
