#!/usr/bin/env python
#-*- coding: UTF-8 -*-
#Ticloud web version 2.0
#author:WangRui

from common.dbsqlite import DBsqLite


class SystemLogObject(object):

    def querylog(self, start_time, end_time):
        db_instance = DBsqLite()
        sql = "SELECT user_name, CASE act_type WHEN 1 THEN '登录' "
        sql += "WHEN 2 THEN '退出'"
        sql += " WHEN 3 THEN '添加'"
        sql += " WHEN 4 THEN '修改'"
        sql += " WHEN 5 THEN '删除'"
        sql += " WHEN 6 THEN '设置'"
        sql += " WHEN 7 THEN '回收站'"
        sql += " ELSE '其他' END act_name, act_time,"
        sql += "act_content FROM t_log WHERE act_time >= '%s 00:00:00' and act_time <= '%s 23:59:59' ORDER BY act_time desc" \
              % (start_time, end_time)
        print sql
        data = db_instance.queryall(sql)
        db_instance.close()

        log_list = []
        if data:
            for d in data:
                temp_dict = dict()
                temp_dict["name"] = d[0]
                temp_dict["type"] = d[1]
                temp_dict["time"] = d[2]
                temp_dict["content"] = d[3]
                log_list.append(temp_dict)

        return log_list

    def insert_log(self, users, act_type, act_content):
        sql = "INSERT INTO t_log(act_time, user_name, act_type, act_content) VALUES(datetime('now', 'localtime'), '%s', %d, '%s')" \
                                                                                    % (users, act_type, act_content)
        db_instance = DBsqLite()
        db_instance.execute(sql)
        db_instance.close()
