using AutoMapper; using MathNet.Numerics.Distributions; using MathNet.Numerics.Statistics.Mcmc; using OASystem.Domain; using OASystem.Domain.Dtos.Resource; using OASystem.Domain.Entities.Groups; using OASystem.Domain.Entities.Resource; using OASystem.Domain.ViewModels.Resource; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OASystem.Infrastructure.Repositories.Resource { public class OfficialActivitiesRepository : BaseRepository { private readonly IMapper _mapper; public OfficialActivitiesRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar) { _mapper = mapper; } /// /// 根据Diid查询公务出访数据List /// /// /// public async Task QueryOfficialActivitiesByDiId(OfficialActivitiesByDiIdDto dto) { Result result = new Result() { Code = -2, Msg = "未知错误" }; try { string sqlWhere = string.Empty; sqlWhere += string.Format(@"And o.Isdel={0} And o.DiId={1} ", 0,dto.DiId); if (!string.IsNullOrEmpty(sqlWhere.Trim())) { Regex r = new Regex("And"); sqlWhere = r.Replace(sqlWhere, "Where", 1); } string sql = string.Format(@"select *,(select CnName from Sys_Users where o.CreateUserId=Id) as CreateUserName,(select Name from Sys_SetData where Id=o.OfficialForm) as OfficialFormName from Res_OfficialActivities o {0} order by o.CreateTime desc", sqlWhere); List OfficialActivities = await _sqlSugar.SqlQueryable(sql).ToListAsync(); List data = await _sqlSugar.Queryable().Where(a => a.IsDel == 0 && a.STid == 38).ToListAsync(); if (OfficialActivities.Count != 0) { if (dto.PageSize == 0 && dto.PageIndex == 0) { result = new Result() { Code = 0, Msg = "查询成功!", Data = OfficialActivities }; } else { int count = OfficialActivities.Count; float totalPage = (float)count / dto.PageSize;//总页数 if (totalPage == 0) totalPage = 1; else totalPage = (int)Math.Ceiling((double)totalPage); List _OfficialActivities = new List(); for (int i = 0; i < dto.PageSize; i++) { var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize; if (RowIndex < OfficialActivities.Count) { _OfficialActivities.Add(OfficialActivities[RowIndex]); } else { break; } } ListViewBase rst = new ListViewBase(); rst.DataList = _OfficialActivities; rst.DataCount = count; rst.CurrPageIndex = dto.PageIndex; rst.CurrPageSize = dto.PageSize; result = new Result() { Code = 0, Msg = "查询成功!", Data = rst }; } } else { result = new Result() { Code = 0, Msg = "暂无数据!", Data = OfficialActivities }; } } catch (Exception ex) { result = new Result() { Code = -2, Msg = "未知错误" }; } return result; } /// /// 根据公务出访Id查询单个数据 /// /// /// /// public async Task QueryOfficialActivitiesById(OfficialActivitiesDiIdDto dto) { Result result = new Result() { Code = -2, Msg = "未知错误" }; try { string sqlWhere = string.Empty; sqlWhere += string.Format(@"And o.Isdel={0} And o.DiId={1} And o.Id={2}", 0, dto.DiId,dto.Id); if (!string.IsNullOrEmpty(sqlWhere.Trim())) { Regex r = new Regex("And"); sqlWhere = r.Replace(sqlWhere, "Where", 1); } string sql = string.Format(@"select *,(select CnName from Sys_Users where o.CreateUserId=Id) as CreateUserName,(select Name from Sys_SetData where Id=o.OfficialForm) as OfficialFormName from Res_OfficialActivities o {0}", sqlWhere); OfficialActivitiesView OfficialActivities = await _sqlSugar.SqlQueryable(sql).FirstAsync(); result = new Result() { Code = 0, Msg = "查询成功!", Data=OfficialActivities}; } catch (Exception ex) { result = new Result() { Code = -2, Msg = "未知错误" }; } return result; } public async Task OpOfficialActivities(OpOfficialActivitiesDto dto) { Result result = new Result() { Code = -2, Msg = "未知错误" }; try { if (dto.Status == 1)//添加 { string selectSql = string.Format(@"select * from Res_OfficialActivities where Client='{0}' and Address='{1}' and IsDel='{2}'", dto.Client,dto.Address, 0); var res_InvitationOfficial = await _sqlSugar.SqlQueryable(selectSql).FirstAsync();//查询是否存在 if (res_InvitationOfficial != null) { return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" }; } else//不存在,可添加 { Res_OfficialActivities _InvitationOfficialActivityData = _mapper.Map(dto); int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync(); if (id == 0) { return result = new Result() { Code = -1, Msg = "添加失败!" }; } return result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } }; } } else if (dto.Status == 2)//修改 { bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_OfficialActivities { Type = dto.Type, Client = dto.Client, Date = dto.Date, Time = dto.Time, Address = dto.Address, Contact = dto.Contact, Job = dto.Job, Tel = dto.Tel, OfficialForm = dto.OfficialForm, Setting = dto.Setting, Dresscode = dto.Dresscode, Attendees = dto.Attendees, IsNeedTrans = dto.IsNeedTrans, Translators = dto.Translators, language = dto.language, Trip = dto.Trip, CreateUserId = dto.CreateUserId, Remark = dto.Remark, }); if (!res) { return result = new Result() { Code = -1, Msg = "修改失败!" }; } return result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } }; } else { return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" }; } } catch (Exception ex) { return result = new Result() { Code = -2, Msg = "程序错误!" }; } } } }