279 lines
12 KiB
C#
279 lines
12 KiB
C#
using AutoMapper;
|
|
using Complex.Application.Services.Basic;
|
|
using Complex.Application.Utility;
|
|
using Complex.Common.Dto;
|
|
using Complex.Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Complex.Application.Complex
|
|
{
|
|
public interface IUnitService
|
|
{
|
|
/// <summary>
|
|
/// ثبت توسط مدیر ساختمان
|
|
/// </summary>
|
|
/// <param name="unitDto"></param>
|
|
/// <returns></returns>
|
|
ResultDto<Guid> InsertUnit(string adminUserId, UnitDto unitDto);
|
|
/// <summary>
|
|
/// ویرایش توسط مدیر ساختمان
|
|
/// </summary>
|
|
/// <param name="unitDto"></param>
|
|
/// <returns></returns>
|
|
ResultDto<bool> AdminEditUnit(string adminUserId, UnitDto unitDto);
|
|
/// <summary>
|
|
/// ویرایش توسط مالک
|
|
/// </summary>
|
|
/// <param name="unitDto"></param>
|
|
/// <returns></returns>
|
|
ResultDto<bool> OwnerEditUnit(string UserId, UnitDto unitDto);
|
|
/// <summary>
|
|
/// ویرایش توسط مستاجر
|
|
/// </summary>
|
|
/// <param name="unitDto"></param>
|
|
/// <returns></returns>
|
|
ResultDto<bool> TenantEditUnit(string UserId, UnitDto unitDto);
|
|
ResultDto<List<UnitDto>> UnitList(string userId, UnitRequestList unitRequest);
|
|
}
|
|
public class UnitService : IUnitService
|
|
{
|
|
private readonly IComplexDBContext _complexDBContext;
|
|
private readonly CostService _costService;
|
|
private IMapper _mapper;
|
|
|
|
public UnitService(IComplexDBContext complexDBContext, IMapper mapper, CostService costService)
|
|
{
|
|
_complexDBContext = complexDBContext;
|
|
_costService = costService;
|
|
_mapper = mapper;
|
|
}
|
|
public ResultDto<List<UnitDto>> UnitList(string userId, UnitRequestList unitRequest)
|
|
{
|
|
var today = PersianDate.GetToday();
|
|
var IsUserInComplex = _complexDBContext.Units.Where(u => u.ComplexId == unitRequest.ComplexId
|
|
&& (u.OwnerId == userId || u.TenantId == userId))
|
|
.Select(a => a.ComplexId)
|
|
.Union(_complexDBContext.ComplexManagers.Where(cm => cm.ComplexId == unitRequest.ComplexId
|
|
&& string.Compare(cm.EndDate, today) >= 0
|
|
&& string.Compare(cm.StartDate, today) <= 0)
|
|
.Where(u => u.ManagerMobileNumberId == userId)
|
|
.Select(u => u.ComplexId));
|
|
|
|
if (IsUserInComplex.Count() == 0)
|
|
return new ResultDto<List<UnitDto>>
|
|
{
|
|
Data = null,
|
|
IsSuccess = false,
|
|
Message = "درخواست نامعتبر"
|
|
};
|
|
|
|
var units = from u in _complexDBContext.Units.Include(u => u.Owner).Include(u => u.Tenant)
|
|
.Include(u => u.State)
|
|
where u.ComplexId == unitRequest.ComplexId
|
|
select u;
|
|
if (unitRequest.Id.HasValue)
|
|
units = units.Where(u => u.Id == unitRequest.Id);
|
|
|
|
if (unitRequest.OwnerId.IsNotNullOrEmpty())
|
|
units = units.Where(u => u.OwnerId == unitRequest.OwnerId);
|
|
if (unitRequest.TenantId.IsNotNullOrEmpty())
|
|
units = units.Where(u => u.TenantId == unitRequest.TenantId);
|
|
|
|
var unitObjs = units.ToList().Select(u => new UnitDto
|
|
{
|
|
ComplexId = u.ComplexId,
|
|
UnitName = u.UnitName,
|
|
Id = u.Id,
|
|
AreaMeter = u.AreaMeter,
|
|
TenantId = u.TenantId,
|
|
EmergencyTels = u.EmergencyTels,
|
|
Floor = u.Floor,
|
|
InsertTime = Convert.ToDateTime(_complexDBContext.Entry(u).Property("InsertTime").CurrentValue),
|
|
UpdateTime = Convert.ToDateTime(_complexDBContext.Entry(u).Property("UpdateTime").CurrentValue),
|
|
//InsertTime = u.InsertTime,
|
|
IsActive = u.IsActive,
|
|
ManagerDescription = u.ManagerDescription,
|
|
ParkingAreaMeter = u.ParkingAreaMeter,
|
|
PublicState = u.PublicState,
|
|
StateTitle = u.StateId.HasValue ? u.State.Title : "",
|
|
StateId = u.StateId,
|
|
YardMeter = u.YardMeter,
|
|
TenantNameFamily = u.TenantId != null ? u.Tenant.FirstName + " " + u.Tenant.LastName : "",
|
|
OwnerNameFamily = u.OwnerId != null ? u.Owner.FirstName + " " + u.Owner.LastName : "",
|
|
OwnerId = u.OwnerId,
|
|
});
|
|
try
|
|
{
|
|
return new ResultDto<List<UnitDto>>
|
|
{
|
|
Data = unitObjs.ToList(),
|
|
IsSuccess = true,
|
|
//RowCount =
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ResultDto<List<UnitDto>>
|
|
{
|
|
Message = ex.InnerException != null ? ex.InnerException.Message + Environment.NewLine + ex.InnerException.Data :
|
|
ex.Message + Environment.NewLine + ex.Data,
|
|
IsSuccess = false
|
|
};
|
|
}
|
|
}
|
|
|
|
public ResultDto<Guid> InsertUnit(string adminUserId, UnitDto unitDto)
|
|
{
|
|
var unit = _mapper.Map<Unit>(unitDto);
|
|
var complexManager = _complexDBContext.ComplexManagers.Where(c => c.ComplexId == unit.ComplexId)
|
|
.Select(cm => cm.ManagerMobileNumberId).ToList();
|
|
if (!complexManager.Any(m => m == adminUserId))
|
|
return new ResultDto<Guid>
|
|
{
|
|
Message = "عدم دسترسی",
|
|
IsSuccess = false
|
|
};
|
|
//unit.InsertTime = DateTime.Now;
|
|
_complexDBContext.Units.Add(unit);
|
|
_complexDBContext.SaveChanges();
|
|
//محاسبه شارژهای اون یونیت
|
|
|
|
var nowDate = DateOnly.FromDateTime(DateTime.Now);
|
|
|
|
//وقتی واحد تازه ثبت شده پس در مدیریت شارژ قابلیت ثبت اختصاصی برای خودش نداشته پس باید شارژ عمومی برایش تعریف شود
|
|
// اگر خواست میتواند برایش به صورت اختصاصی هم ثبت کند که اون موقع برایش محاسبه مجدد خواهد شد
|
|
var costCycleDto = _costService.CostCyclsList(unit.ComplexId, nowDate, nowDate, null).Data
|
|
.FirstOrDefault(c => c.UnitIds == null);
|
|
if (costCycleDto != null)
|
|
{
|
|
//var costCycle = new CostCycle
|
|
//{
|
|
// Amount=costCycleDto.Amount,
|
|
|
|
//};
|
|
var costCycle=_mapper.Map<CostCycle>(costCycleDto);
|
|
_costService.DefineUnitCharge(unit.Id, costCycle, true);
|
|
}
|
|
//
|
|
return new ResultDto<Guid> { Data = unit.Id, IsSuccess = true, Message = "ثبت با موفقیت انجام شد" };
|
|
}
|
|
public ResultDto<bool> AdminEditUnit(string adminUserId, UnitDto unitDto)
|
|
{
|
|
var unit = _complexDBContext.Units.Include(u => u.Complex).ThenInclude(c => c.ComplexManagers).FirstOrDefault(u => u.Id == unitDto.Id);
|
|
if (unit == null)
|
|
return new ResultDto<bool> { Message = "مشکل در یافتن واحد", IsSuccess = false };
|
|
|
|
var today = PersianDate.GetToday();
|
|
var complex = unit.Complex;
|
|
if (complex.ComplexManagers == null || !complex.ComplexManagers.Any(m => m.ManagerMobileNumberId == adminUserId
|
|
&& string.Compare(m.EndDate, today) >= 0))
|
|
return new ResultDto<bool> { Message = "عدم دسترسی", IsSuccess = false };
|
|
|
|
|
|
var oldUnitState = unit.PublicState;
|
|
unit = _mapper.Map<Unit>(unitDto);
|
|
unit.PublicState = oldUnitState;
|
|
_complexDBContext.SaveChanges();
|
|
|
|
|
|
//oldPayment را باید چه کار کرد؟؟؟؟!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
return new ResultDto<bool> { Message = "ویرایش با موفقیت انجام شد", IsSuccess = true };
|
|
}
|
|
public ResultDto<bool> OwnerEditUnit(string UserId, UnitDto unitDto)
|
|
{
|
|
var unit = _complexDBContext.Units.FirstOrDefault(u => u.Id == unitDto.Id);
|
|
if (unit == null)
|
|
return new ResultDto<bool> { Message = "مشکل در یافتن واحد", IsSuccess = false };
|
|
if (unit.OwnerId != UserId)
|
|
return new ResultDto<bool> { Message = "عدم دسترسی", IsSuccess = false };
|
|
|
|
unit.AreaMeter = unitDto.AreaMeter;
|
|
unit.YardMeter = unitDto.YardMeter;
|
|
unit.ParkingAreaMeter = unitDto.ParkingAreaMeter;
|
|
unit.EmergencyTels = unitDto.EmergencyTels;
|
|
unit.Floor = unitDto.Floor;
|
|
unit.ParkingAreaMeter = unitDto.ParkingAreaMeter;
|
|
unit.PublicState = unitDto.PublicState;
|
|
unit.YardMeter = unitDto.YardMeter;
|
|
|
|
|
|
_complexDBContext.SaveChanges();
|
|
return new ResultDto<bool> { Message = "ویرایش با موفقیت انجام شد", IsSuccess = true };
|
|
}
|
|
public ResultDto<bool> TenantEditUnit(string UserId, UnitDto unitDto)
|
|
{
|
|
var unit = _complexDBContext.Units.FirstOrDefault(u => u.Id == unitDto.Id);
|
|
if (unit == null)
|
|
return new ResultDto<bool> { Message = "مشکل در یافتن واحد", IsSuccess = false };
|
|
if (unit.TenantId != UserId)
|
|
return new ResultDto<bool> { Message = "عدم دسترسی", IsSuccess = false };
|
|
|
|
unit.PublicState = unitDto.PublicState;
|
|
|
|
_complexDBContext.SaveChanges();
|
|
return new ResultDto<bool> { Message = "ویرایش با موفقیت انجام شد", IsSuccess = true };
|
|
}
|
|
}
|
|
|
|
public class UnitRequestList
|
|
{
|
|
public Guid? Id { set; get; }
|
|
public Guid ComplexId { set; get; }
|
|
public string? OwnerId { set; get; }
|
|
public string? TenantId { set; get; }
|
|
}
|
|
|
|
public class UnitDto
|
|
{
|
|
public Guid Id { get; set; }
|
|
public Guid ComplexId { set; get; }
|
|
public string UnitName { set; get; }
|
|
public ComplexEntity Complex { set; get; }
|
|
/// <summary>
|
|
/// ساکن
|
|
/// </summary>
|
|
public string? OwnerId { set; get; }
|
|
/// <summary>
|
|
/// شماره مستاجر
|
|
/// </summary>
|
|
public string? TenantId { set; get; }
|
|
public bool IsActive { set; get; }
|
|
/// <summary>
|
|
/// متراژ کل
|
|
/// </summary>
|
|
public int? AreaMeter { set; get; }
|
|
public string? EmergencyTels { set; get; }
|
|
public int? ParkingAreaMeter { set; get; }
|
|
/// <summary>
|
|
/// متراژ حیاط
|
|
/// </summary>
|
|
public int? YardMeter { set; get; }
|
|
public string? PublicState { set; get; }
|
|
public int? StateId { set; get; }
|
|
public string StateTitle { set; get; }
|
|
public int? Floor { set; get; }
|
|
//public string? UnitId { set; get; }
|
|
public string? ManagerDescription { set; get; }
|
|
//-----------------------------------------
|
|
|
|
/// <summary>
|
|
/// نام و نام خانوادگی مالک
|
|
/// </summary>
|
|
public string OwnerNameFamily { set; get; }
|
|
/// <summary>
|
|
/// نام و نام خانوادگی مستاجر
|
|
/// </summary>
|
|
public string TenantNameFamily { set; get; }
|
|
//public string StateByManager { set; get; }
|
|
public DateTime? InsertTime { get; set; }
|
|
public DateTime? UpdateTime { get; set; }
|
|
}
|
|
}
|