.NET经销商实战(七)——获取商品类型接口开发


1.新增返回值类

代码结构如下:

2.ProductTypeVo代码如下:

点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DealerPlatform.Service.ProductApp.Vo
{
	/// 
	/// 商品类型返回值
	/// 
	public class ProductTypeVo
	{
		public string TypeNo { get; set; }
		public string TypeName { get; set; }
	}
}

3.在service项目中找到ProductService类

新增一个方法:GetProductType
代码如下:

点击查看代码
public async Task> GetProductType()
		{
			return await Task.Run(() =>
						{
							var productType = ProductRepo.GetList().Where(s => !string.IsNullOrWhiteSpace(s.TypeName)).Select(s => new ProductTypeVo
							{
								TypeNo = s.TypeNo,
								TypeName = s.TypeName,
							}).Distinct().ToList();
							return productType;
						});
		}

4.在服务1接口层新增一个方法

代码如下:
Task> GetProductType();

5.在vo文件夹中新增一个类

点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DealerPlatform.Service.ProductApp.Vo
{
	public class BelongTypeVo
	{
		public string SysNo { get; set; }
		// public string BelongTypeNo { get; set; }
		public string BelongTypeName { get; set; }
	}
}

6.在ProductService中新增商品类型列表接口

代码如下:

点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using DealerPlatform.Core.Repository;
using DealerPlatform.Domain.GlobalDto;
using DealerPlatform.Domain.Models;
using DealerPlatform.Service.ProductApp.Dto;
using DealerPlatform.Service.ProductApp.Vo;

namespace DealerPlatform.Service.ProductApp
{
	public partial class ProductService : IProductService
	{
		public ProductService(
			IRepository productRepo,
			IRepository productSaleRepo,
			IRepository productSaleAreaDiffRepo,
			IRepository productPhotoRepo,
			IMapper mapper)
		{
			ProductRepo = productRepo;
			ProductSaleRepo = productSaleRepo;
			ProductSaleAreaDiffRepo = productSaleAreaDiffRepo;
			ProductPhotoRepo = productPhotoRepo;
			Mapper = mapper;
		}

		public IRepository ProductRepo { get; }
		public IRepository ProductSaleRepo { get; }
		public IRepository ProductSaleAreaDiffRepo { get; }
		public IRepository ProductPhotoRepo { get; }
		public IMapper Mapper { get; }

		/// 
		/// 获取商品数据
		/// 
		/// 
		public async Task> GetProductDto(string systemNo, PageWithSortDto dto)
		{
			dto.Sort ??= "ProductName";
			// int skipNum = (pageIndex - 1) * pageSize;

			// var products = (from p in (await ProductRepo.GetListAsync())
			// 				orderby p.GetType().GetProperty(sort).GetValue(p)
			// 				select p).Skip(skipNum).Take(pageSize).ToList();
			//获取商品主档信息
			var products = await ProductRepo.GetListAsync(dto, s => s.SysNo == systemNo);

			var dtos = Mapper.Map>(products);
			var productPhotos = await GetProductPhotosByProductNo(dtos.Select(s => s.ProductNo).ToArray());
			var productSales = await GetProductSalesByProductNo(dtos.Select(s => s.ProductNo).ToArray());
			dtos.ForEach(s =>
			{
				s.ProductPhoto = productPhotos.FirstOrDefault(c => c.ProductNo == s.ProductNo);
				s.ProductSale = productSales.FirstOrDefault(c => c.ProductNo == s.ProductNo);
			});
			//根据productId取到属性
			return dtos;
		}
		public async Task> GetProductType()
		{
			return await Task.Run(() =>
						{
							var productType = ProductRepo.GetList().Where(s => !string.IsNullOrWhiteSpace(s.TypeName)).Select(s => new ProductTypeVo
							{
								TypeNo = s.TypeNo,
								TypeName = s.TypeName,
							}).Distinct().ToList();
							return productType;
						});
		}
		/// 
		/// 商品类型查询列表
		/// 
		/// 
		public async Task> GetBelongTypesAsync()
		{
			var data = ProductRepo.GetQueryable().Select(s => new BelongTypeVo
			{
				SysNo = s.SysNo,
				BelongTypeName = s.BelongTypeName,
			}).Distinct();
			return data.ToList();
		}
	}
}

上面两个接口都需要在各自的接口中新增方法,在控制器中添加对应的方法,这里我就不过多介绍了,能看到这里的说明有基础
下一篇改造前端页面,将前端界面从vue2改到vue3

相关