예제 #1
0
        /// <summary>
        /// 获取T_BM_CustomerFile主表数据数据
        /// </summary>
        /// <param name="requestObject">Get请求参数</param>
        /// <param name="currentUser"></param>
        /// <returns>返回响应结果对象,包括响应代码,查询操作结果</returns>
        public async Task <ResponseObject <List <TBMCustomerFileQueryModel> > > GetMainListAsync(RequestGet requestObject, CurrentUser currentUser)
        {
            try
            {
                List <TBMCustomerFileQueryModel> queryData = new List <TBMCustomerFileQueryModel>(); //查询结果集对象
                RefAsync <int> totalNumber = -1;                                                     //总记录数
                var            query       = _db.Instance.Queryable <TBMCustomerFileDbModel, TBMCustomerContactDbModel>((t, t1) => new object[] {
                    JoinType.Left, t1.CustomerId == t.ID
                }).Where((t, t1) => t.CompanyId == currentUser.CompanyID && t.DeleteFlag == false);


                //数据字典
                List <TBMDictionaryCacheModel> dics  = BasicCacheGet.GetDic(currentUser);
                List <ChinaAreaRecord>         Areas = BasicCacheGet.GetChinaArea(currentUser);



                //查询条件
                if (requestObject.QueryConditions != null && requestObject.QueryConditions.Count > 0)
                {
                    var customerConditions1 = requestObject.QueryConditions.Where(p => p.Column.ToLower() == "contactname").ToList();
                    if (customerConditions1 != null && customerConditions1.Count() > 0)
                    {
                        var conditionals = SqlSugarUtil.GetConditionalModels(customerConditions1);
                        foreach (ConditionalModel item in conditionals)
                        {
                            item.FieldName = $"t1.{item.FieldName}";
                        }
                        query.Where(conditionals);
                    }

                    var customerConditions2 = requestObject.QueryConditions.Where(p => p.Column.ToLower() != "contactname").ToList();
                    if (customerConditions2 != null && customerConditions2.Count() > 0)
                    {
                        var conditionals = SqlSugarUtil.GetConditionalModels(customerConditions2);
                        foreach (ConditionalModel item in conditionals)
                        {
                            item.FieldName = $"t.{item.FieldName}";
                        }
                        query.Where(conditionals);
                    }
                }

                //排序条件
                if (requestObject.OrderByConditions != null && requestObject.OrderByConditions.Count > 0)
                {
                    foreach (var item in requestObject.OrderByConditions)
                    {
                        var exp = SqlSugarUtil.GetOrderByLambda <TBMCustomerFileDbModel>(item.Column);
                        if (exp == null)
                        {
                            continue;
                        }
                        if (item.Condition.ToLower() != "asc" &&
                            item.Condition.ToLower() != "desc")
                        {
                            continue;
                        }
                        query.OrderBy($"{item.Column} {item.Condition}");
                    }
                }

                var query1 = query.Select((t, t1) => t).Distinct();

                List <TBMCustomerFileDbModel> queryDataTemp = new List <TBMCustomerFileDbModel>();
                //执行查询
                if (requestObject.IsPaging)
                {
                    queryDataTemp = await query1
                                    .Where(t => !t.DeleteFlag)
                                    .Select((t) => t)
                                    .ToPageListAsync(requestObject.PageIndex, requestObject.PageSize, totalNumber);
                }
                else
                {
                    queryDataTemp = await query1
                                    .Where(t => !t.DeleteFlag)
                                    .Select(t => t)
                                    .ToListAsync();
                }


                queryDataTemp.ForEach((x) =>
                {
                    var itemEnity       = ExpressionGenericMapper <TBMCustomerFileDbModel, TBMCustomerFileQueryModel> .Trans(x);
                    itemEnity.ChildList = new List <TBMCustomerContactQueryModel>();
                    foreach (var citem in x.Child.OrderBy(p => p.Priority))
                    {
                        var citemEnity = ExpressionGenericMapper <TBMCustomerContactDbModel, TBMCustomerContactQueryModel> .Trans(citem);
                        itemEnity.ChildList.Add(citemEnity);
                    }

                    var item = dics.Where(p => p.ID == x.CustomerTypeId).FirstOrDefault();
                    if (item != null)
                    {
                        itemEnity.CustomerTypeName = item.DicValue;
                    }

                    if (x.IndustryId != null)
                    {
                        var industry = dics.Where(p => p.ID == x.IndustryId).FirstOrDefault();
                        if (x.IndustryId != null)
                        {
                            itemEnity.IndustryName = industry?.DicValue;
                        }
                    }

                    if (!string.IsNullOrEmpty(x.City))
                    {
                        var cityEntity     = Areas.Where(p => p.Code == x.City).FirstOrDefault();
                        itemEnity.CityName = cityEntity?.FullName;
                    }

                    queryData.Add(itemEnity);
                });


                //返回执行结果
                return(ResponseUtil <List <TBMCustomerFileQueryModel> > .SuccessResult(queryData, totalNumber));
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return(ResponseUtil <List <TBMCustomerFileQueryModel> > .FailResult(null, ex.Message));
            }
        }