using Ewide.Core;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Ewide.Application.Service
{
///
/// 片区相关
///
[ApiDescriptionSettings(Name = "HouseZone", Order = 180)]
public class HouseZoneService : IHouseZoneService, IDynamicApiController, ITransient
{
private readonly IRepository _sysOrgRep;
private readonly IRepository _sysEmpRep;
public HouseZoneService(
IRepository sysOrgRep,
IRepository sysEmpRep
)
{
_sysOrgRep = sysOrgRep;
_sysEmpRep = sysEmpRep;
}
///
/// 获取片区列表
///
///
///
[HttpGet("/houseZone/list")]
public async Task GetHouseZoneList([FromQuery] HouseZoneInput input)
{
var areaCode = input.AreaCode.Substring(0, 9);
var road = await _sysOrgRep.DetachedEntities.FirstOrDefaultAsync(p => p.AreaCode == areaCode);
if (road == null) throw Oops.Oh("未在组织机构中配置街道");
return await _sysOrgRep.DetachedEntities
.Where(p => p.Pid == road.Id)
.Where(p => p.Type == (int)OrgType.片区)
.OrderBy(p => p.Sort)
.Select(p => new
{
p.Id,
p.Name
})
.ToListAsync();
}
///
/// 根据用户Id获取所在片区的Id
///
///
///
[HttpGet("houseZone/getByUser")]
public async Task GetZoneByUser([FromQuery][Required(ErrorMessage = "用户Id不能为空")] string userId)
{
var data = await _sysEmpRep.DetachedEntities.FirstOrDefaultAsync(p => p.Id == userId);
if(data == null) throw Oops.Oh("用户不在组织机构中");
var org = await _sysOrgRep.DetachedEntities.FirstOrDefaultAsync(p => p.Id == data.OrgId && p.Type == (int)OrgType.片区);
if(org == null) throw Oops.Oh("用户不在片区中");
return org.Id;
}
}
}