using Ewide.Application.Entity;
using Ewide.Application.Service.HouseProjectInfo.Dto;
using Ewide.Core;
using Ewide.Core.Service;
using Ewide.Core.Util;
using Ewide.EntityFramework.Core;
using Furion;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Ewide.Application.Service.HouseProjectInfo
{
///
/// 项目管理相关服务
///
[ApiDescriptionSettings(Name = "HouseProjectInfo", Order = 170)]
public class HouseProjectInfoService : IHouseProjectInfoService, ITransient, IDynamicApiController
{
private readonly IRepository _houseProjectInfoRep;
public HouseProjectInfoService(IRepository houseProjectInfoRep)
{
_houseProjectInfoRep = houseProjectInfoRep;
}
///
/// 添加项目
///
///
///
[HttpPost("/houseProjectInfo/add")]
public async Task AddProject(AddProjectInput input)
{
await _houseProjectInfoRep.InsertNowAsync(input.Adapt());
}
///
/// 删除项目
///
///
///
[HttpPost("/houseProjectInfo/delete")]
public async Task DeleteProject(DeleteProjectInput input)
{
var project = _houseProjectInfoRep.FirstOrDefault(p => p.Id == input.Id);
await project.DeleteNowAsync();
}
///
/// 编辑项目
///
///
///
[HttpPost("/houseProjectInfo/edit")]
public async Task UpdateProject(UpdateProjectInput input)
{
var project = input.Adapt();
await project.UpdateExcludeAsync(new[] { nameof(BsHouseProjectInfo.AreaCode) }, true);
}
///
/// 通过ID获取项目
///
///
///
[HttpGet("/houseProjectInfo/detailById")]
public async Task GetProjectById([Required] string id)
{
return await _houseProjectInfoRep.DetachedEntities.FirstOrDefaultAsync(p => p.Id == id);
}
///
/// 分页查询
///
///
///
[HttpPost("/houseProjectInfo/page")]
public async Task QueryProjectPageList([FromBody] PageProjectInput input)
{
var areaCodeRep = Db.GetRepository();
var projects = await _houseProjectInfoRep.DetachedEntities
.Join(areaCodeRep.DetachedEntities, p => p.AreaCode, a => a.Code, (p, a) => new { p, AreaName = a.Name })
.Where(!string.IsNullOrEmpty(input.Name), x => x.p.Name.Contains(input.Name))
.Where(!string.IsNullOrEmpty(input.Note), x => x.p.Note.Contains(input.Note))
.Where(!string.IsNullOrEmpty(input.AreaCode), x => x.p.AreaCode == input.AreaCode)
.Where(!string.IsNullOrEmpty(input.pid) , x=> x.p.AreaCode.Contains(input.pid))
.Select(x => new { x.p.Id, x.p.Name, x.p.Note, x.p.Sort, x.p.AreaCode, x.AreaName, x.p.Type }.Adapt()).ToPagedListAsync(input.PageNo, input.PageSize);
return XnPageResult.PageResult(projects);
}
///
///
///
///
///
[HttpGet("/houseProjectInfo/detail")]
public async Task GetProject([FromQuery] QueryProjectInput input)
{
var user = await _houseProjectInfoRep.DetachedEntities.FirstOrDefaultAsync(p => p.Id == input.Id);
var userDto = user.Adapt();
return userDto;
}
[HttpGet("/houseProjectInfo/nextSort")]
public async Task GetNextProjectSortByAreaCode([FromQuery] ListHouseProjectInfoInput input)
{
//var projects = await _houseProjectInfoRep.DetachedEntities
// .Where(p => p.AreaCode == input.AreaCode && p.Type == input.Type)
// .Select(p => p.Sort)
// .DefaultIfEmpty()
// .MaxAsync();
if (input.Type > 2 || input.Type < 1)
{
throw Oops.Oh("类型参数异常");
}
var p = await _houseProjectInfoRep.DetachedEntities
.Where(p => p.AreaCode == input.AreaCode && p.Type == input.Type)
.MaxAsync(p => (int?)p.Sort);
return p.GetValueOrDefault(0) + 1;
}
///
/// 获取项目下拉列表
///
///
///
[HttpGet("houseProjectInfo/list")]
public async Task GetProjectList([FromQuery] ListHouseProjectInfoInput input)
{
return await _houseProjectInfoRep.DetachedEntities
.Where(p => p.AreaCode == input.AreaCode && p.Type == input.Type)
.OrderBy(p => p.Sort)
.ToListAsync();
}
}
}