| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650 | using AutoMapper;using MathNet.Numerics.Statistics.Mcmc;using NPOI.SS.Formula.Functions;using OASystem.Domain;using OASystem.Domain.Dtos.Financial;using OASystem.Domain.Dtos.Groups;using OASystem.Domain.Entities.Financial;using OASystem.Domain.Entities.Groups;using OASystem.Domain.ViewModels.Financial;using OASystem.Domain.ViewModels.Groups;using OASystem.Infrastructure.Repositories.Groups;using OASystem.Infrastructure.Repositories.System;using SqlSugar;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OASystem.Infrastructure.Repositories.Financial{    /// <summary>    /// 财务 - 团组应收款项 仓库    /// 雷怡 2023.08.16 15:03    /// </summary>    public class ForeignReceivablesRepository : BaseRepository<Fin_ForeignReceivables, Fin_ForeignReceivablesView>    {        private readonly IMapper _mapper;        private readonly DelegationInfoRepository _delegationRep;        private readonly SetDataRepository _setDataRep;                public ForeignReceivablesRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository delegationRep, SetDataRepository setDataRep)            : base(sqlSugar)        {            _mapper = mapper;            _delegationRep = delegationRep;            _setDataRep = setDataRep;        }        #region 关联已收款项        /// <summary>        /// 收款账单 数据源        /// </summary>        /// <returns></returns>        public async Task<JsonView> GetDataSource(ForeignReceivablesDataSourcesDto dto)        {            JsonView result = new() { Code = StatusCodes.Status204NoContent };            //已收款项 判断如果是市场部的人员进来的话 只显示自己的 其他的都显示全部的            var userInfos = await _sqlSugar.Queryable<Sys_Users>()                                           .InnerJoin<Sys_Department>((u, d) => u.DepId == d.Id)                                           .Where((u, d) => u.IsDel == 0 && d.DepName.Contains("市场部") && u.Id == dto.CurrUserId)                                           .ToListAsync();            string sqlWhere = "";            if (userInfos.Count > 0) sqlWhere = string.Format(@$" And JietuanOperator={dto.CurrUserId} ");            string sql = string.Format(@$"Select Id,TeamName GroupName From  Grp_DelegationInfo                                              Where TeamName != '' And IsDel = 0 {sqlWhere}                                              Order By Id Desc");            var _groupNameList = await _sqlSugar.SqlQueryable<GroupNameView>(sql).ToListAsync();            //var groupNameData = await _delegationRep.GetGroupNameList(new GroupNameDto());            var currencyData = await _setDataRep.GetSetDataBySTId(_setDataRep, 66); //币种            var remittanceMethodData = await _setDataRep.GetSetDataBySTId(_setDataRep, 14); //汇款方式            result.Code = StatusCodes.Status200OK;            result.Msg = "成功!";            result.Data = new            {                GroupNameData = _groupNameList,                CurrencyData = currencyData.Data,                RemittanceMethodData = remittanceMethodData.Data            };            return result;        }        /// <summary>        /// 根据diid查询团组应收款项        /// </summary>        /// <param name="diid"></param>        /// <returns></returns>        public async Task<Result> GetGroupReceivablesInfoByDiId(ForForeignReceivablesInfoDto dto)        {            Result result = new() { Code = -2 };            var groupInfoData = await _delegationRep.GetGroupInfo(new GroupInfoDto() { Id = dto.DiId });            //应收款项            string groupReceivedSql = string.Format(@"Select * From Fin_ForeignReceivables Where IsDel=0 And Diid={0}", dto.DiId);            var groupReceivedList = await _sqlSugar.SqlQueryable<ForeignReceivablesView>(groupReceivedSql).ToListAsync();            //已收款项            string groupProceedsReceivedSql = string.Format(@"Select * From Fin_ProceedsReceived Where IsDel=0 And Diid={0}", dto.DiId);            var groupProceedsReceivedList = await _sqlSugar.SqlQueryable<ProceedsReceivedView>(groupProceedsReceivedSql).ToListAsync();            List<ProceedsReceivedView> NotFIDData = new List<ProceedsReceivedView>();            if (dto.PortType == 1)            {                foreach (var item in groupReceivedList)                {                    item._ProceedsReceivedDatas = groupProceedsReceivedList.Where(s => s.FID == item.Id).ToList();                }                NotFIDData = groupProceedsReceivedList.Where(s => !groupReceivedList.Any(e => s.FID == e.Id)).ToList();            }            result.Code = 0;            result.Msg = "查询成功!";            result.Data = new            {                GroupInfo = groupInfoData.Data,                GroupCollectionStatementData = new                {                    ReceivedData = groupReceivedList,                    UnallocatedData = NotFIDData                }            };            return result;        }        /// <summary>        /// 应收款项 删除        /// </summary>        /// <param name="dto"></param>        /// <returns></returns>        public async Task<Result> _Del(DelForForeignReceivablesInfoDto dto)        {            Result result = new Result() { Code = -1, Msg = "程序错误!" };            _sqlSugar.BeginTran();            try            {                var res = await _sqlSugar.Updateable<Fin_ForeignReceivables>()                .Where(it => it.Id == dto.Id)                .SetColumns(it => new Fin_ForeignReceivables()                {                    IsDel = 1,                    DeleteUserId = dto.UserId,                    DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")                }                ).ExecuteCommandAsync();                if (res > 0)                {                    await _sqlSugar.Updateable<Fin_ProceedsReceived>()                                      .Where(a => a.FID == dto.Id)                                      .SetColumns(a => new Fin_ProceedsReceived                                      {                                          IsDel = 1,                                          DeleteUserId = dto.UserId,                                          DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),                                      }).ExecuteCommandAsync();                    _sqlSugar.CommitTran();                    result.Msg = "删除成功!";                    result.Code = 0;                }                else                {                    _sqlSugar.RollbackTran();                    result.Msg = "删除失败!";                    return result;                }            }            catch (Exception ex)            {                _sqlSugar.RollbackTran();                result.Msg = ex.Message;                return result;            }            return result;        }        /// <summary>        /// 财务模块        /// 收款账单 Add And Update        /// </summary>        /// <param name="diid"></param>        /// <returns></returns>        public async Task<Result> PostReceivablesOperate(ForeignReceivablesAddAndUpdateDto dto)        {            Result result = new() { Code = -2 };            if (dto.foreignReceivablesInfos.Count <= 0)            {                result.Msg = "收款账单没有信息,不能进行,添加或修改操作!!!";                return result;            }            int addCount = 0, updateCount = 0;            if (dto.PortType == 1)            {                List<Fin_ForeignReceivables> _ForeignReceivables = new List<Fin_ForeignReceivables>();                foreach (var item in dto.foreignReceivablesInfos)                {                    _ForeignReceivables.Add(new Fin_ForeignReceivables()                    {                        Diid = dto.DiId,                        Id = item.Id,                        PriceName = item.PriceName,                        Price = item.Price,                        Count = item.Count,                        Unit = item.Unit,                        ItemSumPrice = item.ItemSumPrice,                        Rate = item.Rate,                        Currency = item.Currency,                        AddingWay = item.AddingWay,                        CreateUserId = dto.UserId,                        CreateTime = DateTime.Now,                        Remark = item.Remark                    });                }                if (_ForeignReceivables.Count > 0)                {                    var x = _sqlSugar.Storageable(_ForeignReceivables).ToStorage();                    addCount = x.AsInsertable.ExecuteCommand();        //不存在插入                    updateCount = x.AsUpdateable.ExecuteCommand();    //存在更新                }                result.Code = 0;                result.Msg = string.Format(@"操作成功!添加:{0}条;更新:{1};", addCount, updateCount);            }            return result;        }        /// <summary>        /// 根据diid查询团组应收款项        /// </summary>        /// <param name="diid"></param>        /// <returns></returns>        public async Task<Result> GetGroupReceivablesByDiid(int diid)        {            Result result = new() { Code = -2 };            string sql = string.Format(@"Select * From Fin_ForeignReceivables Where IsDel=0 And Diid={0}", diid);            var groupReceivedList = await _sqlSugar.SqlQueryable<Fin_ForeignReceivables>(sql).ToListAsync();            result.Code = 0;            result.Msg = "查询成功!";            result.Data = groupReceivedList;            return result;        }        /// <summary>        /// 根据diid 数组 查询团组应收款项        /// </summary>        /// <param name="diid"></param>        /// <returns></returns>        public async Task<Result> GetGroupReceivablesByDiids(int[] diids)        {            Result result = new() { Code = -2 };            //string sql = string.Format(@"Select * From Fin_ProceedsReceived Where IsDel=0 And Diid={0}", diids);            var groupReceivedList = await _sqlSugar.Queryable<Fin_ForeignReceivables>()                .Where(pr => pr.IsDel == 0 && diids.Contains(pr.Diid)).ToListAsync();            result.Code = 0;            result.Msg = "查询成功!";            result.Data = groupReceivedList;            return result;        }        #endregion        #region 未关联已收款项        /// <summary>        /// 收款账单 数据源        /// </summary>        /// <returns></returns>        public async Task<JsonView> PostDataSource(ForeignReceivablesDataSourcesDto dto)        {            JsonView result = new() { Code = StatusCodes.Status204NoContent };            //已收款项 判断如果是市场部的人员进来的话 只显示自己的 其他的都显示全部的            var userInfos = await _sqlSugar.Queryable<Sys_Users>()                                           .InnerJoin<Sys_Department>((u, d) => u.DepId == d.Id)                                           .Where((u, d) => u.IsDel == 0 && d.DepName.Contains("市场部") && u.Id == dto.CurrUserId)                                           .ToListAsync();            string sqlWhere = "";            if (userInfos.Count > 0) sqlWhere = string.Format(@$" And JietuanOperator={dto.CurrUserId} ");            string sql = string.Format(@$"Select Id,TeamName GroupName From  Grp_DelegationInfo                                              Where TeamName != '' And IsDel = 0 {sqlWhere}                                              Order By Id Desc");            var _groupNameList = await _sqlSugar.SqlQueryable<GroupNameView>(sql).ToListAsync();            //var groupNameData = await _delegationRep.GetGroupNameList(new GroupNameDto());            var currencyData = await _setDataRep.GetSetDataBySTId(_setDataRep, 66); //币种            var remittanceMethodData = await _setDataRep.GetSetDataBySTId(_setDataRep, 14); //汇款方式            result.Code = StatusCodes.Status200OK;            result.Msg = "成功!";            result.Data = new            {                GroupNameData = _groupNameList,                CurrencyData = currencyData.Data,                RemittanceMethodData = remittanceMethodData.Data            };            return result;        }        /// <summary>        /// 根据diid查询团组应收款项        /// </summary>        /// <param name="diid"></param>        /// <returns></returns>        public async Task<JsonView> PostGroupReceivablesInfoByDiId(ForForeignReceivablesNewDto dto)        {            JsonView result = new() { Code = 400,Msg="" };            //var groupInfoData = await _delegationRep.GetGroupInfo(new GroupInfoDto() { Id = dto.DiId });            var groupInfoData = await _delegationRep.Query(x => x.Id == dto.DiId)                .Select(x => new                {                    x.Id,                    x.TeamName,                    x.TourCode,                    x.ClientName,                    VisitCountry = x.VisitCountry.Replace("|","、"),                    x.VisitDays,                    x.VisitPNumber,                    x.VisitStartDate,                    x.VisitEndDate                })                .FirstAsync();            //应收款项            string groupReceivedSql = string.Format(@"Select *,su.CnName As AuditorName From Fin_ForeignReceivables  ffrLeft Join Sys_Users su On  ffr.Auditor = su.IdWhere ffr.IsDel=0 And ffr.Diid={0}", dto.DiId);            var groupReceivedList = await _sqlSugar.SqlQueryable<ProceedsReceivedNewView>(groupReceivedSql).ToListAsync();            result.Code = 200;            result.Msg = "查询成功!";            result.Data = new            {                GroupInfo = groupInfoData,                GroupCollectionStatementData = groupReceivedList            };            return result;        }        /// <summary>        /// 财务模块        /// 收款账单 Add And Update        /// </summary>        /// <param name="diid"></param>        /// <returns></returns>        public async Task<JsonView> PostReceivablesSave(ForeignReceivablesSaveDto dto)        {            JsonView result = new() { Code = 4002 };            if (dto.foreignReceivablesInfos.Count <= 0)            {                result.Msg = "收款账单没有信息,不能进行,添加或修改操作!!!";                return result;            }            int addCount = 0, updateCount = 0;            if (dto.PortType == 1)            {                //查询值是否更改                var selectInfos = await _sqlSugar.Queryable<Fin_ForeignReceivables>().Where(it => it.Diid == dto.DiId && it.Status == 1).ToListAsync();                List <Fin_ForeignReceivables> _ForeignReceivables = new List<Fin_ForeignReceivables>();                foreach (var item in dto.foreignReceivablesInfos)                {                    int status = 0, auditor = 0;                    string auditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");                    var info = selectInfos.Find(x => x.Id == item.Id);                    if (info != null)                    {                        if (info.ItemSumPrice == item.ItemSumPrice)                        {                            status = info.Status;                            auditor = info.Auditor;                            auditTime = info.AuditTime;                        }                    }                    _ForeignReceivables.Add(new Fin_ForeignReceivables()                    {                        Diid = dto.DiId,                        Id = item.Id,                        PriceName = item.PriceName,                        Price = item.Price,                        Count = item.Count,                        Unit = item.Unit,                        ItemSumPrice = item.ItemSumPrice,                        Rate = item.Rate,                        Currency = item.Currency,                        AddingWay = item.AddingWay,                        CreateUserId = dto.UserId,                        CreateTime = DateTime.Now,                        Remark = item.Remark,                        Status = status,                        Auditor = auditor,                        AuditTime = auditTime                    });                }                if (_ForeignReceivables.Count > 0)                {                    var x = _sqlSugar.Storageable(_ForeignReceivables).ToStorage();                    addCount = x.AsInsertable.ExecuteCommand();       //不存在插入                    updateCount = x.AsUpdateable.IgnoreColumns(it => new                                     {                                          //it.Status,                                         //it.Auditor,                                         //it.AuditTime,                                         it.CreateUserId,                                         it.CreateTime                                     }).ExecuteCommand();    //存在更新                }                result.Code = 200;                result.Msg = string.Format(@"操作成功!添加:{0}条;更新:{1};", addCount, updateCount);            }            return result;        }        /// <summary>        /// 财务模块        /// 收款账单         /// Audit        /// </summary>        /// <param name="diid"></param>        /// <returns></returns>        public async Task<JsonView> FeeAudit(FeeAuditDto dto)        {            JsonView result = new() { Code = 400 };            //验证            var info = await _sqlSugar.Queryable<Fin_ForeignReceivables>().FirstAsync(x => x.Id == dto.Id);            if (info == null)            {                result.Msg = "数据不存在";                return result;            }            if (info.AddingWay != 2)            {                result.Msg = "该条数据类型不是“实际报价”类型不可审核!";                return result;            }                       var AuditStatus = await _sqlSugar.Updateable<Fin_ForeignReceivables>()                                      .SetColumns(a => new Fin_ForeignReceivables                                      {                                          Status = dto.Status,                                          Auditor = dto.UserId,                                          AuditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),                                      })                                      .Where(a => a.Id == dto.Id)                                      .ExecuteCommandAsync();            if (AuditStatus > 0)            {                result.Msg = "操作成功!";                result.Code = 200;            }            else            {                result.Msg = "操作失败!";            }            return result;        }        /// <summary>        /// 财务模块        /// 收款账单         /// Del        /// </summary>        /// <param name="diid"></param>        /// <returns></returns>        public async Task<Result> PostReceivablesDel(ForeignReceivablesDelDto dto)        {            Result result = new() { Code = -2 };           var delStatus = await _sqlSugar.Updateable<Fin_ForeignReceivables>()                                     .SetColumns(a => new Fin_ForeignReceivables                                     {                                         IsDel = 1,                                         DeleteUserId = dto.UserId,                                         DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),                                     })                                     .Where(a => a.Id == dto.Id)                                     .ExecuteCommandAsync();            if (delStatus > 0)            {                result.Msg = "操作成功!";                result.Code = 0;            }            else            {                result.Msg = "操作成功!";            }            return result;        }        public void OverSpSeteceivables(OverSpSeteceivablesDto dto)        {            var querySaveReceivables = _sqlSugar.Queryable<Fin_ForeignReceivables>()    .First(x => x.Diid == dto.DiId && x.AddingWay == 3 && x.IsDel == 0);            //获取所有超支数据            //整理超支数据            //币种不同则计算rmb值            var overspList = _sqlSugar.Queryable<Fin_GroupExtraCost, Grp_CreditCardPayment>                ((e, c) => c.CTable == 1015 && c.CId == e.Id && c.IsDel == 0).                Where((e, c) => e.IsDel == 0 && e.DiId == dto.DiId && (e.SupervisorConfirm == 1 || e.ManagerConfirm == 1)).                Select((e, c) => new                {                    e.Price,                    e.PriceCount,                    e.Coefficient,                    e.PriceSum,                    c.RMBPrice,                    c.DayRate,                    e.PriceCurrency,                    e.Remark,                })                .ToList();            var overspListGroup = overspList.GroupBy(x => x.PriceCurrency).ToList();            string foreignReceivablesRemake = string.Empty;            int count = 1;            overspList.ForEach(x =>            {                foreignReceivablesRemake += $"{count}.{x.Remark}";                count++;            });            if (querySaveReceivables != null)            {                if (overspList.Count() > 0)                {                    if (overspListGroup.Count() > 1)                    {                        querySaveReceivables.Currency = 836; //人民币                        querySaveReceivables.Rate = 1;                        querySaveReceivables.Price = overspListGroup.Sum(x => x.Sum(y => y.PriceSum * y.Coefficient * y.DayRate));                        querySaveReceivables.ItemSumPrice = overspListGroup.Sum(x => x.Sum(y => y.PriceSum * y.Coefficient * y.DayRate));                        querySaveReceivables.Remark = foreignReceivablesRemake;                        querySaveReceivables.CreateTime = DateTime.Now;                    }                    else                    {                        querySaveReceivables.Currency = overspList[0].PriceCurrency;                        querySaveReceivables.Rate = overspList[0].DayRate;                        querySaveReceivables.Price = overspListGroup[0].Sum(x => x.PriceSum * x.Coefficient);                        querySaveReceivables.ItemSumPrice = overspListGroup[0].Sum(x => x.PriceSum * x.Coefficient);                        querySaveReceivables.Remark = foreignReceivablesRemake;                        querySaveReceivables.AddingWay = 3;                        querySaveReceivables.CreateTime = DateTime.Now;                    }                }                else                {                    querySaveReceivables.Currency = 836; //人民币                    querySaveReceivables.Rate = 1;                    querySaveReceivables.Price = 0;                    querySaveReceivables.ItemSumPrice = 0;                    querySaveReceivables.Remark = "";                    querySaveReceivables.CreateTime = DateTime.Now;                    querySaveReceivables.Count = 1;                }                _sqlSugar.Updateable(querySaveReceivables).ExecuteCommand();            }            else            {                if (overspList.Count() > 0)                {                    if (overspListGroup.Count() > 1)                    {                        querySaveReceivables = new Fin_ForeignReceivables                        {                            CreateTime = DateTime.Now,                            CreateUserId = dto.CreateUserId,                            Diid = dto.DiId,                            PriceName = "超支费用",                            AddingWay = 3,                            Count = 1,                            Currency = 836,                            ItemSumPrice = overspList.Sum(x => x.PriceSum * x.Coefficient * x.DayRate),                            Price = overspList.Sum(x => x.PriceSum * x.Coefficient * x.DayRate),                            Rate = 1,                            IsDel = 0,                            Remark = foreignReceivablesRemake,                            Unit = "团",                        };                    }                    else                    {                        querySaveReceivables = new Fin_ForeignReceivables                        {                            CreateTime = DateTime.Now,                            CreateUserId = dto.CreateUserId,                            Diid = dto.DiId,                            PriceName = "超支费用",                            AddingWay = 3,                            Count = 1,                            Currency = overspList[0].PriceCurrency,                            ItemSumPrice = overspList.Sum(x => x.PriceSum * x.Coefficient),                            Price = overspList.Sum(x => x.PriceSum * x.Coefficient),                            Rate = overspList[0].DayRate,                            IsDel = 0,                            Remark = foreignReceivablesRemake,                            Unit = "团",                        };                    }                }                _sqlSugar.Insertable(querySaveReceivables).ExecuteCommand();            }        }        #endregion    }}
 |