package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
)

func GetRegionIdForName(region_name string, region_type int64) (err error, region model.Region) {
	sql := "select * from region where region_name like '%" + region_name + "%'"
	if region_type == 1 {
		sql += " AND parent_id = 0"
	} else if region_type == 2 {
		sql += " AND parent_id in (SELECT region_id FROM region WHERE parent_id=0)"
	} else if region_type == 3 {
		sql += " AND parent_id in (SELECT region_id FROM region WHERE parent_id>0)"
	}
	global.GVA_DB.Raw(sql).First(&region)
	return
}

func GetRegion() (error, []*model.Region) {
	data := make([]*model.Region, 0)
	sql := "select * from region"
	global.GVA_DB.Raw(sql).Scan(&data)
	return nil, data
}
