from tools import *

addr_level_4="""
select a.addr_id as id_1,a.addr_name as name_1  ,
b.addr_id as id_2 ,b.addr_name as name_2 ,
c.addr_id as id_3,c.addr_name as name_3, 
d.addr_id as id_4  ,d.addr_name as name_4 
from 
(select * from kdsp.t_area_address where addr_level=1) a 
join (select * from kdsp.t_area_address where addr_level=2) b on  a.addr_id=b.parent_addr_id
join (select * from kdsp.t_area_address where addr_level=3) c  on b.addr_id=c.parent_addr_id
left join (select * from kdsp.t_area_address where addr_level=4) d on  c.addr_id=d.parent_addr_id
order by RAND() limit 500
"""

addr_level_3="""
select a.addr_id as id_1,a.addr_name as name_1  ,
b.addr_id as id_2 ,b.addr_name as name_2 ,
c.addr_id as id_3,c.addr_name as name_3
from 
(select * from kdsp.t_area_address where addr_level=1) a 
join (select * from kdsp.t_area_address where addr_level=2) b on  a.addr_id=b.parent_addr_id
join (select * from kdsp.t_area_address where addr_level=3) c  on b.addr_id=c.parent_addr_id
order by RAND() limit 100
"""

user_addr_sql="""
INSERT INTO `kdsp`.`t_user_receiver_addr` ( `user_id`, `uuid`, `province_id`, `province_name`, `city_id`, `city_name`, `county_id`, `county_name`, `town_id`, `town_name`, `detail`, `receiver_name`, `receiver_mobile`, `zip_code`, `addr_default`, `tenant_id`, `enable`, `created_at`, `updated_at`)
 VALUES ( '{user_id}', '{uuid}', '{province_id}', '{province_name}', '{city_id}', '{city_name}', '{county_id}',
  '{county_name}', '{town_id}', '{town_name}', '{detail}', '{receiver_name}',
   '{receiver_mobile}', NULL, 1, 560761, 1, now(), now())
"""

user_uuid_sql="""
select id ,uuid,phone_no from xyqb_user.user order by rand() limit 500
"""

def gen_data():
    from tools.publicFun import gen_title
    user_uuid_df=execmysl('yxm',user_uuid_sql)
    addr_df=execmysl('yxm',addr_level_4)
    df=user_uuid_df.join(addr_df)
    temp_sql=[]
    df=df.astype(str)
    for i in df.itertuples():
        detail=gen_title('address').split('县')[-1].split('市')[-1].split('省')[-1]
        detail= detail if len(detail)>10 else detail+'测试测试'
        receiver_name=gen_title('name')
        user_addr_tempsql=user_addr_sql.format(
            user_id=i.__getattribute__('id'),
            uuid=i.__getattribute__('uuid'),
            province_id=i.__getattribute__('id_1'),
            province_name=i.__getattribute__('name_1'),
            city_id=i.__getattribute__('id_2'),
            city_name=i.__getattribute__('name_2'),
            county_id=i.__getattribute__('id_3'),
            county_name=i.__getattribute__('name_3'),
            town_id=i.__getattribute__('id_4'),
            town_name=i.__getattribute__('name_4'),
            detail=detail,
            receiver_name=receiver_name,
            receiver_mobile=i.__getattribute__('phone_no')
        )

        temp_sql.append(user_addr_tempsql)
    print(';'.join(temp_sql))
    try:
        execmysl('yxm',';'.join(temp_sql))
    except:
        print("insert succ")


if __name__ == '__main__':
    gen_data()
