# -*- coding: utf-8 -*-

import pymysql
import os,sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,parentdir)
from webmodules.basehandler import BaseHandler


'''
    参数设置
'''
# 新增参数
class addSysParam(BaseHandler):
    def post(self):
        # 获取公用参数
        param_code = self.get_argument('param_code')
        param_description = self.get_argument('param_value')
        parent_code = self.get_argument('parent_code')
        level = self.get_argument('level')
        comment = self.get_argument('comment')
        self.cur = self.mysql_conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = "insert into facerecognize.sys_param VALUES  \
                      ('%s', '%s', '%s','%s','%s')" % \
              (param_code, param_description, parent_code, level,comment)
        # print sql
        self.cur.execute(sql)
        self.mysql_conn.commit()
        self.cur.close()
        self.mysql_conn.close()
        self.finish({
            'msg': 'ok',
            # 'data': {'personId': self.cur.lastrowid},
        })


# 获取某个级别的参数--用于select
class getSysParam(BaseHandler):
    def get(self):
        parent_code = self.get_argument('parent_code')
        self.cur = self.mysql_conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = 'SELECT p.param_code,p.param_description' \
              ' FROM facerecognize.sys_param p WHERE p.parent_code="%s"'% parent_code
        self.cur.execute(sql)
        rs = self.cur.fetchall()
        self.cur.close()
        self.mysql_conn.close()
        self.finish({
            'msg': 'ok',
            'data': {'sysParamList': rs},
        })


# 编辑某个参数
class editSysParam(BaseHandler):
    def post(self):
        # 获取公用参数
        old_param_code = self.get_argument('old_param_code')
        param_code = self.get_argument('param_code')
        param_description = self.get_argument('param_value')
        parent_code = self.get_argument('parent_code')
        level = self.get_argument('level')
        comment = self.get_argument('comment')
        self.cur = self.mysql_conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = "UPDATE facerecognize.sys_param set param_code = '%s',param_description = '%s', parent_code = '%s'," \
              " `level` = '%s',`comment` = '%s'  " \
              "WHERE param_code = '%s' " \
              % (param_code, param_description, parent_code, level,comment,old_param_code)
        self.cur.execute(sql)
        self.mysql_conn.commit()
        self.cur.close()
        self.mysql_conn.close()
        self.finish({
            'msg': 'ok'
        })


# 获取某个参数
class getOneSysParam(BaseHandler):
    def get(self):
        param_code = self.get_argument('param_code')
        self.cur = self.mysql_conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = 'SELECT p.param_code,p.param_description,ifnull(p.parent_code,"") AS parent_code,' \
              ' p.`level`,ifnull(p.`comment`,"") AS `comment` ' \
              ' FROM facerecognize.sys_param p WHERE p.param_code="%s" '% param_code
        self.cur.execute(sql)
        rs = self.cur.fetchall()
        self.cur.close()
        self.mysql_conn.close()
        self.finish({
            'msg': 'ok',
            'data': {'sysParam': rs[0]},
        })


# 方法-获取某个参数
def get_sys_param(self, param_code):
        self.cur = self.mysql_conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = 'SELECT p.param_description ' \
              ' FROM facerecognize.sys_param p WHERE p.param_code="%s" '% param_code
        self.cur.execute(sql)
        rs = self.cur.fetchone()
        return rs['param_description']

