﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace LJH.Inventory.BusinessModel
{
    /// <summary>
    /// 表示各种单据的一个接口
    /// </summary>
    public abstract class SheetBase : LJH.GeneralLibrary.Core.DAL.IEntity<string>
    {
        #region 属性
        /// <summary>
        /// 获取或设置单据编号
        /// </summary>
        public string ID { get; set; }
        /// <summary>
        /// 获取或设置采购单类型
        /// </summary>
        public ItemClass SheetType { get; set; }
        /// <summary>
        /// 获取或设置单据日期
        /// </summary>
        public DateTime SheetDate { get; set; }
        /// <summary>
        /// 获取或设置上次活动时间
        /// </summary>
        public DateTime LastActiveDate { get; set; }
        /// <summary>
        /// 获取或设置订单号
        /// </summary>
        public virtual string OrderID { get; set; }
        /// <summary>
        /// 获取或设置项目
        /// </summary>
        public string ProjectID { get; set; }
        /// <summary>
        /// 获取或设置当前状态
        /// </summary>
        public SheetState State { get; set; }
        /// <summary>
        /// 获取或设置总额
        /// </summary>
        public decimal Amount { get; set; }
        /// <summary>
        /// 获取或设置更新时的备注，这个字段只用于将更新备注上传到服务器
        /// </summary>
        public string UpdateMemo { get; set; }
        #endregion

        #region 扩展属性
        /// <summary>
        /// 获取或设置扩展属性
        /// </summary>
        public string Note { get; set; }

        protected Dictionary<string, string> _Externals = null;

        public string GetProperty(ItemNote pn)
        {
            if (_Externals == null && !string.IsNullOrEmpty(Note)) _Externals = JsonConvert.DeserializeObject<Dictionary<string, string>>(Note);
            if (_Externals == null) return null;
            if (_Externals.ContainsKey(((int)pn).ToString())) return _Externals[((int)pn).ToString()];
            if (_Externals.ContainsKey(pn.ToString())) return _Externals[pn.ToString()];
            return null;
        }

        public void SetProperty(ItemNote pn, string value)
        {
            var key = ((int)pn).ToString();
            if (_Externals == null && !string.IsNullOrEmpty(Note)) _Externals = JsonConvert.DeserializeObject<Dictionary<string, string>>(Note);
            if (_Externals == null) _Externals = new Dictionary<string, string>();
            if (value == null && _Externals.ContainsKey(key)) _Externals.Remove(key);
            else _Externals[key] = value;
            Note = JsonConvert.SerializeObject(_Externals);
        }

        public void RemoveProperty(ItemNote pn)
        {
            var key = ((int)pn).ToString();
            if (_Externals == null && !string.IsNullOrEmpty(Note)) _Externals = JsonConvert.DeserializeObject<Dictionary<string, string>>(Note);
            if (_Externals == null) return;
            if (_Externals.ContainsKey(key)) _Externals.Remove(key);
            Note = JsonConvert.SerializeObject(_Externals);
        }

        public decimal? GetPropertyByDecimal(ItemNote pnote)
        {
            string temp = this.GetProperty(pnote);
            decimal sq;
            if (!string.IsNullOrEmpty(temp) && decimal.TryParse(temp, out sq)) return sq;
            return null;
        }

        /// <summary>
        /// 获取或设置备注信息
        /// </summary>
        public virtual string Memo
        {
            get { return GetProperty(ItemNote.备注); }
            set { SetProperty(ItemNote.备注, value); }
        }

        public decimal? 重量小数位
        {
            get { return GetPropertyByDecimal(ItemNote.重量小数位); }
            set { SetProperty(ItemNote.重量小数位, value == null ? null : value.ToString()); }
        }
        #endregion

        #region 扩展状态属性
        /// <summary>
        /// 获取或设置扩展状态属性
        /// </summary>
        public string StateProperties { get; set; }

        protected Dictionary<string, string> _Properties = null;

        public string GetStateProperty(ItemNote pn)
        {
            if (_Properties == null && !string.IsNullOrEmpty(StateProperties)) _Properties = JsonConvert.DeserializeObject<Dictionary<string, string>>(StateProperties);
            if (_Properties == null) return null;
            if (_Properties.ContainsKey(((int)pn).ToString())) return _Properties[((int)pn).ToString()];
            if (_Properties.ContainsKey(pn.ToString())) return _Properties[pn.ToString()];
            return null;
        }

        public decimal? GetStatePropertyOfDecimal(ItemNote pn)
        {
            var temp = GetStateProperty(pn);
            decimal d;
            if (string.IsNullOrEmpty(temp) || !decimal.TryParse(temp, out d)) return null;
            return d;
        }

        public int? GetStatePropertyOfInt(ItemNote pn)
        {
            var temp = GetStateProperty(pn);
            int d;
            if (string.IsNullOrEmpty(temp) || !int.TryParse(temp, out d)) return null;
            return d;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 检测单据是否可以进行某个操作
        /// </summary>
        public abstract bool CanDo(ItemOperation operation);
        /// <summary>
        /// 克隆一个自身复本
        /// </summary>
        /// <returns></returns>
        public virtual SheetBase Clone()
        {
            _Externals = null;
            _Properties = null;
            return this.MemberwiseClone() as SheetBase;
        }
        #endregion
    }
}