#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# Author: hj
# softWare:pycharm
# fileName:consultation_record.py
# createTime:2020/7/6 14:50

# 读表获取咨询记录
import datetime,ast

from flask import request

from common.re_companyName import strQ2B
from dao.consultation_record_dao import consultation_Dao

consultation_sql = consultation_Dao()

def consultingRecord(company_name):
    consultation_list = []
    consultation_info = consultation_sql.select_sql(company_name)
    # for row in consultation_info:
    #     consultation_dict['channel'] = row['source']
    #     consult_product = ast.literal_eval(row['consult_product'])
    #     consult_product_list = []
    #     for consult_product_item in consult_product:
    #         consult_product = consult_product_item['product_name']
    #         re_consult_product = strQ2B(consult_product)
    #         consult_product_list.append(re_consult_product)
    #     str1 = ";".join(map(str, consult_product_list))
    #     consultation_dict['consult_product'] = str1
    #     consultation_dict['customer_address'] = row['address']
    #     consult_time_info = row['create_time']
    #     consultation_dict['consult_time'] = str(consult_time_info)
    #     consultation_list.append(consultation_dict)
    for row in consultation_info:
        consult_product = ast.literal_eval(row['consult_product'])
        for consult_product_item in consult_product:
            consultation_dict = {}
            consult_product = consult_product_item['product_name']
            re_consult_product = strQ2B(consult_product)
            # consultation_dict['product_price'] = consult_product_item['product_price']
            if re_consult_product:
                print('id',row)
                consultation_dict['channel'] = row['source']
                consultation_dict['consult_product'] = re_consult_product
                consultation_dict['customer_address'] = row['address']
                consultation_dict['id'] = row['id']
                consultation_dict['customer_name'] = row['customer_name']
                consult_time_info = row['create_time']
                consultation_dict['consult_time'] = str(consult_time_info)
                consultation_list.append(consultation_dict)
    print(consultation_list)
    return consultation_list
# consultingRecord('北京爱诺斯科技有限公司')
#
def consultingInfo(server):
    @server.route('/gsb/api/consultingRecord', methods=['POST'])
    def consultMain():
        if request.method == 'POST':
            consultParams = request.json
            company_name = consultParams.get('company_name')
            try:
                consultation_list = consultingRecord(company_name)
                return {"status": 200, "msg": "成功", "data": consultation_list}
            except Exception as e:
                print('咨询记录错误',e)
                return {"status": 404, "msg": "失败", "data": ""}