123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- using AutoMapper;
- using OASystem.Domain;
- using OASystem.Domain.Dtos.Resource;
- using OASystem.Domain.Entities.Resource;
- using OASystem.Domain.ViewModels.Resource;
- using SqlSugar;
- namespace OASystem.Infrastructure.Repositories.Resource
- {
- public class ThreeCodeRepository:BaseRepository<Res_ThreeCode,ThreeCodeView>
- {
- private readonly IMapper _mapper;
- public ThreeCodeRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
- {
- _mapper = mapper;
- }
- /// <summary>
- /// sqlSugar分页查询
- /// </summary>
- /// <param name="PageIndex"></param>
- /// <param name="PageSize"></param>
- /// <returns></returns>
- public async Task<Result> QueryThreeCode(QueryThreeCodeDto dto)
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- try
- {
- string sqlWhere = string.Empty;
- if (!string.IsNullOrWhiteSpace(dto.City) && dto.City!="全部")
- {
- sqlWhere += string.Format(@" And City like '%{0}%'", dto.City);
- }
- if (!string.IsNullOrWhiteSpace(dto.Three))
- {
- sqlWhere += string.Format(@" And Three like '%{0}%'", dto.Three);
- }
- sqlWhere += string.Format(@" And IsDel={0}", 0);
- if (!string.IsNullOrEmpty(sqlWhere.Trim()))
- {
- Regex r = new Regex("And");
- sqlWhere = r.Replace(sqlWhere, "Where", 1);
- }
- if (dto.PortType == 1)
- {
- string sql = string.Format(@"select * from Res_ThreeCode {0}", sqlWhere);
- List<Res_ThreeCode> ThreeCode = await _sqlSugar.SqlQueryable<Res_ThreeCode>(sql).ToListAsync();
- if (ThreeCode.Count == 0)
- {
- return result = new Result() { Code = -1, Msg = "暂无数据" };
- }
- ThreeCode = ThreeCode.OrderByDescending(x => x.CreateTime).ToList();
- if (dto.PageSize == 0 && dto.PageIndex == 0)
- {
- return result = new Result()
- {
- Code = 0,
- Msg = "查询成功",
- Data = ThreeCode,
- };
- }
- else
- {
- int count = ThreeCode.Count;
- float totalPage = (float)count / dto.PageSize;//总页数
- if (totalPage == 0) totalPage = 1;
- else totalPage = (int)Math.Ceiling((double)totalPage);
- List<Res_ThreeCode> _ThreeCode = new List<Res_ThreeCode>();
- for (int i = 0; i < dto.PageSize; i++)
- {
- var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
- if (RowIndex < ThreeCode.Count)
- {
- _ThreeCode.Add(ThreeCode[RowIndex]);
- }
- else
- {
- break;
- }
- }
- return result = new Result()
- {
- Code = 0,
- Msg = "查询成功",
- Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = _ThreeCode },
- };
- }
- }
- else if (dto.PortType == 2)
- {
- string sql = string.Format(@"select * from Res_ThreeCode {0}", sqlWhere);
- List<Res_ThreeCode> ThreeCode = await _sqlSugar.SqlQueryable<Res_ThreeCode>(sql).ToListAsync();
- if (ThreeCode.Count == 0)
- {
- return result = new Result() { Code = -1, Msg = "暂无数据" };
- }
- ThreeCode = ThreeCode.OrderByDescending(x => x.CreateTime).ToList();
- if (dto.PageSize == 0 && dto.PageIndex == 0)
- {
- return result = new Result()
- {
- Code = 0,
- Msg = "查询成功",
- Data = ThreeCode,
- };
- }
- else
- {
- int count = ThreeCode.Count;
- float totalPage = (float)count / dto.PageSize;//总页数
- if (totalPage == 0) totalPage = 1;
- else totalPage = (int)Math.Ceiling((double)totalPage);
- List<Res_ThreeCode> _ThreeCode = new List<Res_ThreeCode>();
- for (int i = 0; i < dto.PageSize; i++)
- {
- var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
- if (RowIndex < ThreeCode.Count)
- {
- _ThreeCode.Add(ThreeCode[RowIndex]);
- }
- else
- {
- break;
- }
- }
- return result = new Result()
- {
- Code = 0,
- Msg = "查询成功",
- Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = _ThreeCode },
- };
- }
- }
- else
- {
- return result = new Result() { Code = -2, Msg = "请传入PortType参数!1:Web,2:Android,3:IOS" };
- }
- }
- catch (Exception)
- {
- return result;
- throw;
- }
- }
- /// <summary>
- /// 添加、编辑操作
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<Result> ThreeCodeOperation(ThreeCodeOperationDto dto)
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- try
- {
- if (dto.Status == 1)//添加
- {
- string selectSql = string.Format(@"select * from Res_ThreeCode where Three='{0}' and Country='{1}' and City='{2}' and AirPort='{3}'"
- , dto.Three, dto.Country, dto.City, dto.AirPort);
- var ThreeCode = await _sqlSugar.SqlQueryable<Res_ThreeCode>(selectSql).FirstAsync();//查询是否存在
- if (ThreeCode != null)
- {
- return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
- }
- else//不存在,可添加
- {
- Res_ThreeCode _ThreeCodeDto = _mapper.Map<Res_ThreeCode>(dto);
- int id = await AddAsyncReturnId(_ThreeCodeDto);
- if (id == 0)
- {
- return result = new Result() { Code = -1, Msg = "添加失败!" };
- }
- 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_ThreeCode
- {
- Three=dto.Three,
- Four=dto.Four,
- Country=dto.Country,
- City = dto.City,
- AirPort = dto.AirPort,
- AirPort_En = dto.AirPort_En,
- Remark = dto.Remark,
- });
- if (!res)
- {
- return result = new Result() { Code = -1, Msg = "修改失败!" };
- }
- result = new Result() { Code = 0, Msg = "修改成功!" };
- }
- else
- {
- return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
- }
- }
- catch (Exception ex)
- {
- return result = new Result() { Code = -2, Msg = "程序错误!" };
- }
- return result;
- }
- /// <summary>
- /// 三字码数组查询
- /// </summary>
- /// <param name="codeArr">需要查询的三字码数组</param>
- /// <returns>查询到的键值对数组</returns>
- public Dictionary<string,Res_ThreeCode> QueryThreeCodeArray(List<string> codeArr)
- {
- Dictionary<string, Res_ThreeCode> dic = new Dictionary<string, Res_ThreeCode>();
- codeArr.ForEach(code =>
- {
- code = code.ToUpper();
- });
- var queryData = Query(x => codeArr.Contains(x.Three.ToUpper()) && x.IsDel == 0).ToList();
- var groupData = queryData.GroupBy(x => x.Three).ToList();
- foreach (var item in groupData)
- {
- dic.Add(item.Key, item.ToList()[0]);
- }
- return dic;
- }
- }
- }
|