using Furion.DependencyInjection; using Furion.DistributedIDGenerator; using Nirvana.Common; using Nirvana.Common.ApiBase; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using YBDevice.Application.AdInfo; using YBDevice.Entity; namespace YBDevice.Application { /// /// 资讯管理 /// public class InfoService :IInfoService, ITransient { private readonly ISqlSugarRepository repository; private readonly SqlSugarClient dbClient; private readonly OperatorModel currentUser; public InfoService(ISqlSugarRepository sqlSugarRepository) { repository = sqlSugarRepository; dbClient = repository.Context; currentUser = BaseInfoService.GetUserInfo(); } /// /// 资讯列表 /// /// /// public async Task> GetListAsync(QueryParams param) { RefAsync totalnum = 0; var temquery = dbClient.Queryable(); if (param.queryParam != null && param.queryParam.Count > 0) { List conModels = new List(); param.queryParam.ForEach(x => { if (!string.IsNullOrEmpty(x.Value)) { conModels.Add(new ConditionalModel() { FieldName = x.Name, ConditionalType = (ConditionalType)x.Type, FieldValue = x.Value.Trim() }); } }); if (conModels.Count > 0) { temquery = temquery.Where(conModels); } } if (currentUser.AccountType != AccountType.platform) { temquery = temquery.Where(x => x.BusienssId == currentUser.BusinessId); } string sorts = string.Format("{0} {1}", param.sort, param.order); var query = await temquery.OrderBy(sorts) .Select(x => new SecInfoListModel { Id = x.Id, Title = x.Title, HeadImg = x.HeadImg, CreateTime = x.CreateTime, TagId = x.TagId, Status = x.Status, ClickCount = x.ClickCount }) .Mapper((it, cache) => { var alltag = cache.Get(list => { var ids = list.Select(x => x.TagId).ToList(); return dbClient.Queryable().Where(x => ids.Contains(x.Id)).ToList(); }); it.tagname = alltag.FirstOrDefault(x => x.Id == it.TagId)?.Name; }) .ToPageListAsync(param.offset, param.limit, totalnum); return new PageParms { page = param.offset, Items = query, totalnum = totalnum, limit = param.limit }; } ///// ///// 迁移secinfo到新表中 ///// ///// //public async Task SecToNew() //{ // var allsec = await dbClient.Queryable().ToListAsync(); // var allcontent = await dbClient.Queryable().ToListAsync(); // List slist = new List(); // List alist = new List(); // List clist = new List(); // foreach (var item in allsec) // { // var secinfo = new YB_nSecInfo // { // Id = IDGen.NextID(), // Status = item.Status, // StatusRemark = item.StatusRemark, // BusienssId = item.BusienssId, // ClickCount = item.ClickCount, // HeadImg = item.HeadImg, // CreateTime = item.CreateTime, // TagId = item.TagId, // Title = item.Title, // Type = item.Type // }; // slist.Add(secinfo); // alist.Add(new YB_nSecInfoAppId // { // AppId = item.AppId, // InfoId = secinfo.Id // }); // var content = allcontent.FirstOrDefault(x => x.InfoId == item.Id); // string cont = content != null ? content.Content : ""; // clist.Add(new YB_nSecInfoContent // { // InfoId = secinfo.Id, // Content = cont // }); // } // await dbClient.Insertable(slist).ExecuteCommandAsync(); // await dbClient.Insertable(alist).ExecuteCommandAsync(); // await dbClient.Insertable(clist).ExecuteCommandAsync(); //} /// /// 信息提交 /// /// /// public async Task SubmitAsync(SecInfoSubmitModel model) { model.AppId = model.AppId.ToStr(); List appids = model.AppId.Split(',').ToList(); if (model.Id !=Guid.Empty) { await dbClient.Updateable().SetColumns(x => new YB_nSecInfo { HeadImg = model.HeadImg, TagId = model.TagId, Title = model.Title, Type = model.Type, ClickCount = model.ClickCount }).Where(x => x.Id == model.Id).ExecuteCommandAsync(); await dbClient.Updateable().SetColumns(x => new YB_nSecInfoContent { Content = model.content }).Where(x => x.InfoId == model.Id).ExecuteCommandAsync(); //修改绑定的小程序 await dbClient.Deleteable().Where(x => x.InfoId == model.Id).ExecuteCommandAsync(); var list = new List(); appids.ForEach(x => { list.Add(new YB_nSecInfoAppId { AppId = x, InfoId = model.Id }); }); await dbClient.Insertable(list).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "修改成功"); } else { var info = new YB_nSecInfo { CreateTime = DateTime.Now, Status = (int)AdStatus.Wait, BusienssId = currentUser.BusinessId, ClickCount = model.ClickCount, HeadImg = model.HeadImg, StatusRemark = "", TagId = model.TagId, Title = model.Title, Type = model.Type, Id = IDGen.NextID() }; await dbClient.Insertable(info).ExecuteCommandAsync(); var data = new YB_nSecInfoContent { Content = model.content, InfoId = info.Id }; await dbClient.Insertable(data).ExecuteCommandAsync(); //修改绑定的小程序 var list = new List(); appids.ForEach(x => { list.Add(new YB_nSecInfoAppId { AppId = x, InfoId = model.Id }); }); await dbClient.Insertable(list).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "添加成功"); } } /// /// 状态修改 /// /// 记录ID /// 状态 /// 状态描述 /// public async Task SetStatusAsync(Guid id, AdStatus status, string remark) { if (!await dbClient.Queryable().AnyAsync(x => x.Id == id)) { return new ResultInfo(ResultState.FAIL, "记录未找到"); } //更新状态 await dbClient.Updateable().SetColumns(x => new YB_nSecInfo { Status = status }).Where(x => x.Id == id).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "状态更新成功"); } /// /// 详情 /// /// /// public async Task DetailAsync(Guid id) { var info = await dbClient.Queryable().Select(x => new SecInfoSubmitModel { Title = x.Title, HeadImg = x.HeadImg, TagId = x.TagId, Type = x.Type, Id = x.Id }).Where(x => x.Id == id).FirstAsync(); var content = await dbClient.Queryable().Where(x => x.InfoId == id).FirstAsync(); info.content = content?.Content; var appid = await dbClient.Queryable() .Where(x => x.InfoId == id) .Select(x=>x.AppId) .ToListAsync(); info.AppId = String.Join(",", appid); return info; } /// /// 类型列表 /// /// /// public async Task> GetTypeListAsync(QueryParams param) { RefAsync totalnum = 0; var temquery = dbClient.Queryable(); if (param.queryParam != null && param.queryParam.Count > 0) { List conModels = new List(); param.queryParam.ForEach(x => { if (!string.IsNullOrEmpty(x.Value)) { conModels.Add(new ConditionalModel() { FieldName = x.Name, ConditionalType = (ConditionalType)x.Type, FieldValue = x.Value.Trim() }); } }); if (conModels.Count > 0) { temquery = temquery.Where(conModels); } } if (currentUser.AccountType != AccountType.platform) { temquery = temquery.Where(x => x.BusienssId == currentUser.BusinessId); } string sorts = string.Format("{0} {1}", param.sort, param.order); var query = await temquery.OrderBy(sorts) .Select(x => new SecInfoTypeModel { Id = x.Id, Status = x.Status, SortCode = x.SortCode, BusienssId = x.BusienssId, CreateTime = x.CreateTime, Name = x.Name, Remark = x.Remark }) .Mapper((it, cache) => { var allbuss = cache.Get(list => { var ids = list.Where(x => x.BusienssId > 0).Select(x => x.BusienssId).ToList(); return dbClient.Queryable().Where(x => ids.Contains(x.Id)).ToList(); }); it.BusinessName = allbuss.FirstOrDefault(x => x.Id == it.BusienssId)?.Name; }) .ToPageListAsync(param.offset, param.limit, totalnum); return new PageParms { page = param.offset, Items = query, totalnum = totalnum, limit = param.limit }; } /// /// 所有类型列表 /// /// public async Task> GetAllTypeAsync() { var tempquery = dbClient.Queryable() .Where(x => x.Status == StatusType.Enabled); if (currentUser.AccountType != AccountType.platform) { tempquery = tempquery.Where(x => x.BusienssId == currentUser.BusinessId); } else { tempquery = tempquery.Where(x => x.BusienssId == 0); } return await tempquery .OrderBy(x => x.SortCode, OrderByType.Asc) .ToListAsync(); } /// /// 类型信息修改 /// /// /// public async Task SubmitTypeAsync(SecInfoTypeCS2Dto model) { model.Remark = model.Remark.ToStr(); if (model.Id > 0) { if (!await dbClient.Queryable().AnyAsync(x => x.Id == model.Id)) { return new ResultInfo(ResultState.FAIL, "记录未找到"); } await dbClient.Updateable().SetColumns(x => new YB_SecInfoType { Name = model.Name, SortCode = model.SortCode, Remark = model.Remark }).Where(x => x.Id == model.Id).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "修改成功"); } else { await dbClient.Insertable(new YB_SecInfoType { Name = model.Name, CreateTime = DateTime.Now, Status = StatusType.Enabled, SortCode = model.SortCode, Remark = model.Remark, BusienssId = currentUser.BusinessId }).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "添加成功"); } } /// /// 类型状态修改 /// /// 记录ID /// 状态 /// public async Task SetTypeStatusAsync(int id, StatusType status) { if (!await repository.Change().Context.Queryable().AnyAsync(x => x.Id == id)) { return new ResultInfo(ResultState.FAIL, "记录未找到"); } //更新状态 await repository.Change().Context.Updateable().SetColumns(x => new YB_SecInfoType { Status = status }).Where(x => x.Id == id).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "状态更新成功"); } /// /// 类型详情 /// /// /// public async Task DetailTypeAsync(int id) { return await repository.Change().Context.Queryable().Where(x => x.Id == id).FirstAsync(); } } }