wesley 6 years ago
parent
commit
e09e391ebe

+ 32 - 0
database/migrations/2018_12_19_111243_add_tag_to_call_records.php

xqd
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddTagToCallRecords extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('call_records', function (Blueprint $table) {
+            $table->string('tag',255)->after('call_id')->nullable()->commit('任务名称');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('call_records', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 25 - 18
pyshell/mysql_sync_psql.py

xqd xqd
@@ -47,28 +47,30 @@ if __name__ == '__main__':
     nums = mysql_cur.fetchall()
 
     postgres_cur = postgres_conn.cursor()
-    group_name = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
+    group_name = time.strftime('%Y-%m-%d', time.localtime(time.time()))
     # 生成号码组
     if nums:
-        psql_insert_group = "INSERT INTO ai_number_group(group_name, own_id) VALUES ('" + group_name + "', 1) RETURNING id"
-        postgres_cur.execute(psql_insert_group)
-        postgres_conn.commit()
-        group_id = postgres_cur.fetchone()[0]
+        #检查号码组是否存在,不存在则新建
+        psql_select_group = "Select id from ai_number_group where group_name = '" + group_name + "'"
+        postgres_cur.execute(psql_select_group)
+        group_id = postgres_cur.fetchone()
+
+        if (group_id == None):
+            psql_insert_group = "INSERT INTO ai_number_group(group_name, own_id) VALUES ('" + group_name + "', 1) RETURNING id"
+            postgres_cur.execute(psql_insert_group)
+            postgres_conn.commit()
+            group_id = postgres_cur.fetchone()[0]
+        else:
+            group_id = group_id[0]
 
         # 插入号码
         for num in nums:
             # 检查号码是否已经导入
-            psql_select_num = "SELECT dst_number from ai_numbers WHERE dst_number = '" + num['phone'] + "' "
+            psql_select_num = "SELECT id from ai_numbers WHERE dst_number = '" + num['phone'] + "' "
             postgres_cur.execute(psql_select_num)
-            rows = postgres_cur.fetchall()
-
-            # 更新mysql中该号码状态
-            mysql_sql_update = "UPDATE call_list SET sync=1 WHERE id= %s"
-            mysql_cur.execute(mysql_sql_update % (num['id']))
-            mysql_conn.commit()
-
+            rows = postgres_cur.fetchone()
             if (rows):
-                continue
+                num_id = rows[0]
             else:
                 # 插入号码到ai_numbers
                 psql_insert_num = "INSERT INTO ai_numbers(dst_number) VALUES ('" + num['phone'] + "') RETURNING id"
@@ -76,11 +78,16 @@ if __name__ == '__main__':
                 postgres_conn.commit()
                 num_id = postgres_cur.fetchone()[0]
 
-                # 将导入的号码添加到号码组
-                psql_insert_group_map = "INSERT INTO ai_number_group_map(group_id, number_id) VALUES ('" + str(
+            # 将导入的号码添加到号码组
+            psql_insert_group_map = "INSERT INTO ai_number_group_map(group_id, number_id) VALUES ('" + str(
                 group_id) + "', '" + str(num_id) + "')"
-                postgres_cur.execute(psql_insert_group_map)
-                postgres_conn.commit()
+            postgres_cur.execute(psql_insert_group_map)
+            postgres_conn.commit()
+
+            # 更新mysql中该号码状态
+            mysql_sql_update = "UPDATE call_list SET sync=1 WHERE id= %s"
+            mysql_cur.execute(mysql_sql_update % (num['id']))
+            mysql_conn.commit()
 
         mysql_conn.close()
         postgres_conn.close()

+ 10 - 3
pyshell/psql_sync_mysql.py

xqd
@@ -50,16 +50,23 @@ if __name__ == '__main__':
     last_id = mysql_cur.fetchone()
     last_id = last_id['cdr_id'] if last_id else 0
     # 获取要同步的通话详单
-    postgres_cur.execute("""select caller,start_time,end_time,record_path,hangup_dispostion,call_id,id,term_status,intention from ai_cdr  where id > %s order by id asc""",
+    postgres_cur.execute("""select caller,start_time,end_time,record_path,hangup_dispostion,call_id,id,term_status,intention,task_id from ai_cdr  where id > %s order by id asc""",
                          ([last_id]))
     call_records = postgres_cur.fetchall()
 
     for call_record in call_records:
+
+        postgres_cur.execute("""select tk_name from ai_task  where id = %s""",
+                         ([call_record[9]]))
+        task_name = postgres_cur.fetchone()
+        tk_id = call_record[9]
+        task_name = task_name['tk_name'] if task_name else '任务' + str(tk_id)
+
         record_path = ip+':8088/recordings/'+call_record[3]
         mysql_cur.execute(
-            """insert into  call_records(phone, start_time, end_time, record_path, hangup_dispostion, call_id, cdr_id, term_status,intention, ip) value (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
+            """insert into  call_records(phone, start_time, end_time, record_path, hangup_dispostion, call_id, cdr_id, term_status,intention, ip,tag) value (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
             (call_record[0], call_record[1], call_record[2], record_path,
-             call_record[4], call_record[5], call_record[6], call_record[7], call_record[8], ip)
+             call_record[4], call_record[5], call_record[6], call_record[7], call_record[8], ip,task_name)
         )
         mysql_conn.commit()