using AutoMapper;
using LinqKit;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.SearchModel;
using Ropin.Inspection.Model.ViewModel;
using Ropin.Inspection.Repository.Interface;
using Ropin.Inspection.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace Ropin.Inspection.Service
{
///
/// 巡检内容
///
public class TispContentService : ITispContentService
{
private readonly ITispContentRepository _repository;
private readonly IMapper _mapper;
private readonly IClaimsAccessor _claims;
public TispContentService(IClaimsAccessor claims, ITispContentRepository repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
_claims = claims;
}
public async Task CreateOneAsync(TispContentViewModel viewModel)
{
var content = _mapper.Map(viewModel);
content.C_ID = Guid.NewGuid();
content.C_CreateBy = _claims.ApiUserId;
content.D_CreateOn = DateTime.Now;
content.C_Status = "1";
_repository.Create(content);
var result = await _repository.SaveAsync();
if (!result)
{
throw new Exception("创建失败");
}
}
public async Task DeleteAsync(Guid id)
{
var content = await _repository.GetByIdAsync(id);
if (content == null)
{
throw new Exception("没有此巡检内容");
}
//_repository.Delete(content);
content.C_LastUpdatedBy = _claims.ApiUserId;
content.D_LastUpdatedOn = DateTime.Now;
content.C_Status = "0";
_repository.Update(content);
var result = await _repository.SaveAsync();
if (!result)
{
throw new Exception("删除失败");
}
}
public async Task> GetAllAsync(string storeCode)
{
var pagedList = await _repository.GetAllAsync();
var contentDtoList = _mapper.Map>(pagedList.Where(i=>i.C_StoreCode == storeCode));
return contentDtoList.ToList();
}
public async Task> GetContentsConditionAsync(TispContentSearchModel searchModel)
{
var predicate = PredicateBuilder.New(true);//查询条件,推荐后台使用这种方式灵活筛选
#region 添加条件查询
predicate = predicate.And(i => i.C_StoreCode.Equals(searchModel.C_StoreCode));
if (!string.IsNullOrEmpty(searchModel.C_Status))
{
predicate = predicate.And(i => i.C_Status.Contains(searchModel.C_Status));
}
if (!string.IsNullOrEmpty(searchModel.C_Name))
{
predicate = predicate.And(i => i.C_Name.Contains(searchModel.C_Name));
}
#endregion
var list = await _repository.GetPageAsync(predicate, "I_Sort", searchModel.IsPagination, searchModel.PageIndex, searchModel.PageSize);//I_Sort,D_CreateOn
searchModel.TotalCount = list.Totals;
var dtoList = _mapper.Map, List>(list.Rows);
return dtoList;
}
public Task> GetByConditionAsync(Expression> expression)
{
throw new NotImplementedException();
}
public async Task GetByIdAsync(Guid id)
{
var content = await _repository.GetByIdAsync(id);
var contentDto = _mapper.Map(content);
return contentDto;
}
public Task IsExistAsync(Guid id)
{
throw new NotImplementedException();
}
public async Task UpdateAsync(Guid id, TispContentUpdateViewModel updateModel)
{
var content = await _repository.GetByIdAsync(id);
if (content == null)
{
throw new Exception("没有此巡检点");
}
_mapper.Map(updateModel, content, typeof(TispContentUpdateViewModel), typeof(TISP_Content));
content.C_LastUpdatedBy = _claims.ApiUserId;
content.D_LastUpdatedOn = DateTime.Now;
content.C_Status = updateModel.C_Status;
_repository.Update(content);
var result = await _repository.SaveAsync();
if (!result)
{
throw new Exception("更新失败");
}
}
public Task UpdateOneAsync(TispContentViewModel viewModel, params string[] fields)
{
throw new NotImplementedException();
}
}
}