using MoYu.FriendlyException;
using MoYu.RemoteRequest.Extensions;
using Newtonsoft.Json.Linq;
using SafeCampus.Application.Manager.DeepelephManager;
using SafeCampus.Application.Services.Business.ClassTeacherService;
using SafeCampus.Application.Services.Business.PersonSetInfoService;
namespace SafeCampus.Web.Core.Controllers.Application.Business;
///
/// 人员班级管理接口
///
[Route("/business/[controller]")]
[ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Order = 99,Tag = "人员班级管理")]
public class DfieldApi : IDynamicApiController
{
private readonly IDeepelephManager _deepelephManager;
private readonly IClassTeacherService _classTeacherService;
private readonly IPersonSetInfoService _personSetInfoService;
public DfieldApi(IDeepelephManager deepelephManager, IClassTeacherService classTeacherService, IPersonSetInfoService personSetInfoService)
{
_deepelephManager = deepelephManager;
_classTeacherService = classTeacherService;
_personSetInfoService = personSetInfoService;
}
///
/// 创建底库
///
///
public async Task CreateDfieldA(ControllersNameInput input)
{
if (await _personSetInfoService.CheckName(input.Name,null))
{
throw Oops.Oh("【"+input.Name+"】班级名称已存在");
}
var personSetId = Guid.NewGuid().ToString("N");
var appSettings = App.GetOptionsMonitor();
var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/create"
.SetBody(new
{
token = _deepelephManager.GetToken(),
tenantCode = appSettings.TenantCode,
poiId = appSettings.PoiId,
personSetId,
personSetName = input.Name,
})
.SetContentType("application/json")
.PostAsAsync();
var model = JsonConvert.DeserializeObject(str);
if ((bool)model["success"])
{
await _personSetInfoService.Add(new PersonSetInfoDto{PersonSetId = personSetId ,PersonSetName = input.Name,MajorId = input.MajorId});
return model["data"];
}
throw Oops.Oh(model["message"].ToString());
}
///
/// 查询底库列表
///
///
public async Task QueryAll(long? majorId,string personSetName)
{
return await _personSetInfoService.GetPageList(majorId, personSetName);
var appSettings = App.GetOptionsMonitor();
var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/queryAll"
.SetBody(new
{
token = _deepelephManager.GetToken(),
tenantCode = appSettings.TenantCode,
poiId = appSettings.PoiId,
})
.SetContentType("application/json")
.PostAsAsync();
var model = JsonConvert.DeserializeObject(str);
if ((bool)model["success"])
{
var list = model["data"];
for (int i = 0; i < list.Count(); i++)
{
var models = await _classTeacherService.GetInfo(list[i]["personSetId"].ToString());
if (models!=null)
{
list[i]["userId"]= models.UserId;
list[i]["userName"]= models.Name;
list[i]["id"]= models.Id;
}
else
{
list[i]["userId"] = null;
list[i]["userName"] =null;
}
}
return list;
}
throw Oops.Oh(model["message"].ToString());
}
///
/// 分页获取班级列表
///
///
///
public async Task GetPageList(PersonSetInfoSearch search)
{
return await _personSetInfoService.GetList(search);
}
///
/// 删除底库
///
/// 底库id
///
public async Task DeleteDfieldD(string id)
{
var list = new List();
if (id.Contains(","))
{
list = id.Split(",").ToList();
}
else
{
list.Add(id);
}
foreach (var ids in list)
{
var appSettings = App.GetOptionsMonitor();
var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/delete"
.SetBody(new
{
token = _deepelephManager.GetToken(),
tenantCode = appSettings.TenantCode,
poiId = appSettings.PoiId,
personSetId = ids
})
.SetContentType("application/json")
.PostAsAsync();
var model = JsonConvert.DeserializeObject(str);
if ((bool)model["success"])
{
var isOk = model["data"].ToString() == ids;
await _personSetInfoService.Delete(ids);
}
else if (model["message"].ToString().Contains("不存在"))
{
await _personSetInfoService.Delete(ids);
}
else
{
throw Oops.Oh(model["message"].ToString());
}
}
return true;
}
///
/// 更新底库
///
///
///
public async Task UpdateDfieldU(DfieldInput input)
{
if (await _personSetInfoService.CheckName(input.Name, input.Id))
{
throw Oops.Oh("【" + input.Name + "】班级名称已存在");
}
var appSettings = App.GetOptionsMonitor();
var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/update"
.SetBody(new
{
token = _deepelephManager.GetToken(),
tenantCode = appSettings.TenantCode,
poiId = appSettings.PoiId,
personSetId=input.Id,
personSetName=input.Name
})
.SetContentType("application/json")
.PostAsAsync();
var model = JsonConvert.DeserializeObject(str);
if ((bool)model["success"])
{
var isOk = model["data"].ToString() == input.Id;
if (isOk)
{
await _personSetInfoService.Update(new PersonSetInfoDto
{
PersonSetId = input.Id,
PersonSetName = input.Name,
MajorId = input.MajorId
});
}
return isOk;
}
throw Oops.Oh(model["message"].ToString());
}
///
/// 同步班级数据
///
///
public async Task SyncData()
{
var appSettings = App.GetOptionsMonitor();
var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/queryAll"
.SetBody(new
{
token = _deepelephManager.GetToken(),
tenantCode = appSettings.TenantCode,
poiId = appSettings.PoiId,
})
.SetContentType("application/json")
.PostAsAsync();
var model = JsonConvert.DeserializeObject(str);
if ((bool)model["success"])
{
var list = model["data"];
foreach (var item in list)
{
await _personSetInfoService.Add(new PersonSetInfoDto
{
PersonSetId = item["personSetId"].ToString(),
PersonSetName = item["personSetName"].ToString(),
});
}
}
return true;
}
}