def findRightDate(oneDay,option):
    '''
    根据oneDay和option找出最近的一期（几期）财报
    option = '最近三年','最近一年','最近一期'
    比如 
    oneDay = '2014-12-30'
    option = '最近三年'
    则返回  ['2013-12-31','2012-12-31','2011-12-30']
    '''
    reportDateList = ['03-31','06-30','09-30','12-31']
    #fisrtYear = oneDay[:4]
    #recentMonthDay = oneDay[5:]
    #recentReportDate = recentYear + '-' + [x for x in reportDateList if oneDay[5:] > x][0]

    if option == '最近三年':
        if '12-31' not in oneDay:  
            firstYear = str(eval('int(oneDay[:4]) - 1')) + '-' + '12-31'
            secondYear = str(eval('int(oneDay[:4]) - 2')) + '-' + '12-31'
            thirdYear = str(eval('int(oneDay[:4]) - 3')) + '-' + '12-31'
            fourthYear = str(eval('int(oneDay[:4]) - 4')) + '-' + '12-31' # 多加一年，保险起见
        else:
            firstYear = oneDay
            secondYear = str(eval('int(firstYear[:4]) - 1')) + '-' + '12-31'
            thirdYear = str(eval('int(firstYear[:4]) - 2')) + '-' + '12-31'
            fourthYear = str(eval('int(firstYear[:4]) - 3')) + '-' + '12-31'
        return [firstYear,secondYear,thirdYear,fourthYear]

    elif option == '最近一年':  
        if '12-31' not in oneDay:
            firstYear = str(eval('int(oneDay[:4]) - 1')) + '-' + '12-31'
            secondYear = str(eval('int(oneDay[:4]) - 2')) + '-' + '12-31'
        else:
            firstYear = oneDay
            secondYear = str(eval('int(firstYear[:4]) - 1')) + '-' + [x for x in reportDateList if firstYear[5:] > x][0]
        return [firstYear,secondYear]

    elif option == '最近一期':
        if oneDay[5:] not in reportDateList:
            if oneDay[5:] < reportDateList[0]: #'01-02' < '03-31'
                firstYear = str(eval('int(oneDay[:4]) - 1'))
                firstPeriod = firstYear + '-' + reportDateList[-1]
                secondPeriod = firstYear + '-' + reportDateList[-2]
            elif oneDay[5:] < reportDateList[1]: #'04-05' < '06-30'
                firstYear = oneDay[:4]
                firstPeriod = firstYear + '-' + reportDateList[0]
                secondPeriod = str(eval('int(firstYear) - 1'))+ '-' + reportDateList[3]
            else:
                firstYear = oneDay[:4]
                #print('test: ',[x for x in reportDateList if oneDay[5:] > x])
                firstPeriod = firstYear + '-' + [x for x in reportDateList if oneDay[5:] > x][-1]
                secondPeriod = firstYear + '-' + [x for x in reportDateList if oneDay[5:] > x][-2]
        else: #oneDay = '2015-03-31' or '2016-12-31'
            if oneDay[5:] == '03-31':
                firstPeriod = oneDay
                secondPeriod = str(eval('int(oneDay[:4]) - 1')) + '-' + '12-31'
            else:
                firstPeriod = oneDay
                secondPeriod = oneDay[:4] + '-' + [x for x in reportDateList if oneDay[5:] > x][-1]
        return [firstPeriod,secondPeriod]
    else:
        raise ValueError('输入要是最近一期，最近一年，最近三年，请检查Json的输入') 

oneDay = '2015-12-31'
option = '最近一年'
print(findRightDate(oneDay,option))
