Browse Source

mvp: 增加性别占比信息的统计

增加行为图标关联

Signed-off-by: binren <zhangbr@elab-plus.com>
binren 5 years ago
parent
commit
e78e1d2281
1 changed files with 98 additions and 40 deletions
  1. 98 40
      mvp.py

+ 98 - 40
mvp.py

@@ -58,8 +58,18 @@ class Mvp:
         '1818': 'F',
         '1819': 'G'
     }
-    base_insert_sql = 'insert into {}(crowd_info_id, {}, standard_value, status) values(%s, %s, %s, ' \
-                      '1) '
+    base_insert_sql = '''
+        INSERT INTO {} (
+            crowd_info_id,
+            {},
+            standard_value,
+            STATUS,
+            creator,
+            created
+        )
+        VALUES
+            (%s, %s, %s, 1, 'binren', now())
+    '''
 
     def get_table_name(self, name):
         """
@@ -126,7 +136,7 @@ class Mvp:
     # 根据子选项id统计答题数
     sql_8 = '''
         SELECT
-            count(1)
+            count(DISTINCT a.uuid)
         FROM
             f_t_daren_score_2 a
         LEFT JOIN d_shangju_tiku_02 b ON a.sub_question_id = b.sub_question_id
@@ -260,6 +270,20 @@ class Mvp:
     sql_21 = '''
     '''
 
+    # 更新性别占比数据
+    sql_22 = '''
+            INSERT INTO mvp_crowd_info_gender_rate (
+            crowd_info_id,
+            gender,
+            standard_value,
+            STATUS,
+            creator,
+            created
+        )
+        VALUES
+            (%s, %s, %s, 1, 'binren', now())
+    '''
+
     """
         数据debug SQL
         1:
@@ -315,6 +339,7 @@ class Mvp:
         # self.score_module = ExcelUtil(file_name='行为与模块分值汇总.xlsx', sheet_name='模块').init_scores()
         self.scores_tag = None
         self.score_module = None
+        self.people_info_1 = self.people_info()
 
     def close(self):
         self.shangju_db.close()
@@ -366,10 +391,11 @@ class Mvp:
             city = people[1]
             nld = people[2]
             sex = people[3]
+            if sex:
+                sex = str(sex).split(',')[0]
             sub_option_ids_1 = people[4]
             testcaseid = people[5]
 
-
             if str(city).find('市') != -1:
                 city = str(city).split('市')[0] + '市'
 
@@ -434,16 +460,54 @@ class Mvp:
         """
         self.insert_table = []
         self.linshi_db = MysqlDB('linshi', db_type=1)
+        self.ids = self.query_data()
         for city in self.city_list:
             for age in self.age_list:
                 for crowd in self.crowd:
                     result = self.city_age_crowd(city, age, crowd)
                     self.insert_score_to_db(result)
-        print('{}数据关系完成...'.format(time.time()))
+        print('{}数据更新完成...'.format(time.time()))
+        self.update_gender_rate()
+        self.update_icon()
+
+    def update_gender_rate(self):
+        """
+            更新性别占比
+        :return:
+        """
+        insert_data = []
+        for city in self.city_list:
+            for age in self.age_list:
+                for crowd in self.crowd:
+                    boy = 0
+                    girl = 0
+                    for people in self.people_info_1:
+                        if people.sex is not None and city == people.city and crowd == people.crowd and age == people.age:
+                            if people.sex == 1:
+                                boy += 1
+                            if people.sex == 2:
+                                girl += 1
+                    crowd_info_id = self.get_crowd_info_id([city, age, crowd])
+                    if crowd_info_id:
+                        boy_rate = boy / (boy + girl)
+                        insert_data.append([crowd_info_id, 1, boy_rate])
+                        girl_rate = girl / (boy + girl)
+                        insert_data.append([crowd_info_id, 0, girl_rate])
+        self.linshi_db.add_some(self.sql_22, insert_data)
+        print('性别占比更新完成...')
+
+    def get_crowd_info_id(self, people_info):
+        for id_data in self.ids:
+            city_1 = id_data[2]
+            age_1 = id_data[1]
+            crowd_1 = id_data[3]
+            id_1 = id_data[0]
+            if people_info[0] == city_1 and people_info[1] == age_1 and people_info[2] == crowd_1:
+                return id_1
 
     def update_icon(self):
         """
-            更新行为对应图标
+            标签关联图标
         :return:
         """
         icons = self.linshi_db.select(self.sql_19)
@@ -451,32 +515,22 @@ class Mvp:
             id = ic[0]
             name = ic[1]
             self.linshi_db.update(self.sql_20, [id, name])
-        pass
+        print('行为标签关联图标完成...')
 
     def insert_score_to_db(self, scores):
         """
             行为、模块分数写入数据库
         :return:
         """
-        ids = self.query_data()
         behavior_score = scores['behavior_score']
         module_score = scores['module_score']
         module_insert_sql = self.get_insert_sql('模块分数')
         if module_insert_sql:
             module_insert_data = []
             for module in module_score:
-                city_2 = module[0]
-                age_2 = module[1]
-                crowd_2 = module[2]
-                module_name_2 = module[3]
-                module_score_2 = module[4]
-                for id in ids:
-                    city_1 = id[2]
-                    age_1 = id[1]
-                    crowd_1 = id[3]
-                    id_1 = id[0]
-                    if city_2 == city_1 and age_2 == age_1 and crowd_2 == crowd_1:
-                        module_insert_data.append([id_1, module_name_2, module_score_2])
+                data = self.need_inert(module)
+                if data:
+                    module_insert_data.append(data)
             # 先清空之前的数据
             if len(module_insert_data) > 0:
                 table_name = self.get_table_name('模块分数')
@@ -493,21 +547,9 @@ class Mvp:
                     insert_data = []
                     score = b_score[key]
                     for data in score:
-                        city = data[0]
-                        age = data[1]
-                        tag_name = data[2]
-                        crowd = data[3]
-                        tag_score = data[4]
-                        # if key == '用户画像-行为兴趣' and city == '上海市' and age == '85后':
-                        #     pass
-                        # else:
-                        for id in ids:
-                            city_1 = id[2]
-                            age_1 = id[1]
-                            crowd_1 = id[3]
-                            id_1 = id[0]
-                            if city == city_1 and age == age_1 and crowd == crowd_1:
-                                insert_data.append([id_1, tag_name, tag_score])
+                        insert_data_element = self.need_inert(data)
+                        if insert_data_element:
+                            insert_data.append(insert_data_element)
                     if len(insert_data) > 0:
                         table_name = self.get_table_name(key)
                         if table_name and table_name not in self.insert_table:
@@ -521,6 +563,23 @@ class Mvp:
                     print('未找到对应的表,数据无法插入...')
             print('行为分数更新完成...')
 
+    def need_inert(self, data):
+        city = data[0]
+        age = data[1]
+        crowd = data[2]
+        tag_name = data[3]
+        tag_score = data[4]
+        # if key == '用户画像-行为兴趣' and city == '上海市' and age == '85后':
+        #     pass
+        # else:
+        for id_data in self.ids:
+            city_1 = id_data[2]
+            age_1 = id_data[1]
+            crowd_1 = id_data[3]
+            id_1 = id_data[0]
+            if city == city_1 and age == age_1 and crowd == crowd_1:
+                return [id_1, tag_name, tag_score]
+
     def module_score(self, crowd, city, age, scores):
         """
             模块分数计算
@@ -631,7 +690,6 @@ class Mvp:
         data_start = []
         result = []
         module_scores = []
-        self.people_info_1 = self.people_info()
         if city is not None and age is not None and crowd is not None:
             print('获取指定城市,年龄段,人群类型的数据...')
             # people_uuids = self.get_people_uuid_by_type(crowd)
@@ -767,7 +825,7 @@ class Mvp:
                     if value[2] is not None and value[7] is not None:
                         f += float(value[2] * value[7])
                 print('     标准分:{}'.format(f))
-                scores_sub.append([city, age, key_tag, crowd_type, f])
+                scores_sub.append([city, age, crowd_type, key_tag, f])
             scores[key_tag_type] = scores_sub
             # self.shangju_db.add_some(self.sql_9, scores)
         return scores, scores['用户画像-行为兴趣']
@@ -851,7 +909,7 @@ class Mvp:
 
 
 if __name__ == '__main__':
-    # mvp = Mvp()
-    # mvp.linshi_db = MysqlDB('linshi', db_type=1)
-    # mvp.update_icon()
+    mvp = Mvp()
+    mvp.linshi_db = MysqlDB('linshi', db_type=1)
+    mvp.update_icon()
     pass