# -*- coding: utf8 -*-

from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import Mutation
from configparser import ConfigParser
import hashlib


class HbaseOperation:

    def __init__(self):
        cfg = ConfigParser()
        cfg.read('..\\config.ini')
        ip = cfg.get('hbase', 'ip')
        port = cfg.get('hbase', 'port')
        socket = TSocket.TSocket(ip, port)
        socket.setTimeout(2000)
        self.table = 'test'

        transport = TTransport.TBufferedTransport(socket)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)

        self.client = Hbase.Client(protocol)
        socket.open()
        # print(self.client.getTableNames())

    def put_hlw(self, header):
        mutations = []

        company_name = Mutation(column=b'cf:company_name', value=header['company_name'])
        version = Mutation(column=b'cf:version', value=header['version'])
        ecg_wave = Mutation(column=b'cf:ecg_wave', value=header['ecg_wave'])
        ecg_feq = Mutation(column=b'cf:ecg_feq', value=header['ecg_feq'])
        file_length = Mutation(column=b'cf:file_length', value=header['file_length'])
        data_length = Mutation(column=b'cf:data_length', value=header['data_length'])
        begin_time = Mutation(column=b'cf:begin_time', value=header['data_length'])
        end_time = Mutation(column=b'cf:end_time', value=header['end_time'])
        crc_head = Mutation(column=b'cf:crc_head', value=header['crc_head'])
        crc_data = Mutation(column=b'cf:crc_data', value=header['crc_data'])
        id = Mutation(column=b'cf:id', value=header['id'])
        name = Mutation(column=b'cf:name', value=header['company_name'])
        birthday = Mutation(column=b'cf:birthday', value=header['birthday'])
        sex = Mutation(column=b'cf:sex', value=header['sex'])
        age = Mutation(column=b'cf:age', value=header['age'])
        height = Mutation(column=b'cf:height', value=header['height'])
        weight = Mutation(column=b'cf:weight', value=header['weight'])
        phone = Mutation(column=b'cf:phone', value=header['phone'])
        unit = Mutation(column=b'cf:unit', value=header['unit'])
        address = Mutation(column=b'cf:address', value=header['address'])
        e_name = Mutation(column=b'cf:e_name', value=header['e_name'])
        e_phone = Mutation(column=b'cf:e_phone', value=header['e_phone'])
        bed = Mutation(column=b'cf:bed', value=header['bed'])
        doctor = Mutation(column=b'cf:doctor', value=header['doctor'])
        remark = Mutation(column=b'cf:remark', value=header['remark'])
        reserved = Mutation(column=b'cf:reserved', value=header['reserved'])
        content = Mutation(column=b'cf:content', value=header['content'])

        mutations.append(company_name)
        mutations.append(version)
        mutations.append(ecg_wave)
        mutations.append(ecg_feq)
        mutations.append(file_length)
        mutations.append(data_length)
        mutations.append(begin_time)
        mutations.append(end_time)
        mutations.append(crc_head)
        mutations.append(crc_data)
        mutations.append(id)
        mutations.append(name)
        mutations.append(birthday)
        mutations.append(sex)
        mutations.append(age)
        mutations.append(height)
        mutations.append(weight)
        mutations.append(phone)
        mutations.append(unit)
        mutations.append(address)
        mutations.append(e_phone)
        mutations.append(e_name)
        mutations.append(bed)
        mutations.append(doctor)
        mutations.append(remark)
        mutations.append(reserved)
        mutations.append(content)
        head = header['id'].decode('utf-8')
        row_id = hashlib.md5(header['id']).hexdigest() + head[::-1]
        self.client.mutateRow(b'test', row_id.encode(), mutations, {})

    def put_hly(self, header):
        mutations = []

        company_name = Mutation(column=b'cf:company_name', value=header['company_name'])
        version = Mutation(column=b'cf:version', value=header['version'])
        ecg_wave = Mutation(column=b'cf:ecg_wave', value=header['ecg_wave'])
        ecg_feq = Mutation(column=b'cf:ecg_feq', value=header['ecg_feq'])
        other_wave = Mutation(column=b'cf:other_wave', value=header['other_wave'])
        other_feq = Mutation(column=b'cf:other_feq', value=header['other_feq'])
        file_length = Mutation(column=b'cf:file_length', value=header['file_length'])
        data_length = Mutation(column=b'cf:data_length', value=header['data_length'])
        begin_time = Mutation(column=b'cf:begin_time', value=header['data_length'])
        end_time = Mutation(column=b'cf:end_time', value=header['end_time'])
        crc_head = Mutation(column=b'cf:crc_head', value=header['crc_head'])
        crc_data = Mutation(column=b'cf:crc_data', value=header['crc_data'])
        id = Mutation(column=b'cf:id', value=header['id'])
        name = Mutation(column=b'cf:name', value=header['company_name'])
        birthday = Mutation(column=b'cf:birthday', value=header['birthday'])
        sex = Mutation(column=b'cf:sex', value=header['sex'])
        age = Mutation(column=b'cf:age', value=header['age'])
        height = Mutation(column=b'cf:height', value=header['height'])
        weight = Mutation(column=b'cf:weight', value=header['weight'])
        phone = Mutation(column=b'cf:phone', value=header['phone'])
        unit = Mutation(column=b'cf:unit', value=header['unit'])
        address = Mutation(column=b'cf:address', value=header['address'])
        e_name = Mutation(column=b'cf:e_name', value=header['e_name'])
        e_phone = Mutation(column=b'cf:e_phone', value=header['e_phone'])
        bed = Mutation(column=b'cf:bed', value=header['bed'])
        doctor = Mutation(column=b'cf:doctor', value=header['doctor'])
        remark = Mutation(column=b'cf:remark', value=header['remark'])
        field = Mutation(column=b'cf:field', value=header['field'])
        reserved = Mutation(column=b'cf:reserved', value=header['reserved'])
        content = Mutation(column=b'cf:content', value=header['content'])

        mutations.append(company_name)
        mutations.append(version)
        mutations.append(ecg_wave)
        mutations.append(ecg_feq)
        mutations.append(other_wave)
        mutations.append(other_feq)
        mutations.append(file_length)
        mutations.append(data_length)
        mutations.append(begin_time)
        mutations.append(end_time)
        mutations.append(crc_head)
        mutations.append(crc_data)
        mutations.append(id)
        mutations.append(name)
        mutations.append(birthday)
        mutations.append(sex)
        mutations.append(age)
        mutations.append(height)
        mutations.append(weight)
        mutations.append(phone)
        mutations.append(unit)
        mutations.append(address)
        mutations.append(e_phone)
        mutations.append(e_name)
        mutations.append(bed)
        mutations.append(doctor)
        mutations.append(remark)
        mutations.append(field)
        mutations.append(reserved)
        mutations.append(content)
        head = header['id'].decode('utf-8')
        row_id = hashlib.md5(header['id']).hexdigest() + head[::-1]
        self.client.mutateRow(b'test', row_id.encode(), mutations, {})
