| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992 | 
							- using AutoMapper;
 
- using AutoMapper.Execution;
 
- using MySqlX.XDevAPI.Relational;
 
- using NPOI.OpenXmlFormats.Dml.Diagram;
 
- using NPOI.SS.Formula.Functions;
 
- using OASystem.Domain;
 
- using OASystem.Domain.Dtos;
 
- using OASystem.Domain.Dtos.CRM;
 
- using OASystem.Domain.Entities.Customer;
 
- using OASystem.Domain.Entities.Resource;
 
- using OASystem.Domain.Enums;
 
- using OASystem.Domain.ViewModels.CRM;
 
- using OASystem.Domain.ViewModels.QiYeWeChat;
 
- using SqlSugar;
 
- using System;
 
- using System.Collections;
 
- using System.Collections.Generic;
 
- using System.ComponentModel.Design;
 
- using System.Linq;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- using System.Xml.Linq;
 
- using static Google.Protobuf.Reflection.SourceCodeInfo.Types;
 
- using static OASystem.Domain.Dtos.CRM.NewClientDataQueryDto;
 
- namespace OASystem.Infrastructure.Repositories.CRM
 
- {
 
-     public class NewClientDataRepository : BaseRepository<Crm_NewClientData, NewClientDataView>
 
-     {
 
-         private readonly IMapper _mapper;
 
-         public NewClientDataRepository(SqlSugarClient sqlSugar, IMapper mapper) :
 
-          base(sqlSugar)
 
-         {
 
-             _mapper = mapper;
 
-         }
 
-         /// <summary>
 
-         /// 客户资料
 
-         /// 基础数据源
 
-         /// </summary>
 
-         /// <param name="dto"></param>
 
-         /// <returns></returns>
 
-         public async Task<Result> _Init(int portType)
 
-         {
 
-             Result result = new Result() { Code = -2, Msg = "未知错误" };
 
-             if (portType == 1 || portType == 2 || portType == 3)
 
-             {
 
-                 #region 下拉框初始化数据
 
-                 //负责人下拉框
 
-                 List<dynamic> _Users = new List<dynamic>();
 
-                 List<Sys_Users> users = GetNewExistClient();
 
-                 foreach (Sys_Users user in users)   
 
-                 {
 
-                     var data = new
 
-                     {
 
-                         Id = user.Id,
 
-                         Name = user.CnName
 
-                     };
 
-                     _Users.Add(data);
 
-                 };
 
-                 List<Sys_SetData> initData = _sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0).ToList();
 
-                 //客户级别数据
 
-                 List<dynamic> _level = new List<dynamic>();
 
-                 List<Sys_SetData> level = initData.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 = initData.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 = initData.Where(u => u.STid == 36 && u.IsDel == 0).ToList();
 
-                 foreach (Sys_SetData item in ServiceClass)
 
-                 {
 
-                     var data = new
 
-                     {
 
-                         Id = item.Id,
 
-                         Name = item.Name
 
-                     };
 
-                     _ServiceClass.Add(data);
 
-                 };
 
-                 //身份分类
 
-                 List<dynamic> _ProvinceClass = new List<dynamic>();
 
-                 List<Sys_SetData> ProvinceClass = initData.Where(u => u.STid == 42 && u.IsDel == 0).ToList();
 
-                 foreach (Sys_SetData item in ProvinceClass)
 
-                 {
 
-                     var data = new
 
-                     {
 
-                         Id = item.Id,
 
-                         Name = item.Name
 
-                     };
 
-                     _ProvinceClass.Add(data);
 
-                 };
 
-                 #endregion
 
-                 var data1 = new {
 
-                     Users = _Users,
 
-                     Level = _level,
 
-                     CustomerClass = _CustomerClass,
 
-                     ServiceClass= _ServiceClass,
 
-                     ProvinceClass = _ProvinceClass
 
-                 };
 
-                 return result = new Result()
 
-                 {
 
-                     Code = 0,
 
-                     Msg = "查询成功",
 
-                     Data = data1
 
-                 };
 
-             }
 
-             else
 
-             {
 
-                 result.Msg = string.Format("请传入有效的PortType参数!");
 
-             }
 
-             return result;
 
-         }
 
-         /// <summary>
 
-         /// 市场客户资料数据
 
-         /// 详情
 
-         /// </summary>
 
-         /// <param name="dto"></param>
 
-         /// <returns></returns>
 
-         public async Task<Result> _Details(int portType,int id)
 
-         {
 
-             Result result = new Result() { Code = -2, Msg = "未知错误" };
 
-             if (portType == 1 || portType == 2 || portType == 3)
 
-             {
 
-                 if (id < 0)
 
-                 {
 
-                     result.Msg = string.Format("请传入有效的Id参数!");
 
-                     return result;
 
-                 }
 
-                 string infoSql = string.Format(@" Select * From  Crm_NewClientData Where Isdel = 0 And Id = {0}", id);
 
-                 var info = await _sqlSugar.SqlQueryable<DetailsView>(infoSql).FirstAsync();
 
-                 if (info != null )
 
-                 {
 
-                     List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
 
-                            ("select u1.UsersId as UserId ,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId=" + info.Id + "   AND u1.ISDEL = 0").ToListAsync();
 
-                     info.AscribedUser = AscribedUser.Select(it => it.UserId).ToList();
 
-                     List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
 
-                   ("select  d2.Id,d2.Name,d1.NewClientDataId  from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId=" + info.Id + "  AND d1.ISDEL = 0").ToListAsync();
 
-                     info.AscribedDepartment = AscribedDepartment.Select(it => it.Id).ToList();
 
-                     result.Code = 0;
 
-                     result.Data = info;
 
-                 }
 
-             }
 
-             else
 
-             {
 
-                 result.Msg = string.Format("请传入有效的PortType参数!");
 
-             }
 
-             return result;
 
-         }
 
-         /// <summary>
 
-         /// 客户资料初识初始化
 
-         /// </summary>
 
-         /// <param name="dto"></param>
 
-         /// <returns></returns>
 
-         public async Task<Result> QueryNewClientData(NewClientDataQueryDto dto)
 
-         {
 
-             Result result = new Result() { Code = -2, Msg = "未知错误" };
 
-             try
 
-             {
 
-                 #region 交集
 
-                 List<int> NewClientDataId1 = new List<int>();
 
-                 List<int> NewClientDataId2 = new List<int>();
 
-                 string NewClientDataId = "";
 
-                 int state = 0;
 
-                 #region 负责人
 
-                 if (dto.OperationUserId != 21)
 
-                 {
 
-                     dto.Userid = dto.OperationUserId.ToString();
 
-                 }
 
-                 if (!string.IsNullOrWhiteSpace(dto.Userid))
 
-                 {
 
-                     string sql = string.Format(@"select u1.UsersId as UserId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and u1.UsersId in ({0})  and u1.IsDel = 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 = "暂无数据" };
 
-                     }
 
-                     state = -1;
 
-                 }
 
-                 #endregion
 
-                 #region 业务归属
 
-                 if (!string.IsNullOrWhiteSpace(dto.Business))
 
-                 {
 
-                     string sql = string.Format(@"select   d2.Id,d2.Name,d1.NewClientDataId   from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and d1.SetDataId in ({0}) and d1.isdel = 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 = "暂无数据" };
 
-                     }
 
-                     state = -1;
 
-                 }
 
-                 #endregion
 
-                 List<int> intList = new List<int>();
 
-                 if (NewClientDataId1.Count != 0 && NewClientDataId2.Count != 0)
 
-                 {
 
-                     intList = NewClientDataId1.Intersect(NewClientDataId2).ToList();
 
-                 }
 
-                 else if (NewClientDataId1.Count != 0)
 
-                 {
 
-                     intList = NewClientDataId1;
 
-                 }
 
-                 else if (NewClientDataId2.Count != 0)
 
-                 {
 
-                     intList = NewClientDataId2;
 
-                 }
 
-                 #endregion
 
-                 foreach (var item in intList)
 
-                 {
 
-                     NewClientDataId += item + ",";
 
-                 }
 
-                 if (!string.IsNullOrWhiteSpace(NewClientDataId))
 
-                 {
 
-                     NewClientDataId = NewClientDataId.Substring(0, NewClientDataId.Length - 1);
 
-                 }
 
-                 string sqlWhere = string.Empty;
 
-                 if (dto.PortType == 1)
 
-                 {
 
-                     #region 联系人条件
 
-                     if (!string.IsNullOrWhiteSpace(dto.Contact))
 
-                     {
 
-                         sqlWhere += string.Format(@" And s.Contact like '%{0}%'", dto.Contact);
 
-                     }
 
-                     #endregion
 
-                     #region 地区条件
 
-                     if (!string.IsNullOrWhiteSpace(dto.Location))
 
-                     {
 
-                         sqlWhere += string.Format(@" And s.Location like '%{0}%'", dto.Location);
 
-                     }
 
-                     #endregion
 
-                     #region 单位条件
 
-                     if (!string.IsNullOrWhiteSpace(dto.Client))
 
-                     {
 
-                         sqlWhere += string.Format(@" And s.Client like '%{0}%'", dto.Client);
 
-                     }
 
-                     #endregion
 
-                 }
 
-                 else if (dto.PortType == 2 || dto.PortType == 3)
 
-                 {
 
-                     sqlWhere += string.Format("And (Contact like '%{0}%' or Location  like '%{0}%' or Client like '%{0}%' )", dto.Client);
 
-                 }
 
-                 if (state == -1)
 
-                 {
 
-                     if (string.IsNullOrWhiteSpace(NewClientDataId))
 
-                     {
 
-                         NewClientDataId = "0";
 
-                     }
 
-                     sqlWhere += string.Format(@" And s.Id in({0})", NewClientDataId);
 
-                 }
 
-                 #region 地市州条件
 
-                 if (dto.Lvlid != 0)
 
-                 {
 
-                     sqlWhere += string.Format(@" And s.Lvlid={0}", dto.Lvlid);
 
-                 }
 
-                 #endregion
 
-                 #region 省域条件
 
-                 if (dto.Range != 0)
 
-                 {
 
-                     string setDataSql = "select * from Sys_SetData where STid = 33 and isdel = 0  ";
 
-                     switch (dto.Range)
 
-                     {
 
-                         case 419:
 
-                             setDataSql += " and (Name like '%四%川%' or Name like '%成%都%')";
 
-                             break;
 
-                         case 421:
 
-                             setDataSql += " and (Name like '%贵%州%' or Name like '%贵%阳%')";
 
-                             break;
 
-                         case 420:
 
-                             setDataSql += " and (Name like '%云%南%' or Name like '%昆%明%')";
 
-                             break;
 
-                         case 423:
 
-                             setDataSql += " and (Name like '%重庆%')";
 
-                             break;
 
-                         case 422:
 
-                             setDataSql += " and (Name like '%西%藏%' or Name like '%拉%萨%')";
 
-                             break;
 
-                         case 578:
 
-                             setDataSql += " and (Name like '%青%海%' or Name like '%西%宁%')";
 
-                             break;
 
-                         case 605:
 
-                             setDataSql += " and (Name like '%陕%西%' or Name like '%西%安%')";
 
-                             break;
 
-                         case 606:
 
-                             setDataSql += " and (Name like '%宁%夏%' or Name like '%银%川%')";
 
-                             break;
 
-                         case 625:
 
-                             setDataSql += " and (Name like '%甘%肃%' or Name like '%兰%州%')";
 
-                             break;
 
-                         case 634:
 
-                             setDataSql += " and (Name like '%新%疆%' or Name like '%乌%鲁%木%齐%')";
 
-                             break;
 
-                     }
 
-                     var RangeSetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(setDataSql).Select(x => x.Id).ToList();
 
-                     string lvlds = string.Join(',', RangeSetDataList).TrimEnd(',');
 
-                     if (!string.IsNullOrEmpty(lvlds))
 
-                     {
 
-                         sqlWhere += string.Format(@" And s.Lvlid in ({0}) ", lvlds);
 
-                     }
 
-                 }
 
-                 #endregion
 
-                 #region 客户类别
 
-                 if (dto.Category != 0)
 
-                 {
 
-                     sqlWhere += string.Format(@" And s.Category = {0}", dto.Category);
 
-                 }
 
-                 #endregion
 
-                 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> NewClientDataView = await _sqlSugar.SqlQueryable<NewClientDataView>(sqlNew).ToListAsync();
 
-                 foreach (var item in NewClientDataView)
 
-                 {
 
-                     Sys_SetData CategoryStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Category);
 
-                     if (CategoryStr != null)
 
-                     {
 
-                         item.CategoryStr = CategoryStr != null ? CategoryStr.Name : null;
 
-                     }
 
-                     Sys_SetData lvlStr = _sqlSugar.Queryable<Sys_SetData>().Single(it => it.Id == item.Lvlid);
 
-                     if (lvlStr != null)
 
-                     {
 
-                         item.LvlidStr = lvlStr != null ? lvlStr.Name : null;
 
-                     }
 
-                 }
 
-                 #region 下拉框初始化数据
 
-                 //负责人下拉框
 
-                 List<dynamic> _Users = new List<dynamic>();
 
-                 List<Sys_Users> users = GetNewExistClient();
 
-                 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 ServiceClass)
 
-                 {
 
-                     var data = new
 
-                     {
 
-                         Id = item.Id,
 
-                         Name = item.Name
 
-                     };
 
-                     _ServiceClass.Add(data);
 
-                 };
 
-                 #endregion
 
-                 var groupNumber = await QueryNumberGroups();
 
-                 if (NewClientDataView.Count > 0)
 
-                 {
 
-                     int count = NewClientDataView[0].countPage;
 
-                     float totalPage = (float)count / dto.PageSize;//总页数
 
-                     if (totalPage == 0) totalPage = 1;
 
-                     else totalPage = (int)Math.Ceiling((double)totalPage);
 
-                     if (dto.PortType == 1)
 
-                     {
 
-                         foreach (var item in NewClientDataView)
 
-                         {
 
-                             List<AscribedUser> AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
 
-                            ("select u1.UsersId as UserId ,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId=" + item.Id + "   AND u1.ISDEL = 0").ToListAsync();
 
-                             item.AscribedUser = AscribedUser;
 
-                             List<AscribedDepartment> AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
 
-                           ("select  d2.Id,d2.Name,d1.NewClientDataId  from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId=" + item.Id + "  AND d1.ISDEL = 0").ToListAsync();
 
-                             item.AscribedDepartment = AscribedDepartment;
 
-                         }
 
-                         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,
 
-                             groupNumber = groupNumber.Data,
 
-                         };
 
-                         return result = new Result()
 
-                         {
 
-                             Code = 0,
 
-                             Msg = "查询成功",
 
-                             Data = Data
 
-                         };
 
-                     }
 
-                     else if (dto.PortType == 2 || dto.PortType == 3)
 
-                     {
 
-                         List<NewClientDataAndroidIOSView> newClientDataIOSViews = new List<NewClientDataAndroidIOSView>();
 
-                         foreach (var item in NewClientDataView)
 
-                         {
 
-                             newClientDataIOSViews.Add(new NewClientDataAndroidIOSView()
 
-                             {
 
-                                 RowNumber = item.RowNumber,
 
-                                 Id = item.Id,
 
-                                 Client = item.Client,
 
-                                 Contact = item.Contact,
 
-                                 Job = item.Job,
 
-                                 Telephone = item.Telephone,
 
-                                 Location = item.Location,
 
-                             });
 
-                         }
 
-                         result = new Result()
 
-                         {
 
-                             Code = 0,
 
-                             Msg = "查询成功",
 
-                             Data = new { pageCount = count, totalPage = (int)totalPage, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = newClientDataIOSViews },
 
-                         };
 
-                     }
 
-                 }
 
-                 else
 
-                 {
 
-                     if (dto.PortType == 2 || dto.PortType == 3)
 
-                     {
 
-                         var Data = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView };
 
-                         result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
 
-                     } 
 
-                     else
 
-                     {
 
-                         var Data = new
 
-                         {
 
-                             ClientTableData = new { pageCount = 0, totalPage = 0, pageIndex = dto.PageIndex, pageSize = dto.PageSize, pageSource = NewClientDataView },
 
-                             Users = _Users,
 
-                             Province = _Province,
 
-                             level = _level,
 
-                             CustomerClass = _CustomerClass,
 
-                             ServiceClass = _ServiceClass,
 
-                             groupNumber = groupNumber.Data,
 
-                         };
 
-                         result = new Result() { Code = 0, Msg = "获取成功!", Data = Data };
 
-                     }
 
-                     
 
-                 }
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 result = new Result() { Code = -2, Msg = "未知错误" };
 
-             }
 
-             return result;
 
-         }
 
-         public Result QueryUserSelect()
 
-         {
 
-             Result result = new Result() { Code = -2, Msg = "未知错误" };
 
-             try
 
-             {
 
-                 //负责人下拉框
 
-                 List<dynamic> _Users = new List<dynamic>();
 
-                 List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
 
-                 .Where(u =>  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 = "未知错误" };
 
-             }
 
-             return result;
 
-         }
 
-         public async Task<Result> NewClientOp(NewClientOpDto dto)
 
-         {
 
-             Result result = new Result() { Code = -2, Msg = "未知错误" };
 
-             bool istrue = false;
 
-             int AddReturnId = -1;
 
-             
 
-             string BirthdayStr = string.Empty;
 
-             if (!string.IsNullOrWhiteSpace(dto.Birthday))
 
-             {
 
-                 DateTime Birthday = new DateTime();
 
-                 var isParse = DateTime.TryParse(dto.Birthday, out Birthday);
 
-                 BirthdayStr = isParse ? Birthday.ToString("yyyy-MM-dd") : "";
 
-             }
 
-             try
 
-             {
 
-                 BeginTran();
 
-                 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<Crm_NewClientData>(selectSql).FirstAsync();//查询是否存在
 
-                     if (NewClientData == null)
 
-                     {
 
-                         if (string.IsNullOrWhiteSpace(dto.PassportDate))
 
-                         {
 
-                             dto.PassportDate = null;
 
-                         }
 
-                         Crm_NewClientData _NewClientData = _mapper.Map<Crm_NewClientData>(dto);
 
-                         _NewClientData.Birthday = BirthdayStr;
 
-                         int id = await AddAsyncReturnId(_NewClientData); //添加市场客户资料表数据
 
-                         if (id == 0)
 
-                         {
 
-                             result = new Result() { Code = -1, Msg = "添加失败!" };
 
-                         }
 
-                         else
 
-                         {
 
-                             result = new Result() { Code = 0, Msg = "添加成功!",Data = id };
 
-                             istrue = true;
 
-                             AddReturnId = id;
 
-                             CommitTran();
 
-                         }
 
-                     }
 
-                     else
 
-                     {
 
-                         result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
 
-                     }
 
-                 }
 
-                 else if (dto.Status == 2)//修改
 
-                 {
 
-                     DateTime? PassportDate = null;
 
-                     try
 
-                     {
 
-                         PassportDate = DateTime.Parse(dto.PassportDate);
 
-                     }
 
-                     catch (Exception)
 
-                     {
 
-                         PassportDate = null;
 
-                     }
 
-                     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 = PassportDate,
 
-                         Job = dto.Job,
 
-                         Telephone = dto.Telephone,
 
-                         Phone = dto.Phone,
 
-                         Email = dto.Email,
 
-                         Location = dto.Location,
 
-                         Address = dto.Address,
 
-                         Birthday = BirthdayStr,
 
-                         OtherInfo = dto.Otherinfo,
 
-                         Wechat = dto.Wechat,
 
-                         Category = dto.Category,
 
-                         PreDele = dto.Predele,
 
-                         FinlishedDele = dto.FinlishedDele,
 
-                         Remark = dto.Remark,
 
-                     });
 
-                     if (res)
 
-                     {
 
-                         istrue = true;
 
-                         AddReturnId = dto.Id == 0 ? -1 : dto.Id;
 
-                         if (AddReturnId != -1)
 
-                         {
 
-                             await _sqlSugar.Updateable<Crm_ClientDataAndUser>().Where(x=>x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndUser()
 
-                             {
 
-                                 IsDel = 1,
 
-                                 DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
 
-                             }).ExecuteCommandAsync();
 
-                             await _sqlSugar.Updateable<Crm_ClientDataAndBusiness>().Where(x => x.NewClientDataId == AddReturnId).SetColumns(a => new Crm_ClientDataAndBusiness()
 
-                             {
 
-                                 IsDel = 1,
 
-                                 DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
 
-                             }).ExecuteCommandAsync();
 
-                         }
 
-                         result = new Result() { Code = 0, Msg = "修改成功!" };
 
-                     }
 
-                     else
 
-                     {
 
-                         result = new Result() { Code = -1, Msg = "修改失败!" };
 
-                     }
 
-                 }
 
-                 else
 
-                 {
 
-                     result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
 
-                 }
 
-                 if (istrue)
 
-                 {
 
-                     Adds<Crm_ClientDataAndUser>(dto.AscribedUser.Select(x => new Crm_ClientDataAndUser
 
-                     {
 
-                         CreateTime = DateTime.Now,
 
-                         CreateUserId = dto.CreateUserId,
 
-                         IsDel = 0,
 
-                         NewClientDataId = AddReturnId,
 
-                         usersId = x
 
-                     }).ToList());
 
-                     Adds<Crm_ClientDataAndBusiness>(dto.AscribedDepartment.Select(x => new Crm_ClientDataAndBusiness
 
-                     {
 
-                         CreateUserId = dto.CreateUserId,
 
-                         IsDel = 0,
 
-                         CreateTime = DateTime.Now,
 
-                         NewClientDataId = AddReturnId,
 
-                         SetDataId = x,
 
-                     }).ToList());
 
-                     CommitTran();
 
-                     result.Data = AddReturnId;
 
-                 }
 
-                 else
 
-                 {
 
-                     RollbackTran();
 
-                 }
 
-             }
 
-             catch (Exception)
 
-             {
 
-                 RollbackTran();
 
-                 result = new Result() { Code = -2, Msg = "未知错误" };
 
-             }
 
-             return result;
 
-         }
 
-         public async Task<Result> QueryNumberGroups()
 
-         {
 
-             Result result = new Result();
 
-             //preDeleAll 预计总量
 
-             //finlishedDeleAll 已出总量
 
-             DataTable preDeleAndfinlishedDeleAll = await GetDataTableAsync("select SUM(PreDele) as PreDeleAll ,SUM(FinlishedDele) as FinlishedDeleAll from Crm_NewClientData");
 
-             var preDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["PreDeleAll"].ToString();
 
-             var finlishedDeleAll = preDeleAndfinlishedDeleAll.Rows[0]["finlishedDeleAll"].ToString();
 
-             result.Code = 0;
 
-             result.Msg = "成功!";
 
-             result.Data = new
 
-             {
 
-                 preDeleAll,
 
-                 finlishedDeleAll
 
-             };
 
-             return result;
 
-         }
 
-         /// <summary>
 
-         /// 获取下拉列表数据和单条数据信息
 
-         /// </summary>
 
-         /// <param name="dto"></param>
 
-         public async Task<Result> QuerySelectAndSingleData(QuerySingleDto dto)
 
-         {
 
-             Result rest = new Result();
 
-             var QueryData = await GetAsync<Crm_NewClientData>(x => x.Id == dto.Id);
 
-             NewClientDataView MapQueryData = null;
 
-             if (QueryData != null)
 
-             {
 
-                 MapQueryData = _mapper.Map<NewClientDataView>(QueryData);
 
-                 MapQueryData.AscribedUser = await _sqlSugar.SqlQueryable<AscribedUser>
 
-         ("select u1.UsersId as UserId,u2.CnName,u1.NewClientDataId from Crm_ClientDataAndUser u1,Sys_Users u2 where u1.UsersId=u2.Id and NewClientDataId=" + dto.Id + " and u1.isdel = 0").ToListAsync();
 
-                 MapQueryData.AscribedDepartment = await _sqlSugar.SqlQueryable<AscribedDepartment>
 
-               ("select   d2.Id,d2.Name,d1.NewClientDataId   from Crm_ClientDataAndBusiness d1,Sys_SetData d2 where d1.SetDataId=d2.Id and NewClientDataId=" + dto.Id + " and d1.isdel = 0").ToListAsync();
 
-             }
 
-             #region 下拉框初始化数据
 
-             //负责人下拉框
 
-             List<dynamic> _Users = new List<dynamic>();
 
-             List<Sys_Users> users = _sqlSugar.Queryable<Sys_Users>()
 
-             .Where(u =>  u.IsDel == 0).ToList();
 
-             foreach (Sys_Users user in users)
 
-             {
 
-                 var data = new
 
-                 {
 
-                     Id = user.Id,
 
-                     Name = user.CnName
 
-                 };
 
-                 _Users.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 ServiceClass)
 
-             {
 
-                 var data = new
 
-                 {
 
-                     Id = item.Id,
 
-                     Name = item.Name
 
-                 };
 
-                 _ServiceClass.Add(data);
 
-             };
 
-             #endregion
 
-             rest.Code = 0;
 
-             rest.Data = new
 
-             {
 
-                 data = MapQueryData,
 
-                 Users = _Users,
 
-                 level = _level,
 
-                 CustomerClass = _CustomerClass,
 
-                 ServiceClass = _ServiceClass,
 
-             };
 
-             rest.Msg = "获取成功!";
 
-             return rest;
 
-         }
 
-         /// <summary>
 
-         /// 删除市场客户资料数据
 
-         /// </summary>
 
-         /// <param name="dto"></param>
 
-         /// <returns></returns>
 
-         public async Task<Result> DelNewClientData(DelBaseDto dto)
 
-         {
 
-             Result AcrionResult = new Result();
 
-             BeginTran();
 
-             var DBresult = await SoftDeleteByIdAsync<Crm_NewClientData>(dto.Id.ToString(), dto.DeleteUserId);
 
-             try
 
-             {
 
-                 if (DBresult)
 
-                 {
 
-                     AcrionResult.Code = 0;
 
-                     string sqlSet = $"isdel = 1, DeleteUserId = {dto.DeleteUserId} ,DeleteTime = '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'";
 
-                     string sql = $" update Crm_ClientDataAndUser set {sqlSet} where NewClientDataId = {dto.Id} ";
 
-                     await ExecuteCommandAsync(sql);
 
-                     sql = $" update Crm_ClientDataAndBusiness set {sqlSet} where NewClientDataId = {dto.Id} ";
 
-                     await ExecuteCommandAsync(sql);
 
-                     CommitTran();
 
-                     AcrionResult.Code = 0;
 
-                 }
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 RollbackTran();
 
-                 AcrionResult.Msg = ex.Message;
 
-                 AcrionResult.Code = -1;
 
-             }
 
-             return AcrionResult;
 
-         }
 
-         /// <summary>
 
-         /// 市场客户资料数据
 
-         /// 批量指派
 
-         /// </summary>
 
-         /// <param name="dto"></param>
 
-         /// <returns></returns>
 
-         public async Task<Result> _BatchAssignment(BatchAssignmentDto dto)
 
-         {
 
-             Result AcrionResult = new Result() { Code = -1,Msg="操作失败"};
 
-             if (dto.UserIdItem == null || dto.UserIdItem.Count < 1)
 
-             {
 
-                 AcrionResult.Msg = "用户ID集合不能为空!";
 
-                 return AcrionResult;
 
-             }
 
-             if (dto.ClientDataIdItem == null || dto.ClientDataIdItem.Count < 1)
 
-             {
 
-                 AcrionResult.Msg = "客户资料ID集合不能为空!";
 
-                 return AcrionResult;
 
-             }
 
-             List<Crm_ClientDataAndUser> _ClientDataAndUsers = new List<Crm_ClientDataAndUser>();
 
-             List<Crm_ClientDataAndUser> _ClientDataAndUsers1 = await _sqlSugar.Queryable<Crm_ClientDataAndUser>()
 
-                                                                               .Where(it =>
 
-                                                                                            it.IsDel == 0 &&
 
-                                                                                            dto.ClientDataIdItem.Contains(it.NewClientDataId) &&
 
-                                                                                            dto.UserIdItem.Contains(it.usersId)
 
-                                                                                      )
 
-                                                                               .ToListAsync();
 
-             foreach (var clientDataId in dto.ClientDataIdItem)
 
-             {
 
-                 foreach (var userId1 in dto.UserIdItem)
 
-                 {
 
-                     Crm_ClientDataAndUser _ClientDataAndUsers2 = _ClientDataAndUsers1.Where(it => it.NewClientDataId == clientDataId && 
 
-                                                                                                   it.usersId == userId1
 
-                                                                                             ).FirstOrDefault();
 
-                     if (_ClientDataAndUsers2 == null)
 
-                     {
 
-                         _ClientDataAndUsers.Add(new Crm_ClientDataAndUser()
 
-                         {
 
-                             CreateUserId = dto.UserId,
 
-                             NewClientDataId = clientDataId,
 
-                             usersId = userId1
 
-                         });
 
-                     }
 
-                 }
 
-             }
 
-             if (_ClientDataAndUsers.Count > 0)
 
-             {
 
-                 var adds = await _sqlSugar.Insertable(_ClientDataAndUsers).ExecuteCommandAsync();
 
-                 if (adds > 0)
 
-                 {
 
-                     #region 客户资料表操作记录 批量添加
 
-                     List<Crm_TableOperationRecord> _TableOperationRecords = new List<Crm_TableOperationRecord>();
 
-                     foreach (var item in _ClientDataAndUsers)
 
-                     {
 
-                         _TableOperationRecords.Add(
 
-                             new Crm_TableOperationRecord() { 
 
-                                 TableName = "Crm_TableOperationRecord",
 
-                                 PortType = dto.PortType,
 
-                                 OperationItem = OperationEnum.BatchAssignment,
 
-                                 DataId = item.NewClientDataId,
 
-                                 CreateUserId = dto.UserId,
 
-                                 CreateTime = DateTime.Now,
 
-                                 Remark = "",
 
-                                 IsDel = 0
 
-                             });
 
-                     }
 
-                     if (_TableOperationRecords.Count > 0)
 
-                     {
 
-                         await _sqlSugar.Insertable(_TableOperationRecords).ExecuteCommandAsync();
 
-                     }
 
-                     #endregion
 
-                     AcrionResult.Code = 0;
 
-                     return AcrionResult;
 
-                 }
 
-             }
 
-             return AcrionResult;
 
-         }
 
-         public List<Sys_Users> GetNewExistClient()
 
-         {
 
-             string sql = "SELECT  * FROM  Sys_Users su  where su.id in (\r\n\t select distinct usersid from Crm_ClientDataAndUser WHERE  IsDel  = 0 \r\n) \r\n";
 
-             return _sqlSugar.SqlQueryable<Sys_Users>(sql).ToList();
 
-         }
 
-     }
 
- }
 
 
  |