Shisheng-10086

博客园 首页 新随笔 联系 订阅 管理

有导航属性:

//BusinessTrip    表
    public Guid CustomerId { get; set; }    // 客户ID

        public virtual Customer Customer { get; set; }
    var query = await BusinessTripRepository.GetQueryableAsync();
    query = query.Include(x => x.Customer);

    // 2. 应用筛选条件
    if (request.CustomerId != null&&request.CustomerId.HasValue)
    {
        query = query.Where(x => x.CustomerId == request.CustomerId);

    }
    if (request.TripId.HasValue)
    {
        query = query.Where(x => x.Id == request.TripId.Value);
    }
    if (!string.IsNullOrWhiteSpace(request.City))
    {
        query = query.Where(x => x.DestinationCity.Contains(request.City));
    }
    var totalCount = await query.CountAsync();
    // 4. 获取数据
    var items = await query
        .OrderByDescending(x => x.CreationTime)
        .Select(x => new GetBusinessTripResponse
    (
        Id: x.Id,
        CustomerId:x.CustomerId,
        CustomerName: x.Customer != null ? x.Customer.Name : string.Empty,
        CustomerContact: x.Customer != null ? x.Customer.Contact : string.Empty,
        TripName: x.TripName,
        DestinationCity: x.DestinationCity,
        DestinationAddress: x.DestinationAddress,
        Remark: x.remark,
        description: x.Description,
        Status: x.Status.ToString()))
                .ToListAsync();

    return  items;
    }

无导航属性

//准确说是有导航属性我还不会用的时候
var queryable = await customerPlanRepository.GetQueryableAsync();
var queryable1 = await customerRepository.GetQueryableAsync();

// 基础查询
var query = from plan in queryable
            join customer in queryable1 on plan.CustomerId equals customer.Id
            select new { Plan = plan, Customer = customer };
if (request.customerId != null)
{
    query = query.Where(x =>
        x.Plan.CustomerId==request.customerId);
}
// 处理 PlanNameOrCustomerName 参数
if (!request.PlanNameOrCustomerName.IsNullOrWhiteSpace())
{
    var searchTerm = request.PlanNameOrCustomerName.Trim();
    query = query.Where(x =>
        x.Plan.PlanName.Contains(searchTerm) ||
        x.Customer.Name.Contains(searchTerm));
}

// 处理 Status 参数
if (!request.Status.IsNullOrWhiteSpace()&& request.customerId != null)
{
    if (Enum.TryParse<CustomerPlanStatus>(request.Status, true, out var customerPlanStatus))
    {
        query = query.Where(x => x.Plan.Status == customerPlanStatus);
    }
    else
    {
        // 如果状态参数无效,可以选择返回空列表或所有数据
        // 这里选择返回空列表,因为参数无效
        return new List<GetPlanResponse>();
    }
}
else
{
    query=query.Where(x => x.Plan.Status != CustomerPlanStatus.已完成 && x.Plan.Status != CustomerPlanStatus.已取消);
}

    // 执行查询获取结果
    var result = await AsyncExecuter.ToListAsync(query);

// 如果没有结果,直接返回空列表
if (!result.Any())
{
    return new List<GetPlanResponse>();
}

// 收集所有需要查询的用户ID
var userIds = new HashSet<Guid>();

foreach (var item in result)
{
    if (item.Plan.CreatorId.HasValue)
        userIds.Add(item.Plan.CreatorId.Value);

    if (item.Plan.BindPlanPersonId.HasValue)
        userIds.Add(item.Plan.BindPlanPersonId.Value);
}

// 批量查询用户信息
var users = await userManager.Users
    .Where(u => userIds.Contains(u.Id))
    .Select(u => new { u.Id, u.UserName })
    .ToListAsync();

var userDict = users.ToDictionary(u => u.Id, u => u.UserName);

// 排序逻辑
// 1. 先按状态优先级排序(未受理 > 已受理 > 已报价 > ...)
// 2. 然后按客户重要性( Customer 有 Importance 字段)
// 3. 然后按是否加急(Customer 有 Urgent 字段)
// 4. 最后按创建时间

var sortedResult = result
    .OrderByDescending(x => GetStatusPriority(x.Plan.Status))  // 状态优先级
    .ThenByDescending(x => x.Customer.Importance)              // 客户重要性(值越大越重要)
    .ThenByDescending(x => x.Customer.Urgent)                  // 是否加急(true > false)
    .ThenByDescending(x => x.Plan.CreationTime)                // 创建时间(最新的在前)
    .ToList();

return sortedResult
    .Select(x => new GetPlanResponse(
        Id: x.Plan.Id,
        CustomerId: x.Plan.CustomerId,
        CustomerName: x.Customer.Name,
        PlanName: x.Plan.PlanName,
        Version: x.Plan.Version,
        Description: x.Plan.Description ?? string.Empty,
        Status: x.Plan.Status.ToString(),
        Frame: x.Plan.Frame,
        Venue: x.Plan.Venue,
        //storageCAD: x.Plan.StorageCAD,
        storageBase: x.Plan.StorageBase,
        //storagePDF: x.Plan.StoragePDF,
        PlanCreatorName: x.Plan.CreatorId.HasValue &&
                       userDict.TryGetValue(x.Plan.CreatorId.Value, out var creatorName)
            ? creatorName
            : null,
        PlanBindPersonName: x.Plan.BindPlanPersonId.HasValue &&
                          userDict.TryGetValue(x.Plan.BindPlanPersonId.Value, out var bindPersonName)
            ? bindPersonName
            : null,
        LastUpdatedTime: x.Plan.LastUpdatedTime,
        CreationTime: x.Plan.CreationTime,
        // 添加客户相关信息用于排序显示(可选)
        CustomerImportant: x.Customer.Importance,
        CustomerIsUrgent: x.Customer.Urgent
    ))
    .ToList();

 

posted on 2026-01-14 16:14  零点10086  阅读(1)  评论(0)    收藏  举报