#-*- encoding:utf-8 -*-
#!/usr/bin/python

from dao.Dataoper_dao import DataoperDao
from db.mod_db import DataBase
import time

class Datao:
    def Getdata(self,cate_name,establishment_time):
        try:
            Dataoper = DataoperDao()
            # 将时间戳转化为年
            struct_time = time.localtime(establishment_time)  # 得到结构化时间格式
            establishment_time = time.strftime("%Y-%m-%d %H:%M:%S", struct_time).split('-')[0]
            establishment_time = int(establishment_time)
            print(establishment_time, "======establishment_time")
            re1 = Dataoper.select_sql_1(cate_name)
            if re1:
                list = []
                for ree in re1:
                    if ree:
                        policy_name = ree["policy_name"]
                        life_cycle = ree["life_cycle"]
                        life_cycle = int(life_cycle)
                        life_year = establishment_time + life_cycle
                        dict = {}
                        dict[policy_name] = life_year
                        list.append(dict)

                return 1, list
            else:
                DataInfo = DataBase()
                DataInfo.close()
                return 0, {}
        except:
            DataInfo = DataBase()
            DataInfo.close()
            return -1, {}


    def InsertData(self,datainfo):
        # 数据库连接
        try:
            Dataoper = DataoperDao()
            for data in datainfo:
                cate_name = data["cate_name"]
                policy_name = data["policy_name"]
                life_cycle = data["life_cycle"]
                re1 = Dataoper.select_sql_2(cate_name, policy_name)
                if re1:
                    id = re1["id"]
                    Dataoper.updateData(life_cycle, id)
                else:
                    Dataoper.insertData(cate_name, policy_name, life_cycle)

            DataInfo = DataBase()
            DataInfo.close()
            return 1

        except FileExistsError as e:
            DataInfo = DataBase()
            DataInfo.roll_back()
            DataInfo.close()
            return 0


# 下面一句的作用1是：运行本程序文件时执行什么操作
if __name__ == "__main__":
    #查询数据
    cate_name = "文化、体育和娱乐业"
    DataoInfo = Datao()
    re1,re2 = DataoInfo.Getdata(cate_name)
    print(str(re1))
    print(str(re2))