using AllBase; using AllBase.Helper; using AllBase.Model.Result; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Work.Services { /// /// 云商品数据服务 /// public class CloudGoodsService { private static string ServerUrl = "http://103.90.155.78:9001"; static CloudGoodsService() { if (EnvironmentHelper.IsDevelopment) { ServerUrl = "http://localhost:14312"; } } public static async Task> GetGoods(GoodsSearchRequest request) { var html = await GetHtml.PostJsonAsync(ServerUrl + "/api/CloudGoods/SearchGoods", request); var ret = JsonConvert.DeserializeObject>>(html); if (ret.Code != 200) { throw new Exception(ret.Msg); } return ret.Data; } public static string GetGoodsDetail(long goodsId) { var html = GetHtml.PostJson(ServerUrl + $"/api/CloudGoods/GetGoodsRawData?goodsId={goodsId}", ""); var ret = JsonConvert.DeserializeObject>(html); if (ret.Code != 200) { throw new Exception(ret.Msg); } return ret.Data; } #region user /// /// 搜索收藏 /// /// /// /// public static async Task> SearchFavorites(UserFavoritesGoodsSearchRequest request) { request.UserName = GetUserName(); var html = await GetHtml.PostJsonAsync(ServerUrl + "/api/CloudGoods/SearchFavorites", request); var ret = JsonConvert.DeserializeObject>>(html); if (ret.Code != 200) { throw new Exception(ret.Msg); } return ret.Data; } /// /// 收藏 /// /// /// /// public static async Task Favorite(List goodsIds) { var html = await GetHtml.PostJsonAsync(ServerUrl + $"/api/CloudGoods/AddFavorite", new { UserName = GetUserName(), GoodsIds = goodsIds, }); var ret = JsonConvert.DeserializeObject>(html); if (ret.Code != 200) { throw new Exception(ret.Msg); } return ret.Data; } /// /// 取消收藏 /// /// /// /// public static async Task RemoveFavorites(List goodsIds) { var html = await GetHtml.PostJsonAsync(ServerUrl + $"/api/CloudGoods/RemoveFavorites", new { UserName = GetUserName(), GoodsIds = goodsIds, }); var ret = JsonConvert.DeserializeObject>(html); if (ret.Code != 200) { throw new Exception(ret.Msg); } return ret.Data; } #endregion public static string GetUserName() { return string.IsNullOrEmpty(BaseClass.ParentUserName) ? BaseClass.UserName : BaseClass.ParentUserName; } } /// /// 商品搜索请求 /// public class GoodsSearchRequest { /// /// 标题(模糊搜索) /// public string Title { get; set; } = string.Empty; /// /// 最低价格(元) /// public long? MinPrice { get; set; } /// /// 最高价格(元) /// public long? MaxPrice { get; set; } /// /// 一级类目ID /// public int? CatId1 { get; set; } /// /// 二级类目ID /// public int? CatId2 { get; set; } /// /// 三级类目ID /// public int? CatId3 { get; set; } /// /// 四级类目ID /// public int? CatId4 { get; set; } /// /// 最低销量 /// public int? MinSoldQuantity { get; set; } /// /// 最高销量 /// public int? MaxSoldQuantity { get; set; } /// /// 平台类型 /// public PlatformType Platform { get; set; } /// /// 页码(默认1) /// public int PageIndex { get; set; } = 1; /// /// 每页数量(默认20) /// public int PageSize { get; set; } = 20; } public class UserFavoritesGoodsSearchRequest : GoodsSearchRequest { public string UserName { get; set; } } /// /// 商品搜索返回结果 /// public class GoodsSearchResponse { /// /// 商品列表 /// [JsonProperty("list")] public List List { get; set; } = new List(); /// /// 总记录数 /// [JsonProperty("total")] public int Total { get; set; } /// /// 当前页码 /// [JsonProperty("pageIndex")] public int PageIndex { get; set; } /// /// 每页数量 /// [JsonProperty("pageSize")] public int PageSize { get; set; } /// /// 总页数 /// [JsonProperty("totalPages")] public int TotalPages { get; set; } } public class CloudGoods : BindableBase { public bool IsSelected { get => GetValue(); set => SetValue(value); } /// /// 自增ID /// public long Id { get; set; } /// /// 平台类型 /// public byte Platform { get; set; } /// /// 商品编号 /// public long GoodsId { get; set; } /// /// 标题 /// public string Title { get; set; } = string.Empty; /// /// 描述 /// public string? Description { get; set; } /// /// 缩略图 /// public string? ThumbUrl { get; set; } /// /// 一级类目ID /// public int? CatId1 { get; set; } /// /// 二级类目ID /// public int? CatId2 { get; set; } /// /// 三级类目ID /// public int? CatId3 { get; set; } /// /// 四级类目ID /// public int? CatId4 { get; set; } /// /// 最低价快照(元),导入时计算 /// public decimal MinPrice { get; set; } /// /// 最高价快照(元),导入时计算 /// public decimal MaxPrice { get; set; } public string Price => $"{MinPrice / 100m:N2}"; /// /// 销量 /// public int SoldQuantity { get; set; } /// /// 状态:0正常 1已删除 /// public byte Status { get; set; } /// /// 创建时间 /// public DateTime CreatedTime { get; set; } /// /// 更新时间(自动更新) /// public DateTime UpdatedTime { get; set; } /// /// 索引同步状态:0未同步 1已同步 /// public byte IndexStatus { get; set; } } }