package controllers

import (
	"SSO/enums"
	"SSO/service"
)

type DistrictController struct {
	BaseController
}

// /provinces [get]
func (this *DistrictController) GetProvinces() {
	provinces := service.GetAllProvince()
	if len(provinces) == 0 {
		this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBSelectNoResult)
		this.ServeJSON()
		return
	} else {
		this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
			"list": provinces,
		})
		this.ServeJSON()
		return
	}
}

// /city [get]
// @param province_id:int
func (this *DistrictController) GetCities() {
	pid, _ := this.GetInt("province_id", 0)
	if pid == 0 {
		this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		this.ServeJSON()
		return
	}
	cities := service.GetCitiesWithProvinceID(pid)
	if len(cities) == 0 {
		this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBSelectNoResult)
		this.ServeJSON()
		return
	} else {
		this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
			"list": cities,
		})
		this.ServeJSON()
		return
	}
}

// /district [get]
// @param city_id:int
func (this *DistrictController) GetDistricts() {
	cid, _ := this.GetInt("city_id", 0)
	if cid == 0 {
		this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		this.ServeJSON()
		return
	}
	cities := service.GetDistrictsWithCityID(cid)
	if len(cities) == 0 {
		this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBSelectNoResult)
		this.ServeJSON()
		return
	} else {
		this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
			"list": cities,
		})
		this.ServeJSON()
		return
	}
}
