package dao

import (
	"git.rosy.net.cn/jx-callback/business/model"
	"github.com/astaxie/beego/client/orm"
)

const (
	EnableCondAll      = 0
	EnableCondEnalbed  = 1
	EnableCondDisabled = 2
)

func GetPlaceByCode(db *DaoDB, code int) (place *model.Place, err error) {
	if db == nil {
		db = GetDB()
	}
	place = &model.Place{
		Code: code,
	}
	err = GetEntity(db, place, "Code")
	return place, err
}

func GetPlacesByCond(db *DaoDB, enableCond int) (placeList []*model.Place, err error) {
	if db == nil {
		db = GetDB()
	}
	sql := `
	SELECT *
	FROM place
	WHERE level = 2
	`
	if enableCond == EnableCondEnalbed {
		sql += " AND enabled = 1"
	} else if enableCond == EnableCondDisabled {
		sql += " AND enabled = 0"
	}
	sql += " ORDER BY code"
	err = GetRows(db, &placeList, sql)
	return placeList, err
}

// GetParentCodeByCode 根据城市code获取省份code
func GetParentCodeByCode(db *DaoDB, code int) (*model.Place, error) {
	sql := `
		SELECT p1.* FROM place p1 
		INNER JOIN place p2  On p1.code = p2.parent_code 
		WHERE p2.code =  ?	`

	place := &model.Place{}
	if err := GetRow(db, place, sql, []interface{}{code}...); err != nil {
		return nil, err
	}
	return place, nil
}

func GetPlaceByName(db *DaoDB, name string, level int, parentCode int) (place *model.Place, err error) {
	if db == nil {
		db = GetDB()
	}
	cols := []string{
		"Name",
		"Level",
	}
	place = &model.Place{
		Name:  name,
		Level: int8(level),
	}
	if parentCode != 0 {
		cols = append(cols, "ParentCode")
		place.ParentCode = parentCode

	}
	if err = GetEntity(db, place, cols...); err == orm.ErrNoRows {
		err = db.Db.Raw("SELECT * FROM place WHERE parent_code = ? AND level = ? AND name LIKE ?", parentCode, level, "%"+name+"%").QueryRow(place)
	}
	return place, err
}

func GetPlaceByJdCode(db *DaoDB, jdCode int) (place *model.Place, err error) {
	if db == nil {
		db = GetDB()
	}
	place = &model.Place{
		JdCode: jdCode,
	}
	err = db.Db.Read(place, "JdCode")
	return place, err
}

func GetPlaceByJdsCode(db *DaoDB, jdsCode int) (place *model.Place, err error) {
	if db == nil {
		db = GetDB()
	}
	place = &model.Place{
		JdsCode: jdsCode,
	}
	err = db.Db.Read(place, "JdsCode")
	return place, err
}

func GetPlaces(db *DaoDB, cityCodes []int) (places []*model.Place, err error) {
	sql := `
		SELECT * 
		  FROM place 
		 WHERE 1 = 1 
	`
	sqlParams := []interface{}{}
	if len(cityCodes) > 0 {
		sql += " AND code IN (" + GenQuestionMarks(len(cityCodes)) + ")"
		sqlParams = append(sqlParams, cityCodes)
	}
	err = GetRows(db, &places, sql, sqlParams)
	return places, err
}

func GetPlacesByDistrictCode(districtCode int) (*DistrictName, error) {
	sql := ` 
				SELECT p1.name province_name,p1.code province_code,p2.name city_name,p2.code city_code,p3.name county_name,p3.code county_code FROM place p1
				LEFT JOIN place p2 ON p2.parent_code = p1.code
				LEFT JOIN place p3 ON p3.parent_code = p2.code
				WHERE p3.code = ? 
`
	sqlParams := []interface{}{districtCode}
	place := &DistrictName{}
	if err := GetRow(GetDB(), place, sql, sqlParams...); err != nil {
		return nil, err
	}

	return place, nil
}

type DistrictName struct {
	ProvinceName string `json:"province_name"`
	ProvinceCode int    `json:"province_code"`
	CityName     string `json:"city_name"`
	CityCode     int    `json:"city_code"`
	CountyName   string `json:"county_name"`
	CountyCode   int    `json:"county_code"`
}
