from math import radians, cos, sin, asin, sqrt
from common.common_func import commonFuc
import json


class businessFuc(object):

    def diff_years(self, source_year, target_year):
        """
        计算两个日期之间相差的年数
        :param target_year: 当前时间
        :param source_year: 原始年日期
        :return:
        """
        years = int(target_year.strftime('%Y')) - int(str(source_year).split("-")[0])
        return years

    def geopy_distance(self, lng1, lat1, lng2, lat2):
        """
        计算两个经纬度之间的距离
        :return:
        """
        lng1, lat1, lng2, lat2 = map(radians, [float(lng1), float(lat1), float(lng2), float(lat2)])
        dlon = lng2 - lng1
        dlat = lat2 - lat1
        a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
        distance = 2 * asin(sqrt(a)) * 6371 * 1000  # 地球平均半径，6371km
        distance = round(distance / 1000, 3)
        return distance

    def get_globalsid(self):
        login_url = commonFuc().get_business_data("", "login_url")
        result = commonFuc().http_get(login_url)
        return result["sid"]










