| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 | using AutoMapper;using EyeSoft.Collections.Generic;using OASystem.Domain;using OASystem.Domain.Dtos.Groups;using OASystem.Domain.Entities.Groups;using OASystem.Domain.ViewModels.Groups;using OASystem.Infrastructure.Tools;using Org.BouncyCastle.Utilities;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OASystem.Infrastructure.Repositories.Groups{    /// <summary>    /// 团组-出入境费用报价表 仓储    /// </summary>    public class EnterExitCostQuoteRepository : BaseRepository<Grp_EnterExitCostQuoteItem, EnterExitCostQuoteView>    {        private readonly IMapper _mapper;        public EnterExitCostQuoteRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)        {            _mapper = mapper;        }        /// <summary>        /// 初始化基础项        /// </summary>        /// <param name="removeNl">是否移除换行符</param>        /// <returns></returns>        public async Task<List<InitBasicItemView>> InitBasicItemAsync(bool removeNl)        {            var origList = await _sqlSugar.Queryable<Sys_SetData>()                .Where(x => x.IsDel == 0 && x.STid == 105)                .Select(x => new                {                    Id = x.Id,                    Name = x.Name,                    Index = x.Remark ?? "-1"                }).ToListAsync();            var newList = origList.Select(x => new InitBasicItemView                {                    Id = x.Id,                    Name = removeNl ? x.Name.Replace("\\r\\n", "") : x.Name,                    Index = int.TryParse(x.Index, out int index) ? index : -1                })                .OrderBy(x => x.Index)                .ToList();            var onlyItems = new List<int>() {                1358, //邀请函发放对象                1360, //邀请函发放时间            };            newList.ForEach(x =>            {                if (onlyItems.Contains(x.Id)) x.IsOnlyRemark = true;            });            return newList;        }        /// <summary>        /// 获取报价名称列表        /// </summary>        /// <param name="name"></param>        /// <param name="pageIndex"></param>        /// <param name="pageSize"></param>        /// <returns></returns>        public async Task<JsonView> QuoteNameListAsync(EnterExitCostQuoteNameListDto dto)        {            int pageIndex = dto.PageIndex, pageSize = dto.PageSize;            string name = dto.Name;            if (pageIndex < 1 || pageSize < 1)            {                var noPageList = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()                .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.Name))                .WhereIF(!string.IsNullOrEmpty(name), x => x.Name.Contains(name))                .Select(x => new EnterExitCostQuoteNameView                {                    Id = x.Id,                    Name = x.Name                }).ToListAsync();                return new JsonView                {                    Code = StatusCodes.Status200OK,                    Msg = "获取成功",                    Count = noPageList.Count,                    Data = noPageList                };            }            RefAsync<int> total = 0;            var pageList = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()                .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.Name))                .WhereIF(!string.IsNullOrEmpty(name), x => x.Name.Contains(name))                .Select(x => new EnterExitCostQuoteNameView                {                    Id = x.Id,                    Name = x.Name                }).ToPageListAsync(pageIndex, pageSize, total);            return new JsonView            {                Code = StatusCodes.Status200OK,                Msg = "获取成功",                Count = total,                Data = pageList            };        }        /// <summary>        /// 获取团组名称列表        /// </summary>        /// <param name="name"></param>        /// <param name="pageIndex"></param>        /// <param name="pageSize"></param>        /// <returns></returns>        public async Task<JsonView> GroupNameListAsync(EnterExitCostQuoteGroupNameListDto dto)        {            int pageIndex = dto.PageIndex, pageSize = dto.PageSize;            string name = dto.Name;            if (pageIndex < 1 || pageSize < 1)            {                var noPageList = await _sqlSugar.Queryable<Grp_DelegationInfo>()                .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.TeamName))                .WhereIF(!string.IsNullOrEmpty(name), x => x.TeamName.Contains(name))                .OrderByDescending(x => x.CreateTime)                .Select(x => new EnterExitCostQuoteGroupNameView                {                    Id = x.Id,                    Name = x.TeamName                }).ToListAsync();                return new JsonView                {                    Code = StatusCodes.Status200OK,                    Msg = "获取成功",                    Count = noPageList.Count,                    Data = noPageList                };            }            RefAsync<int> total = 0;            var pageList = await _sqlSugar.Queryable<Grp_DelegationInfo>()                .Where(x => x.IsDel == 0 && !string.IsNullOrEmpty(x.TeamName))                .WhereIF(!string.IsNullOrEmpty(name), x => x.TeamName.Contains(name))                .OrderByDescending(x => x.CreateTime)                .Select(x => new EnterExitCostQuoteGroupNameView                {                    Id = x.Id,                    Name = x.TeamName                }).ToPageListAsync(pageIndex, pageSize, total);            return new JsonView            {                Code = StatusCodes.Status200OK,                Msg = "获取成功",                Count = total,                Data = pageList            };        }        /// <summary>        /// 获取报价详情        /// </summary>        /// <param name="name"></param>        /// <param name="pageIndex"></param>        /// <param name="pageSize"></param>        /// <returns></returns>        public async Task<EnterExitCostQuoteInfoView> InfoAsync(EnterExitCostQuoteInfoDto dto)        {            int quoteId = dto.Id;            var viewInfo = new EnterExitCostQuoteInfoView();            viewInfo.Id = quoteId;            var basicItems = await InitBasicItemAsync(true);            if (basicItems.Any())            {                var quoteItemInfos = new List<QuoteItemInfo>();                basicItems.ForEach(x =>                {                    var quoteSubItemInfos = new List<QuoteSubItemInfo>();                    if (x.IsOnlyRemark)                    {                        quoteSubItemInfos.Add(new QuoteSubItemInfo                        {                            Id = 0,                            ItemId = x.Id,                            Index = 1,                            FeeName = "",                            UnitPrice = 0.00M,                            Currency = "CNY",                            Quantity = 1.00M,                            PplNum = 1,                            TotalAmt = 0.00M,                            Remark = $"-"                        });                    }                    quoteItemInfos.Add(new QuoteItemInfo                    {                        QuoteId = quoteId,                        ItemId = x.Id,                        ItemName = x.Name,                        IsOnlyRemark = x.IsOnlyRemark,                        Infos = quoteSubItemInfos.ToArray(),                        Index = x.Index                    });                });                viewInfo.FeeItems = quoteItemInfos.OrderBy(x => x.Index).ToArray();            }            var quoteInfo = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()                 .Where(x => x.IsDel == 0 && x.Id == quoteId)                 .FirstAsync();            if (quoteInfo != null)            {                viewInfo.Id = quoteInfo.Id;                viewInfo.Name = quoteInfo.Name;                viewInfo.GroupId = quoteInfo.GroupId;                viewInfo.Rates = CommonFun.GetCurrencyChinaToList(quoteInfo.CurrencyRemark).ToArray();                var quoteItems = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>()                    .Where(x => x.IsDel == 0 && x.QuoteId == quoteInfo.Id)                    .Select(x => new QuoteSubItemInfo                    {                        Id = x.Id,                        ItemId = x.ItemId,                        FeeName = x.FeeName,                        UnitPrice = x.UnitPrice,                        Quantity = x.Quantity,                        PplNum = x.PplNum,                        Currency = x.Currency,                        TotalAmt = x.TotalAmt,                        Index = x.Index,                        Remark = x.Remark                    }).ToListAsync();                if (quoteItems.Any())                {                    foreach (var x in viewInfo.FeeItems)                    {                        var currQuoteInfos = quoteItems.Where(y => y.ItemId == x.ItemId).OrderBy(y => y.Index).ToArray();                        if (x.IsOnlyRemark)                        {                            if (currQuoteInfos.Length > 0)                            {                                x.Infos = currQuoteInfos;                            }                        }                        else x.Infos = currQuoteInfos;                    }                }            }            return viewInfo;        }        /// <summary>        /// 获取报价详情 By GroupId        /// </summary>        /// <param name="name"></param>        /// <param name="pageIndex"></param>        /// <param name="pageSize"></param>        /// <returns></returns>        public async Task<EnterExitCostQuoteInfoView> InfoByGroupIdAsync(int groupId)        {            var viewInfo = new EnterExitCostQuoteInfoView();            var basicItems = await InitBasicItemAsync(true);            if (basicItems.Any())            {                var quoteItemInfos = new List<QuoteItemInfo>();                basicItems.ForEach(x =>                {                    var quoteSubItemInfos = new List<QuoteSubItemInfo>();                    if (x.IsOnlyRemark)                    {                        quoteSubItemInfos.Add(new QuoteSubItemInfo                        {                            Id = 0,                            ItemId = x.Id,                            Index = 1,                            FeeName = "",                            UnitPrice = 0.00M,                            Currency = "CNY",                            Quantity = 1.00M,                            PplNum = 1,                            TotalAmt = 0.00M,                            Remark = $"-"                        });                    }                    quoteItemInfos.Add(new QuoteItemInfo                    {                        QuoteId = 0,                        ItemId = x.Id,                        ItemName = x.Name,                        IsOnlyRemark = x.IsOnlyRemark,                        Infos = quoteSubItemInfos.ToArray(),                        Index = x.Index                    });                });                viewInfo.FeeItems = quoteItemInfos.OrderBy(x => x.Index).ToArray();            }            var quoteInfo = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>()                 .Where(x => x.IsDel == 0 && x.GroupId == groupId)                 .FirstAsync();            if (quoteInfo != null)            {                viewInfo.Id = quoteInfo.Id;                viewInfo.Name = quoteInfo.Name;                viewInfo.GroupId = quoteInfo.GroupId;                viewInfo.Rates = CommonFun.GetCurrencyChinaToList(quoteInfo.CurrencyRemark).ToArray();                var quoteItems = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>()                    .Where(x => x.IsDel == 0 && x.QuoteId == quoteInfo.Id)                    .Select(x => new QuoteSubItemInfo                    {                        Id = x.Id,                        ItemId = x.ItemId,                        FeeName = x.FeeName,                        UnitPrice = x.UnitPrice,                        Quantity = x.Quantity,                        PplNum = x.PplNum,                        Currency = x.Currency,                        TotalAmt = x.TotalAmt,                        Index = x.Index                    }).ToListAsync();                if (quoteItems.Any())                {                    //viewInfo.FeeItems = viewInfo.FeeItems.Select(x => new QuoteItemInfo { Index = x.Index, QuoteId = quoteInfo.Id, ItemId = x.ItemId, ItemName = x.ItemName, Infos = quoteItems.Where(y => y.ItemId == x.ItemId).OrderBy(y => y.Index).ToArray() }).ToArray();                    viewInfo.FeeItems.ForEach(x =>                    {                        x.QuoteId = quoteInfo.Id;                        x.Infos = quoteItems.Where(y => y.ItemId == x.ItemId).OrderBy(y => y.Index).ToArray(); ;                    });                }            }            return viewInfo;        }        /// <summary>        /// Add Or Edit        /// </summary>        /// <param name="name"></param>        /// <param name="pageIndex"></param>        /// <param name="pageSize"></param>        /// <returns></returns>        public async Task<JsonView> OpAsync(EnterExitCostQuoteOpDto dto)        {            var jw = new JsonView() { Code = StatusCodes.Status400BadRequest };            string quoteName = dto.Name;            int quoteId = dto.Id;            if (string.IsNullOrEmpty(dto.Name))            {                jw.Msg = "报价名称不能为空";                return jw;            }            var quoteInfo = new Grp_EnterExitCostQuote            {                Id = quoteId,                Name = quoteName,                GroupId = dto.GroupId,                CurrencyRemark = CommonFun.GetCurrencyChinaToString(dto.Rates.ToList()),                CreateUserId = dto.CurrUserId            };            var quoteItemInfos = new List<Grp_EnterExitCostQuoteItem>();            if (dto.FeeItems.Any())            {                foreach (var item in dto.FeeItems)                {                    if (item.Infos.Any())                    {                        quoteItemInfos.AddRange(item.Infos.Select(x => new Grp_EnterExitCostQuoteItem                        {                            Id = x.Id,                            QuoteId = quoteId,                            ItemId = x.ItemId,                            Index = x.Index,                            FeeName = x.FeeName,                            UnitPrice = x.UnitPrice,                            Currency = x.Currency,                            Quantity = x.Quantity,                            PplNum = x.PplNum,                            TotalAmt = x.TotalAmt,                            Remark = x.Remark,                            CreateUserId = dto.CurrUserId                        }));                    }                }            }            var isNull = await _sqlSugar.Queryable<Grp_EnterExitCostQuote>().FirstAsync(x => x.IsDel == 0 && x.Id == quoteId) == null ? true : false;            _sqlSugar.BeginTran();            if (isNull) //新增            {                quoteId = await _sqlSugar.Insertable(quoteInfo).ExecuteReturnIdentityAsync();                if (quoteId < 1)                {                    jw.Msg = "新增失败!";                    _sqlSugar.RollbackTran();                    return jw;                }                quoteItemInfos.ForEach(x => x.QuoteId = quoteId);                var quoteItemIds = await _sqlSugar.Insertable(quoteItemInfos).ExecuteReturnIdentityAsync();                if (quoteItemIds < 1)                {                    jw.Msg = "新增失败";                    _sqlSugar.RollbackTran();                    return jw;                }                jw.Msg = "新增成功!";            }            else if (!isNull) //编辑            {                var quoteUpd = await _sqlSugar.Updateable(quoteInfo)                    .IgnoreColumns(x => new { x.CreateUserId, x.CreateTime, x.IsDel })                    .ExecuteCommandAsync();                if (quoteUpd < 1)                {                    jw.Msg = "编辑失败!";                    _sqlSugar.RollbackTran();                    return jw;                }                var insertItems = quoteItemInfos.Where(x => x.Id < 1).ToList();                var updItems = quoteItemInfos.Where(x => x.Id > 0).ToList();                if (insertItems.Any())                {                    foreach (var item in insertItems) {                        var insertInfo = await _sqlSugar.Insertable(item).ExecuteReturnEntityAsync();                        if (insertInfo == null)                        {                            jw.Msg = "编辑失败!";                            _sqlSugar.RollbackTran();                            return jw;                        }                        quoteItemInfos.Add(insertInfo);                    }                }                if (updItems.Any())                {                    var updItem = await _sqlSugar.Updateable(updItems).IgnoreColumns(x => new { x.CreateUserId, x.CreateTime, x.IsDel }).ExecuteCommandAsync();                    if (updItem < 1)                    {                        jw.Msg = "编辑失败!";                        _sqlSugar.RollbackTran();                        return jw;                    }                }                //验证及处理前台删除项数据                var perDelItems = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>()                    .Where(x => x.IsDel == 0 && x.QuoteId == quoteId)                    .ToListAsync();                if (perDelItems.Any())                {                    var delItems = perDelItems.Where(x => !quoteItemInfos.Select(y => y.Id).Contains(x.Id)).ToList();                    if (delItems.Any())                    {                        var newDelItems = delItems.Select(x =>                            new Grp_EnterExitCostQuoteItem                            {                                Id = x.Id,                                DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),                                DeleteUserId = dto.CurrUserId,                                IsDel = 1                            }).ToList();                        var delItemStatus = await _sqlSugar.Updateable(newDelItems)                            .UpdateColumns(x => new { x.DeleteTime, x.DeleteUserId, x.IsDel })                            .ExecuteCommandAsync();                        if (delItemStatus < 1)                        {                            jw.Msg = "移除项费用失败!";                            _sqlSugar.RollbackTran();                            return jw;                        }                    }                }                jw.Msg = "编辑成功!";            }            _sqlSugar.CommitTran();            jw.Code = StatusCodes.Status200OK;            jw.Data = quoteId;            return jw;        }        /// <summary>        /// ItemDel        /// </summary>        /// <param name="name"></param>        /// <param name="pageIndex"></param>        /// <param name="pageSize"></param>        /// <returns></returns>        public async Task<JsonView> ItemDelAsync(EnterExitCostQuoteItemDelDto dto)        {            var jw = new JsonView() { Code = StatusCodes.Status400BadRequest };            int currUserId = dto.CurrUserId,                id = dto.Id;            if (currUserId < 1)            {                jw.Msg = MsgTips.UserId;                return jw;            }            if (id < 1)            {                jw.Msg = MsgTips.Id;                return jw;            }            var info = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>().FirstAsync(x => x.IsDel == 0 && x.Id == id);            if (info == null)            {                jw.Msg = $"操作成功!";                jw.Code = StatusCodes.Status200OK;                return jw;            }            var itemInfos = await _sqlSugar.Queryable<Grp_EnterExitCostQuoteItem>().Where(x => x.IsDel == 0 && x.QuoteId == info.QuoteId && x.ItemId == info.ItemId).ToListAsync();            _sqlSugar.BeginTran();            var delInfo = new Grp_EnterExitCostQuoteItem { DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), DeleteUserId = currUserId, IsDel = 1 };            var del = await _sqlSugar.Updateable(delInfo)                .UpdateColumns(x => new { x.DeleteTime, x.DeleteUserId, x.IsDel })                .Where(x => x.Id == id)                .ExecuteCommandAsync();            if (del < 1)            {                jw.Msg = $"删除失败!";                _sqlSugar.RollbackTran();                return jw;            }            if (itemInfos.Any())            {                // 更新剩余项的索引                var itemsToUpdate = itemInfos.Where(i => i.Index > itemInfos.First(i => i.Id == id).Index).ToList();                foreach (var item in itemsToUpdate)                {                    item.Index -= 1;                }                await _sqlSugar.Updateable(itemsToUpdate).ExecuteCommandAsync();            }            _sqlSugar.CommitTran();            jw.Msg = "操作成功!";            jw.Code = StatusCodes.Status200OK;            return jw;        }    }}
 |