def add_right_wrong(qid, field, is_worked, business_id, supplier_id):
     if qid:
         sql = """
         update supplier_qa_history set {field}={field}+ 1,is_worked='{is_worked}' where business_id={business_id} and supplier_id={supplier_id} and  id = {id}""".format(
             field=field,
             is_worked=is_worked,
             business_id=business_id,
             supplier_id=supplier_id,
             id=qid)
         n = PgDbDumper.update(conn_str, sql)
         logger.info("SupplierQaHistory table add_right_wrong ={}".format(
             dict(qid=qid, business_id=business_id,
                  supplier_id=supplier_id)))
         return n
 def add_history(business_id,
                 supplier_id,
                 question,
                 answer,
                 origin_question,
                 qtype,
                 btype,
                 username='******',
                 data=None,
                 robot=None,
                 intent_type=0,
                 is_worked=0,
                 right_count=0,
                 wrong_count=0):
     sql = """
     insert into supplier_qa_history(business_id,supplier_id,question, answer,origin_question, qtype,btype, username, create_time, qa_dict, robot,intent_type,is_worked,right_count,wrong_count)
     values('{business_id}','{supplier_id}','{question}', '{answer}','{origin_question}','{qtype}', '{btype}', '{username}', now(), E'{qa_dict}', '{robot}','{intent_type}','{is_worked}','{right_count}','{wrong_count}')
     """.format(business_id=business_id,
                supplier_id=supplier_id,
                question=question,
                answer=answer,
                origin_question=origin_question,
                qtype=qtype,
                btype=btype,
                username=username,
                qa_dict=re.sub("'", "\\'", data),
                robot=robot if robot else btype,
                intent_type=intent_type,
                is_worked=is_worked,
                right_count=right_count,
                wrong_count=wrong_count)
     PgDbDumper.update(conn_str, sql)
     logger.info("SupplierQaHistory table add_history ={}".format(
         dict(business_id=business_id,
              supplier_id=supplier_id,
              queetion=question,
              answer=answer)))
 def update(business_id, supplier_id, default_questions, update_user,
            status):
     if business_id and supplier_id:
         sql = '''
         insert into supplier_robot_config(business_id, supplier_id, default_questions, update_user, status)
         values({business_id}, {supplier_id},  ARRAY['{default_questions}'], '{update_user}', {status}) 
         on conflict (business_id, supplier_id) do update set  default_questions=ARRAY['{default_questions}'], 
         update_user='******', status={status}, update_time=now() 
         '''.format(business_id=business_id,
                    supplier_id=supplier_id,
                    default_questions=array_to_string(default_questions),
                    update_user=update_user,
                    status=status)
         n = PgDbDumper.update(conn_str, sql)
         logger.info("SupplierRobotConfig table update ={}".format(
             default_questions))
         return n
     return 0
 def update(qaid, business_id, supplier_id, question, answer, status,
            update_user):
     if len(qaid) == 0:
         sql = """
         insert into supplier_qa_data(business_id, supplier_id, question, answer, update_user) values({business_id}, {supplier_id}, ARRAY['{question}'], '{answer}', '{update_user}')
         """.format(business_id=business_id,
                    supplier_id=supplier_id,
                    question=array_to_string(question),
                    answer=sql_escape(answer),
                    update_user=update_user)
     else:
         sql = """
         update supplier_qa_data set business_id={business_id}, supplier_id={supplier_id}, question=ARRAY['{question}'], answer='{answer}'=}', status={status}, update_user='******' where id = {id}""".format(
             id=qaid,
             business_id=business_id,
             supplier_id=supplier_id,
             question=array_to_string(question),
             answer=sql_escape(answer),
             status=status,
             update_user=update_user)
     logger.info("SupplierQaData table update ={}".format(
         dict(question=question, answer=answer)))
     n = PgDbDumper.update(conn_str, sql)
     return n