package main

import (
	"fmt"
	log "github.com/donnie4w/go-logger/logger"
	qu "qfw/util"
)

func saveAreaMysql() {
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q, total := map[string]interface{}{}, 0
	it := sess.DB(save_mgo.DbName).C(o_area_coll).Sort("_id").Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%1000 == 0 {
			log.Debug("curent index ", total)
		}
		tmpid := BsonTOStringId(tmp["_id"])
		delete(tmp, "_id")
		code := qu.IntAll(tmp["code"])
		tmp["code"] = code
		insertMysqlData("v_area", tmp, tmpid)
		tmp = make(map[string]interface{})
	}
	log.Debug("is over ", total)
}

func exportAreaMysql() {
	sess := qy_mgo.GetMgoConn()
	defer qy_mgo.DestoryMongoConn(sess)
	q, total := map[string]interface{}{
		"town": map[string]interface{}{
			"$exists": 0,
		},
	}, 0
	//二级map
	infoMap := map[string]string{}
	codeMap := map[string]string{}
	it := sess.DB(qy_mgo.DbName).C(s_area_coll).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		province := qu.ObjToString(tmp["province"])
		city := qu.ObjToString(tmp["city"])
		district := qu.ObjToString(tmp["district"])
		province_code := fmt.Sprintf("%d0000", tmp["province_code"])
		city_code := fmt.Sprintf("%d00", tmp["city_code"])
		district_code := fmt.Sprintf("%d", tmp["district_code"])
		data := map[string]interface{}{}
		if district == "" {
			if city == "" {
				data = map[string]interface{}{
					"name":  province,
					"code":  province_code,
					"level": "1",
					"pcode": "",
				}
				save_mgo.Save(o_area_coll, data)
				if codeMap[province_code] == "" {
					codeMap[province_code] = province_code
				} else {
					log.Debug(data)
				}
			} else {
				if city == "北京市" {
					city_code = "110100"
				}
				if city == "天津市" {
					city_code = "120100"
				}
				if city == "重庆市" {
					city_code = "500100"
				}
				if city == "上海市" {
					city_code = "310100"
				}
				data = map[string]interface{}{
					"name":  city,
					"code":  city_code,
					"level": "2",
					"pcode": province_code,
				}
				infoMap[city_code] = city_code
				save_mgo.Save(o_area_coll, data)

				if codeMap[city_code] == "" {
					codeMap[city_code] = city_code
				} else {
					log.Debug(data)
				}

				if city == "重庆市" {
					city_code = "500200"
					data = map[string]interface{}{
						"name":  city,
						"code":  city_code,
						"level": "2",
						"pcode": province_code,
					}
					infoMap[city_code] = city_code
					save_mgo.Save(o_area_coll, data)

					if codeMap[city_code] == "" {
						codeMap[city_code] = city_code
					} else {
						log.Debug(data)
					}
				}
			}
		} else {

			if infoMap[city_code] == "" {
				data = map[string]interface{}{
					"name":  district,
					"code":  district_code,
					"level": "2",
					"pcode": province_code,
				}
				infoMap[district_code] = district_code
				save_mgo.Save(o_area_coll, data)

				if codeMap[district_code] == "" {
					codeMap[district_code] = district_code
				} else {
					log.Debug(data)
				}
			} else {
				//确保一下-是否重复三级城市
				data = map[string]interface{}{
					"name":  district,
					"code":  district_code,
					"level": "3",
					"pcode": city_code,
				}
				if codeMap[district_code] == "" {
					save_mgo.Save(o_area_coll, data)
					codeMap[district_code] = district_code
				} else {
					log.Debug(data)
				}

			}
			tmp = make(map[string]interface{})
		}
	}
	log.Debug("is area over ", total)
}