using Com.FirmLib.Entity;
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 ProductDirectoryBll:BaseHttpBll
    {
        private ProductDirectoryBll() : base(@"/product/directory")
        {

        }
        /// <summary>
        /// 增加厂商与型号关联信息
        /// </summary>
        /// <param name="venderId">厂商id</param>
        /// <param name="typeIds">型号Ids</param>
        /// <returns></returns>
        public bool Add(string venderId,List<string> typeIds)
        {
            Uri uri = (new Uri(GetEndUri() + "/add", UriKind.RelativeOrAbsolute));
            JObject jObject = new JObject();
            jObject.Add("venderId", venderId);
             var typeIdsArray=JArray.FromObject(typeIds);
            jObject.Add("specId", typeIdsArray);
            // var result = PostJsonAdd(jObject);
            var result = PostJsonAsync(uri, jObject);
            var flag = HandlerResult(result, (jo) => {  }, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); });
            return flag;
        }
        /// <summary>
        /// 删除指定厂商关联型号信息
        /// </summary>
        /// <param name="venderId"></param>
        /// <param name="typeIds"></param>
        /// <returns></returns>
        public bool Delete(string venderId, List<string> typeIds)
        {
            JObject jObject = new JObject();
            jObject.Add("venderId", venderId);
            var typeIdsArray = JArray.FromObject(typeIds);
            jObject.Add("specId", typeIdsArray);
            var result = PostJsonDelete(jObject);
            var flag = HandlerResult(result, (jo) => { }, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); });
            return flag;
        }
        /// <summary>
        /// 获取厂商的关联商品信息
        /// </summary>
        /// <param name="venderId"></param>
        /// <returns></returns>
        public List<ProductTypeDetailItem> GetAllItems(string venderId)
        {
            List<ProductTypeDetailItem> items = new List<ProductTypeDetailItem>();
            JObject jObject = new JObject();
            jObject.Add("venderId", venderId);
            var result = PostJsonQuery(jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<ProductTypeDetailItem>>(listStr);
            });
            return items;
        }
    }
}
