独角鲸同步合作方公司数据项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

72 lines
3.2 KiB

import logging
import traceback
from rest_framework.views import APIView
# 去掉区县级
from ChaCeRndTrans.basic import CCAIResponse
from ChaCeRndTrans.code import SERVER_ERROR
from common.models import Area
logger = logging.getLogger('error')
class NewRegisterAreaListAPIView(APIView):
'''
注册地区
'''
def get(self, request, format=None):
try:
sql = 'SELECT a.id, b.DataDictionaryDetailId as dict_id,' \
'b.ParentCode as parent_code, b.ParentId as parent_id, b.DictionaryValue as dict_val, b.DictionaryCode as dict_code,' \
'b.FullName as dict_name, b.Remark as remark, b.IsEnabled as is_enabled, b.SortNo as sort_no, a.BaiDuCode as baidu_code,' \
'a.IsMunicipality as is_municipality, a.IsProvince as is_province, a.IsCity as is_city, a.IsTownship as is_town_ship, a.IsHotCity as is_hot_city,' \
'a.Code as code, a.Abbreviation as abbreviation, b.SortNoTwo as sort_no_two, a.TopImg as top_img ' \
'FROM common_area a ' \
'LEFT JOIN common_datadictionarydetail b ' \
'ON a.DataDictionaryDetailId = b.DataDictionaryDetailId WHERE b.FullName != "" and (a.IsMunicipality = 1 or a.IsProvince = 1 or a.IsCity = 1)'
query_rows = Area.objects.raw(sql)
municipality_map = {}
municipality_data = []
province_data = []
all_data = []
dict_map = {}
tree_dict = {}
tree_data = []
for item in query_rows:
item.__dict__.pop('_state')
if item.parent_code != 'RegisterArea_' and item.dict_code != 'RegisterArea_' and item.dict_code.count(
'_') < 5 and 'RegisterArea_ZXS_Chongqing_' not in item.dict_code and 'RegisterArea_ZXS_Tianjin_' not in item.dict_code and 'RegisterArea_ZXS_Beijing_' not in item.dict_code and 'RegisterArea_ZXS_Shanghai_' not in item.dict_code:
dict_map[item.dict_code] = item.dict_name
tree_dict[item.dict_id] = item.__dict__
for i in tree_dict:
data = tree_dict[i]
parent_id = tree_dict[i]['parent_id']
parent_code = tree_dict[i]['parent_code']
if parent_id and parent_code in dict_map:
pid = parent_id
parent = tree_dict[pid]
parent.setdefault('children', []).append(data)
else:
tree_data.append(data)
for item in tree_data:
if item['is_municipality'] == 1:
municipality_data.append(item)
else:
province_data.append(item)
municipality_map['dict_code'] = 'RegisterArea_ZXS'
municipality_map['dict_name'] = '直辖市'
municipality_map['children'] = municipality_data
all_data.append(municipality_map)
all_data.extend(province_data)
return CCAIResponse(all_data)
except Exception as e:
logger.error("user: %s, get area failed: \n%s" % (request.user.id, traceback.format_exc()))
return CCAIResponse("获取地区失败", SERVER_ERROR)