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

namespace Com.FirmLib.Bll
{
    /// <summary>
    /// 产品逻辑操作
    /// </summary>
    public class ProductBll : BaseHttpBll
    {
        private ProductBll():base(@"/manufacturer/product")
        {

        }
        /// <summary>
        /// 增加品牌
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Add(ProductItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("venderId", item.ManufacturerId);
            jObject.Add("brandId", item.BrandId);
            jObject.Add("name", item.Name);
            jObject.Add("eqFamily", item.FamilyCode);
            var result = PostJsonAdd(jObject);
            return HandlerResult(result, (jo) => { item.Id = jo["id"].ToString(); }, jo => CatchErrorMessage(jo));        
        }

        /// <summary>
        /// 获取指定品牌的产品集合
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public List<ProductItem> GetAllItems(string brandId)
        {
            List<ProductItem> items = new List<ProductItem>();
            JObject jObject = new JObject ();
            jObject.Add("brandId", brandId);
            jObject.Add("orderDesc", true);
            var result = PostJsonQuery(jObject);
             HandlerResult(result, (jo) =>
             {
                 var listStr = jo["content"].ToString();
                 items=JsonConvert.DeserializeObject<List<ProductItem>>(listStr);
             }, jo => CatchErrorMessage(jo));        
            return items;
        }
        /// <summary>
        /// 修改生品牌
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Update(ProductItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("productId", item.Id);
            jObject.Add("brandId", item.Id);
            jObject.Add("venderId", item.ManufacturerId);
            jObject.Add("name", item.Name);
            jObject.Add("eqFamily", item.FamilyCode);
            var result = PostJsonUpdate(jObject);
            return HandlerResult(result,null,jo => CatchErrorMessage(jo));              
        }

        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Delete(ProductItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("productId", item.Id);
            //jObject.Add("brandId", item.Id);
            //jObject.Add("venderId", item.ManufacturerId);
            var result = PostJsonDelete(jObject);
            return HandlerResult(result,null,jo => CatchErrorMessage(jo));               
        }
    }
}
