You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
318 lines
13 KiB
318 lines
13 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Yitter.IdGenerator;
|
|
using Znyc.Admin.Commons.Cache;
|
|
using Znyc.Admin.Commons.Const;
|
|
using Znyc.Admin.Commons.Entitys;
|
|
using Znyc.Admin.Commons.Enums;
|
|
using Znyc.Admin.Commons.Extensions;
|
|
using Znyc.Admin.Commons.Mapping;
|
|
using Znyc.Admin.Commons.Pages;
|
|
using Znyc.Admin.Commons.Services;
|
|
using Znyc.Admin.MongoDb.Core.Collection;
|
|
using Znyc.Admin.MongoDb.Core.IRepositorys;
|
|
using Znyc.Admin.Security.Dtos;
|
|
using Znyc.Admin.Security.Entitys;
|
|
using Znyc.Admin.Security.IRepositories;
|
|
using Znyc.Admin.Security.IServices;
|
|
|
|
namespace Znyc.Admin.Security.Services
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class VehicleService : BaseService<Vehicle, VehicleOutputDto, long>, IVehicleService
|
|
|
|
{
|
|
private readonly IVehicleRepository _vehicleRepository;
|
|
private readonly IGpsRealTimeRepository _gpsRealTimeRepository;
|
|
private readonly ICompanyRepository _companyRepository;
|
|
private readonly IVehicleGroupRepository _vehicleGroupRepository;
|
|
private readonly ICacheService _cacheService;
|
|
public VehicleService(IVehicleRepository repository,
|
|
IGpsRealTimeRepository gpsRealTimeRepository,
|
|
ICompanyRepository companyRepository,
|
|
IVehicleGroupRepository vehicleGroupRepository,
|
|
ICacheService cacheService
|
|
) : base(repository)
|
|
{
|
|
_vehicleRepository = repository;
|
|
_gpsRealTimeRepository = gpsRealTimeRepository;
|
|
_companyRepository = companyRepository;
|
|
_vehicleGroupRepository = vehicleGroupRepository;
|
|
_cacheService = cacheService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ·ÖÒ³²éѯ³µÁ¾ÐÅÏ¢
|
|
/// </summary>
|
|
/// <param name="search"></param>
|
|
/// <returns></returns>
|
|
public async Task<PageResult<VehicleOutputDto>> FindWithPagerSearchAsync(SearchVehicleModel search)
|
|
{
|
|
bool order = search.Order == "asc" ? false : true;
|
|
string where = GetDataPrivilege(false);
|
|
if (!string.IsNullOrEmpty(search.Keywords))
|
|
{
|
|
@where += string.Format(" and (VehicleCode like '%{0}%' or SimNo like '%{0}%' or VehiclePlate like '%{0}%' or ImeiNo like '%{0}%' )", search.Keywords);
|
|
}
|
|
if (search.CompanyId > 0)
|
|
{
|
|
@where += string.Format(" and CompanyId={0}", search.CompanyId);
|
|
}
|
|
if (search.Status > (int)CommonStatus.DELETED)
|
|
{
|
|
@where += string.Format(" and Status={0}", search.Status);
|
|
}
|
|
if (!string.IsNullOrEmpty(search.StartTime))
|
|
{
|
|
@where += " and CreatedTime >='" + search.StartTime + " 00:00:00'";
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(search.EndTime))
|
|
{
|
|
@where += " and CreatedTime <='" + search.EndTime + " 23:59:59'";
|
|
}
|
|
|
|
PagerInfo pagerInfo = new PagerInfo
|
|
{
|
|
CurrenetPageIndex = search.CurrenetPageIndex,
|
|
PageSize = search.PageSize
|
|
};
|
|
List<Vehicle> list = await repository.FindWithPagerAsync(where, pagerInfo, "ImeiNo", order);
|
|
List<VehicleOutputDto> vehicleOutputDto = list.MapTo<VehicleOutputDto>();
|
|
foreach (var item in vehicleOutputDto)
|
|
{
|
|
item.TerminalTypeName = EnumExtensions.GetDescription(typeof(TerminalTypeEnum), (int)TerminalTypeEnum.BSJKGM_08) ?? CommonConst.Unknown_TerminalType;
|
|
item.CardTypeName = EnumExtensions.GetDescription(typeof(CardTypeEnum), (int)CardTypeEnum.Beijing_Mobile) ?? CommonConst.Unknown_CardType;
|
|
}
|
|
PageResult<VehicleOutputDto> pageResult = new PageResult<VehicleOutputDto>
|
|
{
|
|
CurrentPage = pagerInfo.CurrenetPageIndex,
|
|
Items = vehicleOutputDto,
|
|
ItemsPerPage = pagerInfo.PageSize,
|
|
TotalItems = pagerInfo.RecordCount
|
|
};
|
|
return pageResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ÐÂÔö³µÁ¾ÐÅÏ¢
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public async Task<CommonResult> InsertAsync(Vehicle entity)
|
|
{
|
|
CommonResult result = new CommonResult();
|
|
//1.ÐÂÔö³µÁ¾
|
|
//2.³õʼ»¯gpsÐÅÏ¢
|
|
VehicleGroup vehicleGroup = await _vehicleGroupRepository.GetWhereAsync(" CompanyId=" + entity.CompanyId);
|
|
string where = string.Format(@"(CompanyId={0} and VehicleCode='{1}') or (VehiclePlate='{2}' AND Status=1)", entity.CompanyId, entity.VehicleCode, entity.VehiclePlate);
|
|
Vehicle car = await _vehicleRepository.GetWhereAsync(where);
|
|
if (car != null)
|
|
{
|
|
result.Success = false;
|
|
result.ErrMsg = ReturnConst.Vehicle_Repeat;
|
|
return result;
|
|
}
|
|
string key = string.Format(@" (SimNo='{0}' or ImeiNo='{1}') AND Status=1", entity.SimNo, entity.ImeiNo);
|
|
Vehicle repeatVehicle = await _vehicleRepository.GetWhereAsync(key);
|
|
if (repeatVehicle != null)
|
|
{
|
|
result.Success = false;
|
|
result.ErrMsg = string.Format(ReturnConst.Vehicle_Equipment_Repeat, repeatVehicle.VehiclePlate);
|
|
return result;
|
|
}
|
|
entity.Id = YitIdHelper.NextId();
|
|
entity.VehicleType = 1;
|
|
entity.VehicleGroup = vehicleGroup.Id;
|
|
entity.TerminalNo = entity.ImeiNo;
|
|
entity.AgreementNo = entity.AgreementNo ?? "";
|
|
entity.DriverPhone = "";
|
|
entity.VehicleDriver = 0;
|
|
entity.ContactPerson = "";
|
|
entity.ContactPhone = "";
|
|
entity.Acc = 0;
|
|
entity.Work = 0;
|
|
entity.GpsState = 0;
|
|
entity.IsActivate = true;
|
|
entity.IsGps = true;
|
|
entity.Status = (int)CommonStatus.ENABLE;
|
|
entity.GpsTime = new DateTime(1990, 1, 1);
|
|
entity.IsOverspeedAlarm = false;
|
|
entity.Overspeed = CommonConst.DEFAULT_OVERSPEED;
|
|
entity.Sort = entity.Sort;
|
|
await _vehicleRepository.InsertAsync(entity);
|
|
Vehicle vehicle = await _vehicleRepository.GetWhereAsync(" Id=" + entity.Id);
|
|
Company company = await _companyRepository.GetWhereAsync(" Id=" + entity.CompanyId);
|
|
GpsRealTime gpsRealTime = new GpsRealTime
|
|
{
|
|
VehicleId = vehicle.Id,
|
|
VehicleCode = vehicle.VehicleCode,
|
|
VehiclePlate = vehicle.VehiclePlate,
|
|
VehicleDriver = vehicle.VehicleDriver,
|
|
DriverPhone = vehicle.DriverPhone,
|
|
Status = (int)CommonStatus.ENABLE,
|
|
GpsState = 0,
|
|
GpsTime = Convert.ToDateTime("1900-01-01 8:00:00.000"), //new DateTime(1990, 1, 1),
|
|
RecTime = Convert.ToDateTime("1900-01-01 8:00:00.000"), //new DateTime(1990, 1, 1),
|
|
Longitude = company.Longitude,
|
|
Latitude = company.Latitude,
|
|
Direct = 0,
|
|
Speed = 0,
|
|
Acc = 0,
|
|
Address = "",
|
|
DurationTime = "",
|
|
AccDurationTime = "",
|
|
SimNo = vehicle.SimNo,
|
|
CompanyId = company.Id,
|
|
TerminalNo = vehicle.TerminalNo,
|
|
ModifiedTime = DateTime.Now,
|
|
CreatedTime = DateTime.Now
|
|
};
|
|
await _gpsRealTimeRepository.InsertGpsRealTime(gpsRealTime);
|
|
result.Success = true;
|
|
//Çå³ý³µÁ¾ÁÐ±í»º´æ
|
|
await _cacheService.RemoveAsync(CacheKey.CACHE_KEY_VehicleByCompanyId + $"{company.Id}");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ¸üгµÁ¾ÐÅÏ¢
|
|
/// </summary>
|
|
/// <param name="tinfo"></param>
|
|
/// <returns></returns>
|
|
public async Task<CommonResult> UpdateAsync(Vehicle tinfo)
|
|
{
|
|
CommonResult result = new CommonResult();
|
|
Vehicle info = await repository.GetAsync(tinfo.Id);
|
|
GpsRealTime gpsRealTime = await _gpsRealTimeRepository.GetGpsRealTime(info.Id);
|
|
if (!(info?.Id > 0) || gpsRealTime == null)
|
|
{
|
|
result.Success = false;
|
|
result.ErrMsg = ReturnConst.Vehicle_Not_Exist;
|
|
return result;
|
|
}
|
|
string where = string.Format(@" (SimNo='{0}' or ImeiNo='{1}') AND Status=1 AND Id !={2}", tinfo.SimNo, tinfo.ImeiNo, tinfo.Id);
|
|
Vehicle repeatVehicle = await _vehicleRepository.GetWhereAsync(where);
|
|
if (repeatVehicle != null)
|
|
{
|
|
result.Success = false;
|
|
result.ErrMsg = string.Format(ReturnConst.Vehicle_Equipment_Repeat, repeatVehicle.VehiclePlate);
|
|
return result;
|
|
}
|
|
info.VehicleCode = tinfo.VehicleCode;
|
|
info.VehiclePlate = tinfo.VehiclePlate;
|
|
info.CardType = tinfo.CardType;
|
|
info.TerminalType = tinfo.TerminalType;
|
|
info.SimNo = tinfo.SimNo;
|
|
info.ImeiNo = tinfo.ImeiNo;
|
|
info.TerminalNo = tinfo.ImeiNo;
|
|
info.OpenTime = tinfo.OpenTime;
|
|
info.ExpireTime = tinfo.ExpireTime;
|
|
info.ModifiedUserId = tinfo.ModifiedUserId;
|
|
info.ModifiedTime = tinfo.ModifiedTime;
|
|
info.Sort = tinfo.Sort;
|
|
await repository.UpdateAsync(info, tinfo.Id).ConfigureAwait(false);
|
|
gpsRealTime.VehicleCode = tinfo.VehicleCode;
|
|
gpsRealTime.VehiclePlate = tinfo.VehiclePlate;
|
|
gpsRealTime.SimNo = tinfo.SimNo;
|
|
gpsRealTime.TerminalNo = info.TerminalNo;
|
|
gpsRealTime.Status = info.Status;
|
|
|
|
result.Success = await _gpsRealTimeRepository.UpdateGpsRealTime(gpsRealTime);
|
|
await ClearVelicleCacheAsync(info.Id, info.CompanyId);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ð޸ijµÁ¾×´Ì¬
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="status">״̬</param>
|
|
/// <returns></returns>
|
|
public async Task<CommonResult> UpdateStatusAsync(long id, int status)
|
|
{
|
|
CommonResult result = new CommonResult();
|
|
Vehicle info = await repository.GetAsync(id);
|
|
GpsRealTime gpsRealTime = await _gpsRealTimeRepository.GetGpsRealTime(id);
|
|
if (!(info?.Id > 0) || gpsRealTime == null)
|
|
{
|
|
result.Success = false;
|
|
result.ErrMsg = ReturnConst.Vehicle_Not_Exist;
|
|
return result;
|
|
}
|
|
info.Status = status;
|
|
gpsRealTime.Status = status;
|
|
await repository.UpdateAsync(info, info.Id).ConfigureAwait(false);
|
|
result.Success = await _gpsRealTimeRepository.UpdateGpsRealTime(gpsRealTime);
|
|
|
|
await ClearVelicleCacheAsync(info.Id, info.CompanyId);
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ð޸ijµÁ¾ÊÇ·ñ¼¤»î
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="isActivate">״̬</param>
|
|
/// <returns></returns>
|
|
public async Task<CommonResult> UpdateIsActivateAsync(long id, bool isActivate)
|
|
{
|
|
CommonResult result = new CommonResult();
|
|
Vehicle info = await repository.GetAsync(id);
|
|
if (!(info?.Id > 0))
|
|
{
|
|
result.Success = false;
|
|
result.ErrMsg = ReturnConst.Vehicle_Not_Exist;
|
|
return result;
|
|
}
|
|
info.IsActivate = isActivate;
|
|
await repository.UpdateAsync(info, info.Id).ConfigureAwait(false);
|
|
await ClearVelicleCacheAsync(info.Id, info.CompanyId);
|
|
|
|
result.Success = true;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ð޸ijµÁ¾ÊÇ·ñ¿ªÆôGPS
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="isGps">״̬</param>
|
|
/// <returns></returns>
|
|
public async Task<CommonResult> UpdateIsGpsAsync(long id, bool isGps)
|
|
{
|
|
CommonResult result = new CommonResult();
|
|
Vehicle info = await repository.GetAsync(id);
|
|
if (!(info?.Id > 0))
|
|
{
|
|
result.Success = false;
|
|
result.ErrMsg = ReturnConst.Vehicle_Not_Exist;
|
|
return result;
|
|
}
|
|
info.IsGps = isGps;
|
|
await repository.UpdateAsync(info, info.Id).ConfigureAwait(false);
|
|
result.Success = true;
|
|
await ClearVelicleCacheAsync(info.Id, info.CompanyId);
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="velicId"></param>
|
|
/// <param name="companyId"></param>
|
|
/// <returns></returns>
|
|
public async Task ClearVelicleCacheAsync(long velicId, long companyId)
|
|
{
|
|
//Çå³ý»º´æ
|
|
await _cacheService.RemoveAsync(CacheKey.CACHE_KEY_VehicleByCompanyId + $"{companyId}");
|
|
await _cacheService.RemoveAsync(CacheKey.CACHE_KEY_Vehicle + $"{velicId}");
|
|
}
|
|
}
|
|
}
|