|
- using System;
- using System.Linq;
- using System.Net.Cache;
- using Microsoft.EntityFrameworkCore;
- using Permission.Entity.DbContext;
- using Permission.Entity.News;
- using Permission.Infrastructure.Repositories;
- using Permission.Infrastructure.WebControls;
- using Permission.Service.IServices;
- using Permission.Utils.Validate;
-
- namespace Permission.Service.Services
- {
- public class NewsInfoService : INewsInfoService
- {
- private readonly IUnitOfWork _unitOfWork;
- private readonly PermissionContext _dbContext;
- private readonly IBaseRepository<NewsInfo> _newsInfoRepository;
-
- public NewsInfoService(IUnitOfWork unitOfWork, PermissionContext dbContext, IBaseRepository<NewsInfo> newsInfoRepository)
- {
- this._unitOfWork = unitOfWork;
- this._dbContext = dbContext;
- this._newsInfoRepository = newsInfoRepository;
- }
- public bool AddModel(NewsInfo model)
- {
- try
- {
- model.GuidId = Guid.NewGuid().ToString();
- model.IsEnabled = model.IsEnabled != null;
- model.DeleteMark = false;
- model.CreateTime = DateTime.Now;
- var res = _newsInfoRepository.Save(model);
- return res;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 编辑内容
- /// </summary>
- /// <param name="model"></param>
- /// <returns></returns>
- public bool ModifyModel(NewsInfo model)
- {
- try
- {
- var newinfo = _newsInfoRepository.Get(model.Id);
- newinfo.SortCode = model.SortCode;
- newinfo.InfoLevel = model.InfoLevel;
- newinfo.NewsTitle = model.NewsTitle;
- newinfo.SubTitle = model.SubTitle;
- newinfo.PushContent = model.PushContent;
- newinfo.ModifyTime = model.ModifyTime;
- var res = _newsInfoRepository.Update(newinfo);
- return res;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- /// <summary>
- /// Modifies the model.
- /// </summary>
- /// <param name="newId">The new identifier.</param>
- /// <param name="op">The op.</param>
- /// <returns></returns>
- public bool ModifyModel(int newId, int op)
- {
- try
- {
- var newinfo = _newsInfoRepository.Get(newId);
- newinfo.Status = op == 1 ? 1 : 0;
- newinfo.ModifyTime = DateTime.Now;
- var res = _newsInfoRepository.Update(newinfo);
- return res;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
-
- public bool DeleteModel(int key)
- {
- bool flag = false;
- try
- {
- if (!key.IsEmpty() || key > 0)
- {
- var model = _newsInfoRepository.Get(key);
- _newsInfoRepository.Delete(model);
- flag = true;
- }
- else
- {
- flag = false;
- }
- return flag;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- public NewsInfo GetNewsInfoByKey(int key)
- {
- try
- {
- if (!key.IsEmpty() || key > 0)
- {
- var data = _newsInfoRepository.Get(key);
- return data;
- }
- return null;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
-
-
- /// <summary>
- /// 条件分页查询
- /// </summary>
- /// <param name="typeid">新闻消息类型 -1表示查询所有</param>
- /// <param name="keyword"></param>
- /// <param name="pagesize"></param>
- /// <param name="pageindex"></param>
- /// <returns></returns>
- public Page<NewsInfo> PageList(int typeid, string keyword, int pagesize, int pageindex)
- {
- try
- {
- int total = 0;
- Page<NewsInfo> data;
- if (keyword.IsEmpty())
- {
- data = _newsInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(d => d.SortCode).ThenByDescending(ur => ur.CreateTime),
- u => u,
- out total,
- u => u.NewsTypeId == typeid && u.DeleteMark == false);
-
- return data;
- }
- data = _newsInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(d => d.SortCode).ThenByDescending(ur => ur.CreateTime),
- u => u,
- out total,
- u => u.NewsTitle.Contains(keyword) && u.NewsTypeId == typeid && u.DeleteMark == false);
-
- return data;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
-
- public Page<NewsInfo> NewsListPageList(int pagesize, int pageindex)
- {
- try
- {
- int total = 0;
- Page<NewsInfo> data;
- data = _newsInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(d => d.SortCode).ThenByDescending(ur => ur.CreateTime),
- u => u,
- out total,
- u => u.Status == 1 && u.DeleteMark == false);
-
- return data;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
- }
- }
|