using AgriculturalPlatform.Api.Data; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace AgriculturalPlatform.Api.Controllers; /// /// 行政区划接口: /// 省、市、县、乡镇来自 AreaCity 全国公开行政区划数据(Regions 表,深 0~3); /// 村、组来自农户档案表(Famers 表已录入数据,去重后作为下拉选项)。 /// [ApiController] [Route("api/regions")] [Authorize] public class RegionsController(AppDbContext db) : ControllerBase { /// 省(自治区/直辖市)列表,热门省份(热度值高)优先 [HttpGet("provinces")] public async Task> Provinces() { var items = await db.Regions .Where(r => r.Deep == 0) .OrderByDescending(r => r.Hot) .ThenBy(r => r.Id) .Select(r => r.ExtName) .ToListAsync(); return Ok(items); } /// /// 按省获取县(区/县级市)列表,附带所属地级市(city)用于前端分组, /// used=true 表示数据库农户表中已录入过该区县(前端优先展示)。 /// [HttpGet("counties")] public async Task Counties(string? province) { if (string.IsNullOrWhiteSpace(province)) return BadRequest(new { message = "请先选择省份" }); var prov = await db.Regions.FirstOrDefaultAsync(r => r.Deep == 0 && r.ExtName == province); if (prov == null) return Ok(Array.Empty()); var items = await (from c in db.Regions join city in db.Regions on c.Pid equals city.Id where city.Pid == prov.Id && c.Deep == 2 orderby city.Id, c.Id select new { name = c.ExtName, city = city.ExtName }) .ToListAsync(); var usedNames = await db.Farmers .Where(f => f.County != null && f.County != "") .Select(f => f.County) .Distinct() .ToListAsync(); var used = new HashSet(usedNames, StringComparer.OrdinalIgnoreCase); return Ok(items.Select(c => new { c.name, c.city, used = used.Contains(c.name) })); } /// 按县获取乡镇(街道)列表(公开数据 + 农户表已录入数据补充) [HttpGet("townships")] public async Task> Townships(string? county, string? city) { if (string.IsNullOrWhiteSpace(county)) return BadRequest(new { message = "请先选择县(区)" }); var countyQuery = db.Regions.Where(r => r.Deep == 2 && r.ExtName == county); if (!string.IsNullOrWhiteSpace(city)) { var cityRow = await db.Regions.FirstOrDefaultAsync(r => r.Deep == 1 && r.ExtName == city); if (cityRow != null) countyQuery = countyQuery.Where(r => r.Pid == cityRow.Id); } var ids = await countyQuery.Select(r => r.Id).ToListAsync(); var builtin = ids.Count > 0 ? await db.Regions.Where(r => r.Deep == 3 && ids.Contains(r.Pid)) .OrderBy(r => r.Id).Select(r => r.ExtName).ToListAsync() : []; var fromDb = await db.Farmers .Where(f => f.County == county && f.Township != null && f.Township != "") .Select(f => f.Township) .Distinct() .ToListAsync(); return Ok(builtin.Union(fromDb).OrderBy(x => x).ToArray()); } /// /// 村列表(来自农户表,按县/乡镇过滤去重),返回 { name, township } 对象数组。 /// 不传 township 时返回全县所有村(含所属乡镇),供前端做同名村检测。 /// [HttpGet("villages")] public async Task Villages(string? county, string? township) { var q = db.Farmers.Where(f => f.Village != null && f.Village != ""); if (!string.IsNullOrWhiteSpace(township)) q = q.Where(f => f.Township == township); else if (!string.IsNullOrWhiteSpace(county)) q = q.Where(f => f.County == county); var items = await q .Select(f => new { f.Village, f.Township }) .Distinct() .OrderBy(x => x.Village) .ToListAsync(); return Ok(items.Select(x => new { name = x.Village, township = x.Township ?? "" })); } /// 组列表(来自农户表,按县/乡镇/村过滤去重) [HttpGet("groups")] public async Task> Groups(string? county, string? township, string? village) { var q = db.Farmers.Where(f => f.GroupName != null && f.GroupName != ""); if (!string.IsNullOrWhiteSpace(village)) q = q.Where(f => f.Village == village); else if (!string.IsNullOrWhiteSpace(township)) q = q.Where(f => f.Township == township); else if (!string.IsNullOrWhiteSpace(county)) q = q.Where(f => f.County == county); var items = await q.Select(f => f.GroupName).Distinct().OrderBy(x => x).ToListAsync(); return Ok(items); } }