|
@@ -1,9 +1,15 @@
|
|
|
-using OASystem.Domain.Entities.Customer;
|
|
|
+using OASystem.Domain;
|
|
|
+using OASystem.Domain.Dtos.CRM;
|
|
|
+using OASystem.Domain.Entities.Customer;
|
|
|
+using OASystem.Domain.Entities.Resource;
|
|
|
+using OASystem.Domain.ViewModels.CRM;
|
|
|
using System;
|
|
|
+using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
+using System.Xml.Linq;
|
|
|
|
|
|
namespace OASystem.Infrastructure.Repositories.CRM
|
|
|
{
|
|
@@ -12,5 +18,261 @@ namespace OASystem.Infrastructure.Repositories.CRM
|
|
|
public NewClientDataRepository(SqlSugarClient sqlSugar) :
|
|
|
base(sqlSugar)
|
|
|
{ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 客户资料初识初始化
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="dto"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<Result> QueryNewClientData(NewClientDataQueryDto dto)
|
|
|
+ {
|
|
|
+ Result result = new Result() { Code = -2, Msg = "未知错误" };
|
|
|
+ try
|
|
|
+ {
|
|
|
+ List<int> NewClientDataId1= new List<int>();
|
|
|
+ List<int> NewClientDataId2 = new List<int>();
|
|
|
+ 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={0}", dto.Userid);
|
|
|
+ List<AscribedUser> ascribedUsers = await _sqlSugar.SqlQueryable<AscribedUser>(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={0}", dto.Business);
|
|
|
+ List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>(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<int> intStr=new List<int>();
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ dto.PageIndex = dto.PageIndex * dto.PageSize - dto.PageSize + 1;
|
|
|
+ dto.PageSize = 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,dto.PageIndex,dto.PageSize);
|
|
|
+ List<NewClientDataView> NewClientDataView = await _sqlSugar.SqlQueryable<NewClientDataView>(sqlNew).ToListAsync();
|
|
|
+ #region 下拉框初始化数据
|
|
|
+ //负责人下拉框
|
|
|
+ List<dynamic> _Users = new List<dynamic>();
|
|
|
+ List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
|
|
|
+ .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<dynamic> _Province = new List<dynamic>();
|
|
|
+ List<Sys_SetData> province = _sqlSugar.Queryable<Sys_SetData>()
|
|
|
+ .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<dynamic> _level = new List<dynamic>();
|
|
|
+ List<Sys_SetData> level = _sqlSugar.Queryable<Sys_SetData>()
|
|
|
+ .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<dynamic> _CustomerClass = new List<dynamic>();
|
|
|
+ List<Sys_SetData> CustomerClass = _sqlSugar.Queryable<Sys_SetData>()
|
|
|
+ .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<dynamic> _ServiceClass = new List<dynamic>();
|
|
|
+ List<Sys_SetData> ServiceClass = _sqlSugar.Queryable<Sys_SetData>()
|
|
|
+ .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> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
|
|
|
+ ("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> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
|
|
|
+ ("select d1.*,d2.Name from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId="+item.Id+"").ToListAsync();
|
|
|
+ item.AscribedDepartment = AscribedDepartment;
|
|
|
+ }
|
|
|
+ if (dto.PageSize == 0 && dto.PageIndex == 0)
|
|
|
+ {
|
|
|
+ var Data = new
|
|
|
+ {
|
|
|
+ ClientTableData= NewClientDataView,
|
|
|
+ Users = _Users,
|
|
|
+ Province = _Province,
|
|
|
+ level = _level,
|
|
|
+ CustomerClass = _CustomerClass,
|
|
|
+ ServiceClass = _ServiceClass
|
|
|
+ };
|
|
|
+ return result = new Result()
|
|
|
+ {
|
|
|
+ Code = 0,
|
|
|
+ Msg = "查询成功",
|
|
|
+ Data = Data,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ int count = NewClientDataView[0].countPage;
|
|
|
+ float totalPage = (float)count / dto.PageSize;//总页数
|
|
|
+ if (totalPage == 0) totalPage = 1;
|
|
|
+ else totalPage = (int)Math.Ceiling((double)totalPage);
|
|
|
+
|
|
|
+ List<NewClientDataView> NewClientView = new List<NewClientDataView>();
|
|
|
+ for (int i = 0; i < dto.PageSize; i++)
|
|
|
+ {
|
|
|
+ var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
|
|
|
+ if (RowIndex < NewClientDataView.Count)
|
|
|
+ {
|
|
|
+ NewClientView.Add(NewClientDataView[RowIndex]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var Data = new
|
|
|
+ {
|
|
|
+ ClientTableData = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientView },
|
|
|
+ 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 = "暂无数据!" };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ result = new Result() { Code = -2, Msg = "未知错误" };
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|
|
|
}
|