using AutoMapper;
using DTO;
using IServices;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using zhengcaioa.Models;
namespace Services
{
public class AreaService: IAreaService
{
private readonly zhengcaioaContext _context;
private readonly IMapper _mapper;
public AreaService(zhengcaioaContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public ResultEntity save(AreaDTO dto)
{
ResultEntity resultEntity = new ResultEntity();
try
{
var Area = _mapper.Map(dto);
if (String.IsNullOrEmpty(Area.CodeId))
{
// Area.CodeId = Guid.NewGuid().ToString();
_context.Areas.Add(Area);
}
else
{
var updatepltRole = _context.Areas.Find(Area.CodeId);
if (updatepltRole != null)
{
updatepltRole.Name = Area.Name;
updatepltRole.ParentId = Area.ParentId;
}
else
{
// Area.CodeId = Guid.NewGuid().ToString();
_context.Areas.Add(Area);
}
}
_context.SaveChanges();
resultEntity.ReturnID = Area.CodeId;
resultEntity.Result = true;
}
catch (Exception ex)
{
resultEntity.Result = false;
resultEntity.Message = "保存失败,请联系管理员";
}
return resultEntity;
}
public AreaDTO Get(string id)
{
var entity = _context.Areas.Find(id);
if (entity == null)
{
entity = new Area();
}
var AreaDTO = _mapper.Map(entity);
return AreaDTO;
}
public ResultDataEntity SearchByPaging(AreaDTOSearch searchEntity)
{
ResultDataEntity data = new ResultDataEntity();
var query = (from a in _context.Areas//.Where(x => x.RecStatus == "A")
join b in _context.Areas
on a.ParentId equals b.CodeId
into bsss
from bbb in bsss.DefaultIfEmpty()
where 1==1
&& (string.IsNullOrWhiteSpace(searchEntity.Name) || a.Name.Contains(searchEntity.Name.Trim()))
&& (string.IsNullOrWhiteSpace(searchEntity.ParentName) || bbb.Name.Contains(searchEntity.ParentName.Trim()))
select new AreaDTO
{
CodeId = a.CodeId,
Name = a.Name,
ParentName = bbb.Name,
ParentId = a.ParentId,
}).OrderBy(x => x.CodeId).ToList();
//if (searchEntity.totalrows == 0)
searchEntity.totalrows = query.Count();
var rolelist = query.Skip((searchEntity.page - 1) * searchEntity.rows).Take(searchEntity.rows).ToList();
data.LoadData(searchEntity, rolelist);
return data;
}
public ResultEntity ModifyStatus(string id, string userid)
{
ResultEntity result = new ResultEntity();
result.Result = true;
var model = _context.Areas.Find(id);
if (model != null)
{
_context.Areas.Remove(model);
_context.SaveChanges();
}
return result;
}
///
/// 获取所有有效科目
///
///
public List GetList()
{
var listPosition = _context.Areas.ToList();
var list = _mapper.Map>(listPosition);
return list;
}
}
}