﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using LJH.Inventory.BusinessModel;
using LJH.Inventory.BusinessModel.SearchCondition;
using LJH.GeneralLibrary.Core.DAL;
using Newtonsoft.Json;

namespace LJH.Inventory.BLL
{
    public class CompanyBLL : BLLBase<string, Company>
    {
        #region 构造函数
        public CompanyBLL(string repoUri)
            : base(repoUri)
        {
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 获取所有客户信息
        /// </summary>
        /// <returns></returns>
        public QueryResultList<Company> GetAllCustomers()
        {
            CompanySearchCondition con = new CompanySearchCondition();
            con.ClassID = ItemClass.客户;
            return GetItems(con);
        }
        /// <summary>
        /// 获取所有供应商信息
        /// </summary>
        /// <returns></returns>
        public QueryResultList<Company> GetAllSuppliers()
        {
            CompanySearchCondition con = new CompanySearchCondition();
            con.ClassID = ItemClass.供应商;
            return GetItems(con);
        }

        public CommandResult SetFileID(Company customer, int fid)
        {
            var patch = new UpdateItem<string>() { ID = customer.ID, Key = "FileID", Value = fid.ToString() };
            var ret = Patch(customer.ID, new List<UpdateItem<string>>() { patch });
            return ret;
        }

        public CommandResult SetTaxFileID(Company customer, int fid)
        {
            var patch = new UpdateItem<string>() { ID = customer.ID, Key = "TaxFileID", Value = fid.ToString() };
            var ret = Patch(customer.ID, new List<UpdateItem<string>>() { patch });
            return ret;
        }

        public CommandResult 设置财务备注(Company info, string memo)
        {
            var patch = new UpdateItem<string>() { ID = info.ID, Key = "财务备注", Value = memo };
            var ret = Patch(info.ID, new List<UpdateItem<string>>() { patch });
            return ret;
        }

        public CommandResult Merge(string source, string des)
        {
            var info = new EntityMergeInfo() { Source = source, Des = des };
            return new APIClient(RepoUri).Process<string, Company>(null, "merge", info);
        }

        public QueryResult<CustomerFinancialState> GetCustomerState(string customerID)
        {
            return new APIClient(RepoUri).GetByID<string, CustomerFinancialState>(customerID);
        }

        public QueryResultList<CustomerFinancialState> GetAllCustomerStates()
        {
            var con = new CompanySearchCondition() { ClassID = ItemClass.客户 };
            return new APIClient(RepoUri).GetList<string, CustomerFinancialState>(con);
        }

        public QueryResult<SupplierFinancialState> GetSupplierState(string customerID)
        {
            return new APIClient(RepoUri).GetByID<string, SupplierFinancialState>(customerID);
        }

        public QueryResultList<SupplierFinancialState> GetAllSupplierStates()
        {
            var con = new CompanySearchCondition() { ClassID = ItemClass.供应商 };
            return new APIClient(RepoUri).GetList<string, SupplierFinancialState>(con);
        }
        #endregion
    }
}
