using Com.FirmLib.Common;
using Com.FirmLib.Entity;
using FWindSoft.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Com.FirmLib.Bll
{
    /// <summary>
    /// 操作产品类型
    /// </summary>
    public  class ProductTypeBll : BaseHttpBll
    {
        private ProductTypeBll() : base(@"/manufacturer/specification")
        {

        }
        /// <summary>
        /// 增加增加产品类型
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Add(ProductTypeItem item)
        {
            JObject jObject = new JObject();
            jObject.AddCheckNull("venderId", item.ManufacturerId);
            jObject.AddCheckNull("brandId", item.BrandId);
            jObject.Add("productId", item.ProductId);
            jObject.Add("name", item.Name);
            jObject.Add("infos", item.Parameters.ConvertToJObject());
            var result = PostJsonAdd(jObject);
            var flag = HandlerResult(result, (jo) => { item.Id = jo["id"].ToString(); }, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); });
            return flag;
        }

        /// <summary>
        /// 获取指定产品的类型
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public List<ProductTypeItem> GetAllItems(string productId)
        {
            List<ProductTypeItem> items = new List<ProductTypeItem>();
            JObject jObject = new JObject();
            jObject.Add("productId", productId);
            jObject.Add("orderDesc", true);
            var result = PostJsonQuery(jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();

                items = JsonConvert.DeserializeObject<List<ProductTypeItem>>(listStr);
            });
            return items;
        }
        /// <summary>
        /// 修改产品类型
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Update(ProductTypeItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("specificationId", item.Id);
            jObject.AddCheckNull("venderId", item.ManufacturerId);
            jObject.AddCheckNull("brandId", item.BrandId);
            jObject.AddCheckNull("productId", item.ProductId);
            jObject.AddCheckNull("name", item.Name);
            jObject.Add("infos", item.Parameters.ConvertToJObject());
            var result = PostJsonUpdate(jObject);
            return HandlerResult(result, null, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); });
        }

        /// <summary>
        /// 删除产品类型
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Delete(ProductTypeItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("specificationId", item.Id);

            jObject.AddCheckNull("productId", item.ProductId);
            jObject.AddCheckNull("brandId", item.BrandId);
            jObject.AddCheckNull("venderId", item.ManufacturerId);
  
            var result = PostJsonDelete(jObject);
            return HandlerResult(result, null, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); });
        }
      
    }
}
