using AutoMapper; using AutoMapper.Execution; using OASystem.Domain; using OASystem.Domain.Dtos.CRM; using OASystem.Domain.Entities.Customer; using OASystem.Domain.Entities.Resource; using OASystem.Domain.ViewModels.CRM; using SqlSugar; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; using static OASystem.Domain.Dtos.CRM.NewClientDataQueryDto; namespace OASystem.Infrastructure.Repositories.CRM { public class NewClientDataRepository : BaseRepository { private readonly IMapper _mapper; public NewClientDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar) { _mapper= mapper; } /// /// 客户资料初识初始化 /// /// /// public async Task QueryNewClientData(NewClientDataQueryDto dto) { Result result = new Result() { Code = -2, Msg = "未知错误" }; try { if (dto.PortType==1) { List NewClientDataId1= new List(); List NewClientDataId2 = new List(); string NewClientDataId = ""; if (dto.Userid != 0) { string sql = string.Format(@"select u1.UsersId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and u1.UsersId in ({0})", dto.Userid); List ascribedUsers = await _sqlSugar.SqlQueryable(sql).ToListAsync(); if (ascribedUsers.Count!=0) { foreach (var ascribedUser in ascribedUsers) { if (ascribedUser.NewClientDataId != 0) { NewClientDataId1.Add(ascribedUser.NewClientDataId); } } } else { result = new Result() { Code = -1, Msg = "暂无数据" }; } } if (dto.Business != 0) { string sql = string.Format(@"select d1.*,d2.Name from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and d1.SetDataId in ({0})", dto.Business); List AscribedDepartment = await _sqlSugar.SqlQueryable(sql).ToListAsync(); if (AscribedDepartment.Count != 0) { foreach (var item in AscribedDepartment) { if (item.NewClientDataId!= 0) { NewClientDataId2.Add(item.NewClientDataId); } } } else { result = new Result() { Code = -1, Msg = "暂无数据" }; } } List intStr=new List(); if (NewClientDataId1.Count!=0 && NewClientDataId2.Count != 0) { intStr = NewClientDataId1.Intersect(NewClientDataId2).ToList(); }else if (NewClientDataId1.Count != 0) { intStr = NewClientDataId1; }else if (NewClientDataId2.Count != 0) { intStr = NewClientDataId2; } foreach (var item in intStr) { NewClientDataId += item + ","; } if (!string.IsNullOrWhiteSpace(NewClientDataId)) { NewClientDataId = NewClientDataId.Substring(0, NewClientDataId.Length - 1); } string sqlWhere = string.Empty; if (!string.IsNullOrWhiteSpace(dto.Contact)) { sqlWhere += string.Format(@" And s.Contact like '%{0}%'", dto.Contact); } if (!string.IsNullOrWhiteSpace(dto.Location)) { sqlWhere += string.Format(@" And s.Location like '%{0}%'", dto.Location); } if (!string.IsNullOrWhiteSpace(dto.Client)) { sqlWhere += string.Format(@" And s.Client like '%{0}%'", dto.Client); } if (!string.IsNullOrWhiteSpace(NewClientDataId)) { sqlWhere += string.Format(@" And s.Id in({0})", NewClientDataId); } //if (!string.IsNullOrWhiteSpace(dto.Weight.ToString())) //{ // sqlWhere += string.Format(@" And Weight )", dto.Weight); //} if (dto.Lvlid!=0) { sqlWhere += string.Format(@" And s.Lvlid={0}",dto.Lvlid); } sqlWhere += string.Format(@" And s.Lvlid=s1.Id And s.IsDel={0} ", 0); if (!string.IsNullOrEmpty(sqlWhere.Trim())) { Regex r = new Regex("And"); sqlWhere = r.Replace(sqlWhere, "Where", 1); } int pIndex = dto.PageIndex * dto.PageSize - dto.PageSize + 1; int pSize = dto.PageIndex * dto.PageSize; string sqlNew = string.Format(@"select (SELECT COUNT(1) FROM Crm_NewClientData s left Join Sys_SetData s1 On s.Lvlid=s1.Id {0}) AS countPage,* from (select ROW_NUMBER() over(order by s.CreateTime desc) RowNumber,s.*,s1.Name as 'LvlName' from Crm_NewClientData s left Join Sys_SetData s1 On s.Lvlid=s1.Id {0}) as co where RowNumber between {1} and {2} ", sqlWhere, pIndex, pSize); List NewClientDataView = await _sqlSugar.SqlQueryable(sqlNew).ToListAsync(); foreach (var item in NewClientDataView) { Sys_SetData CategoryStr = _sqlSugar.Queryable().Single(it => it.Id == item.Category); item.CategoryStr = CategoryStr.Name; Sys_SetData lvlStr = _sqlSugar.Queryable().Single(it => it.Id == item.Lvlid); item.LvlidStr = lvlStr.Name; } #region 下拉框初始化数据 //负责人下拉框 List _Users = new List(); List users = _sqlSugar.Queryable() .Where(u => u.CnName == "张海麟" || u.CnName == "安宁" || u.CnName == "李彩娟" || u.CnName == "舒庆" || u.CnName == "李媛媛" && u.IsDel == 0).ToList(); foreach (Sys_Users user in users) { var data = new { Id = user.Id, Name = user.CnName }; _Users.Add(data); }; //省域数据 List _Province = new List(); List province = _sqlSugar.Queryable() .Where(u => u.STid == 42 && u.IsDel == 0).ToList(); foreach (Sys_SetData item in province) { var data = new { Id = item.Id, Name = item.Name }; _Province.Add(data); }; //客户级别数据 List _level = new List(); List level = _sqlSugar.Queryable() .Where(u => u.STid == 33 && u.IsDel == 0).ToList(); foreach (Sys_SetData item in level) { var data = new { Id = item.Id, Name = item.Name }; _level.Add(data); }; //客户类别 List _CustomerClass = new List(); List CustomerClass = _sqlSugar.Queryable() .Where(u => u.STid == 37 && u.IsDel == 0).ToList(); foreach (Sys_SetData item in CustomerClass) { var data = new { Id = item.Id, Name = item.Name }; _CustomerClass.Add(data); }; //业务分类 List _ServiceClass = new List(); List ServiceClass = _sqlSugar.Queryable() .Where(u => u.STid == 36 && u.IsDel == 0).ToList(); foreach (Sys_SetData item in province) { var data = new { Id = item.Id, Name = item.Name }; _ServiceClass.Add(data); }; #endregion if (NewClientDataView.Count!=0) { foreach (var item in NewClientDataView) { List AscribedUser = await _sqlSugar.SqlQueryable ("select u1.UsersId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId="+ item .Id+ "").ToListAsync(); item.AscribedUser = AscribedUser; List AscribedDepartment = await _sqlSugar.SqlQueryable ("select d1.*,d2.Name from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId="+item.Id+"").ToListAsync(); item.AscribedDepartment = AscribedDepartment; } int count = NewClientDataView[0].countPage; float totalPage = (float)count / dto.PageSize;//总页数 if (totalPage == 0) totalPage = 1; else totalPage = (int)Math.Ceiling((double)totalPage); var Data = new { ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView }, Users = _Users, Province = _Province, level = _level, CustomerClass = _CustomerClass, ServiceClass = _ServiceClass }; return result = new Result() { Code = 0, Msg = "查询成功", Data = Data }; } else { result = new Result() { Code = -1, Msg = "暂无数据!" }; } } else if(dto.PortType==2) { List NewClientDataId1 = new List(); List NewClientDataId2 = new List(); string NewClientDataId = ""; if (dto.Userid != 0) { string sql = string.Format(@"select u1.UsersId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and u1.UsersId in ({0})", dto.Userid); List ascribedUsers = await _sqlSugar.SqlQueryable(sql).ToListAsync(); if (ascribedUsers.Count != 0) { foreach (var ascribedUser in ascribedUsers) { if (ascribedUser.NewClientDataId != 0) { NewClientDataId1.Add(ascribedUser.NewClientDataId); } } } else { result = new Result() { Code = -1, Msg = "暂无数据" }; } } if (dto.Business != 0) { string sql = string.Format(@"select d1.*,d2.Name from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and d1.SetDataId in({0})", dto.Business); List AscribedDepartment = await _sqlSugar.SqlQueryable(sql).ToListAsync(); if (AscribedDepartment.Count != 0) { foreach (var item in AscribedDepartment) { if (item.NewClientDataId != 0) { NewClientDataId2.Add(item.NewClientDataId); } } } else { result = new Result() { Code = -1, Msg = "暂无数据" }; } } List intStr = new List(); if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0) { intStr = NewClientDataId1.Intersect(NewClientDataId2).ToList(); } else if (NewClientDataId1.Count != 0) { intStr = NewClientDataId1; } else if (NewClientDataId2.Count != 0) { intStr = NewClientDataId2; } foreach (var item in intStr) { NewClientDataId += item + ","; } if (!string.IsNullOrWhiteSpace(NewClientDataId)) { NewClientDataId = NewClientDataId.Substring(0, NewClientDataId.Length - 1); } string sqlWhere = string.Empty; if (!string.IsNullOrWhiteSpace(dto.Contact)) { sqlWhere += string.Format(@" And s.Contact like '%{0}%'", dto.Contact); } if (!string.IsNullOrWhiteSpace(dto.Location)) { sqlWhere += string.Format(@" And s.Location like '%{0}%'", dto.Location); } if (!string.IsNullOrWhiteSpace(dto.Client)) { sqlWhere += string.Format(@" And s.Client like '%{0}%'", dto.Client); } if (!string.IsNullOrWhiteSpace(NewClientDataId)) { sqlWhere += string.Format(@" And s.Id in({0})", NewClientDataId); } //if (!string.IsNullOrWhiteSpace(dto.Weight.ToString())) //{ // sqlWhere += string.Format(@" And Weight )", dto.Weight); //} if (dto.Lvlid != 0) { sqlWhere += string.Format(@" And s.Lvlid={0}", dto.Lvlid); } sqlWhere += string.Format(@" And s.Lvlid=s1.Id And s.IsDel={0} ", 0); if (!string.IsNullOrEmpty(sqlWhere.Trim())) { Regex r = new Regex("And"); sqlWhere = r.Replace(sqlWhere, "Where", 1); } int pIndex= dto.PageIndex * dto.PageSize - dto.PageSize + 1; int pSize = dto.PageIndex * dto.PageSize; string sqlNew = string.Format(@"select (SELECT COUNT(1) FROM Crm_NewClientData s left Join Sys_SetData s1 On s.Lvlid=s1.Id {0}) AS countPage,* from (select ROW_NUMBER() over(order by s.CreateTime desc) RowNumber,s.*,s1.Name as 'LvlName' from Crm_NewClientData s left Join Sys_SetData s1 On s.Lvlid=s1.Id {0}) as co where RowNumber between {1} and {2} ", sqlWhere, pIndex, pSize); List NewClientDataView = await _sqlSugar.SqlQueryable(sqlNew).ToListAsync(); foreach (var item in NewClientDataView) { Sys_SetData CategoryStr = _sqlSugar.Queryable().Single(it => it.Id == item.Category); item.CategoryStr = CategoryStr.Name; Sys_SetData lvlStr = _sqlSugar.Queryable().Single(it => it.Id == item.Lvlid); item.LvlidStr = lvlStr.Name; } int count = NewClientDataView[0].countPage; float totalPage = (float)count / dto.PageSize;//总页数 if (totalPage == 0) totalPage = 1; else totalPage = (int)Math.Ceiling((double)totalPage); result = new Result() { Code = 0, Msg = "查询成功", Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView }, }; } } catch (Exception ex) { result = new Result() { Code = -2, Msg = "未知错误" }; throw; } return result; } public async Task QueryUserSelect() { Result result = new Result() { Code = -2, Msg = "未知错误" }; try { //负责人下拉框 List _Users = new List(); List users = _sqlSugar.Queryable() .Where(u => u.CnName == "张海麟" || u.CnName == "安宁" || u.CnName == "李彩娟" || u.CnName == "舒庆" || u.CnName == "李媛媛" && u.IsDel == 0).ToList(); foreach (Sys_Users user in users) { var data = new { Id = user.Id, Name = user.CnName }; _Users.Add(data); }; if (_Users.Count==0) { result = new Result() { Code = -1, Msg = "暂无数据" }; } result = new Result() { Code = 0, Msg = "查询成功!",Data=_Users }; } catch (Exception) { result = new Result() { Code = -2, Msg = "未知错误" }; throw; } return result; } public async Task NewClientOp(NewClientOpDto dto) { Result result = new Result() { Code = -2, Msg = "未知错误" }; try { if (dto.Status == 1)//添加 { string selectSql = string.Format(@"select * from Crm_NewClientData where Client='{0}' and Contact='{1}' And IsDel={2}" , dto.Client,dto.Contact,0); var NewClientData = await _sqlSugar.SqlQueryable(selectSql).FirstAsync();//查询是否存在 if (NewClientData != null) { return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" }; } else//不存在,可添加 { Crm_NewClientData _NewClientData = _mapper.Map(dto); int id = await AddAsyncReturnId(_NewClientData); 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 Crm_NewClientData { Number=dto.Number, Lvlid= dto.Lvlid, Client= dto.Client, Weight= dto.Weight, ClientShort= dto.Clientshort, Contact= dto.Contact, Gender= dto.Gender, Passport= dto.Passport, PassportDate= dto.PassportDate, Job= dto.Job, Telephone= dto.Telephone, Phone= dto.Phone, Email= dto.Email, Location= dto.Location, Address= dto.Address, Birthday= dto.Birthday, OtherInfo= dto.Otherinfo, Wechat= dto.Wechat, Category= dto.Category, PreDele= dto.Predele, FinlishedDele= dto.FinlishedDele, Remark= dto.Remark, }); if (!res) { return result = new Result() { Code = -1, Msg = "修改失败!" }; } return result = new Result() { Code = 0, Msg = "修改成功!" }; } else { return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" }; } } catch (Exception) { result = new Result() { Code = -2, Msg = "未知错误" }; throw; } return result; } } }