using Newtonsoft.Json.Linq;
using RB_MES_API.Models;
using RB_MES_API.Models.Cloud;
using System.Data;

namespace RB_MES_API.Controllers
{
    /// <summary>
    /// 控制器间通用服务
    /// </summary>
    public interface IShareController
    {
        /// <summary>
        /// POST请求备份(接收数据校验完成后备份到数据库)
        /// </summary>
        /// <param name="intface">接口名称</param>
        /// <param name="doctype">访问类型</param>
        /// <param name="data">传入的JSON（解密后）</param>
        public void PostRequestBack(string intface, string doctype, string data);
        /// <summary>
        /// 获取枚举显示值
        /// </summary>
        /// <param name="saveval"></param>
        /// <returns></returns>
        public string? GetDropDownText<T>(int saveval);
        /// <summary>
        /// 字符串转decimal，错误时返回0
        /// </summary>
        /// <param name="sqty">待转换的代表数量</param>
        /// <returns></returns>
        public decimal DecimalPar(string sqty);
        /// <summary>
        /// 将JSON对象转为单个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="reason"></param>
        /// <returns></returns>
        public T DataToClass<T>(string data, ref string reason) where T : class, new();
        /// <summary>
        /// 将JSON转为实体类
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="data">Json数据包</param>
        /// <param name="reason">错误消息</param>
        /// <returns></returns>
        public List<T>? DataConvertClass<T>(string data, ref string reason) where T : class;
        /// <summary>
        /// 得到查询接口的名称
        /// </summary>
        /// <param name="id">查询接口序号</param>
        /// <returns></returns>
        public string? InterfaceName(int id);
        /// <summary>
        /// 得到接收接口的名称
        /// </summary>
        /// <param name="id">K3单据类型ID</param>
        /// <returns></returns>
        public string? K3BillName(int id);
        /// <summary>
        /// 将DataTable转成XML
        /// </summary>
        /// <param name="tb">数据表</param>
        /// <param name="tbName">表名</param>
        /// <returns></returns>
        public string? DatatableToXml(DataTable tb, string? tbName=null);
        ///// <summary>
        ///// 沃比日志，单独作用域
        ///// </summary>
        ///// <param name="intfc"></param>
        ///// <param name="doctypd">接口类型</param>
        ///// <param name="mess">消息说明</param>
        ///// <param name="sour">消息源</param>
        ///// <param name="staus">是否错误</param>
        ///// <param name="guid">全球唯一码</param>
        //public Task<int> _Sqllogger(int intfc, string doctypd, string mess, string sour, bool staus = false, string guid = "");
        /// <summary>
        /// 沃比SQL日志
        /// </summary>
        /// <param name="intfc">接口分组</param>
        /// <param name="doctypd">接口类型</param>
        /// <param name="mess">消息说明</param>
        /// <param name="sour">消息源</param>
        /// <param name="staus">是否错误</param>
        /// <param name="guid">全球唯一码，成对出现时用于识别传递前后</param>
        public void _SqlloggerInvoke(int intfc, string doctypd, string mess, string sour, bool staus = false, string guid = "");
        public void _SqllogInvoke(RBLog log);
        /// <summary>
        /// 取得查询连接字符串
        /// </summary>
        /// <param name="id">接口ID</param>
        /// <returns></returns>
        public string GetSelectSql(int id);
        /// <summary>
        /// 通过DataTable转为指定的实体类
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="dataTable">输入表单</param>
        /// <returns></returns>
        public List<T> GetClassList<T>(DataTable dataTable) where T : class, new();
        /// <summary>
        /// 字符串64位解密
        /// </summary>
        /// <param name="B64"></param>
        /// <returns></returns>
        public string UnBase64Decode(string B64);
        /// <summary>
        /// 字符串64位加密
        /// </summary>
        /// <param name="B64"></param>
        /// <returns></returns>
        public string Base64Decode(string B64);
        /// <summary>
        /// 在目标ints中，通过组合，检验是否包含参数v
        /// </summary>
        /// <param name="ints">要校验的参数值范围</param>
        /// <param name="v">要判断的枚举值</param>
        /// <param name="setv">当前设置的值</param>
        /// <returns></returns>
        public bool SCombination(List<int> ints, int v, int setv);
        /// <summary>
        /// 将目标枚举T，除exceptobj以外的参数值放入List<int>中。注意！是参数值，不是枚举项目所在index
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="exceptobj">排除的参数值</param>
        /// <returns>除exceptobj以外的参数值集合</returns>
        public List<int> GetSetInts<T>(int exceptobj) where T : struct, Enum;
        /// <summary>
        /// 将DataTable对象转为JSON，带字段名
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public string ObjectToJSON(DataTable table);
        
        /// <summary>
        /// 判断是否批量保存成功
        /// </summary>
        /// <param name="result">需要分析的json</param>
        /// <param name="needfileds">必须返回的字段</param>
        /// <param name="message">如果不成功，代表错误消息；否则代表返回的字段键值对Dictionary<string, string></param>
        /// <returns></returns>
        public bool CloudExecuteBatchSaveStatus(string result, string needfileds, ref List<Dictionary<string, string>> message);
        /// <summary>
        /// 判断金蝶云星空是否批量保存成功
        /// </summary>
        /// <param name="result">需要分析的json</param>
        /// <param name="needfileds">必须返回的字段</param>
        /// <param name="message">如果不成功，代表错误消息；否则代表返回的字段键值对
        /// <returns></returns>
        public bool KDCloudExecuteBatchSaveStatus(string result, string needfileds, ref int errcord, ref List<Dictionary<string, string>> message);
        /// <summary>
        /// Cloud判断是否保存成功
        /// </summary>
        /// <param name="result"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool CloudExecuteSaveStatus(string result, ref string message);

        /// <summary>
        /// 动态生成客户端需要的对象，inputjson必须有字段名！
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fid">FFunctionID</param>
        /// <param name="dectablename">FDesTableName</param>
        /// <param name="inputjson">从金蝶Cloud里获取的数据集</param>
        /// <param name="reason">错误时返回消息</param>
        /// <returns></returns>
        public List<T> GetNewObj<T>(int fid, string dectablename, string inputjson, ref string reason) where T : class, new();
        /// <summary>
        /// 将子查询的返回数据按顺序赋值给对象，inputjson可以无字段名！
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fieldkeys">字段顺序，当inputjson无字段名时以此为准</param>
        /// <param name="inputjson">返回JSON</param>
        /// <param name="reason">错误时返回消息</param>
        /// <returns></returns>
        public List<T> GetNewObjForChild<T>(string fieldkeys, string inputjson, ref string reason) where T : class, new();
        /// <summary>
        /// 将传入的JSON转为DataTable，接收到的数据可以无字段名！字段来源于r_ClumnConvert
        /// </summary>
        /// <param name="fid">FFunctionID</param>
        /// <param name="DocType">FDesTableName</param>
        /// <param name="data">从云星空查询到的数据列表</param>
        /// <returns></returns>
        public DataTable GetInputDataTable(int fid, string DocType, string data, ref string messtr);
        /// <summary>
        /// 根据字段顺序，将JSON数据集转为DataTable,data可以无字段名！fieldkeys来源于r_CloudBillQuery
        /// </summary>
        /// <param name="fieldkeys">字段。格式SQL查询Select后From前的字段名，不能为空！！！</param>
        /// <param name="data"></param>
        /// <param name="messtr">如果有错误就返回消息</param>
        /// <returns></returns>
        public DataTable JsonConvertDatatable(string fieldkeys, string data, ref string messtr);
        /// <summary>
        /// JSON直接转datatable
        /// </summary>
        /// <param name="data">json</param>
        /// <param name="messtr">返回的错误消息</param>
        /// <returns></returns>
        public DataTable JsonToDatatable(string data,ref string messtr);
        /// <summary>
        /// 将有字段名的表转为LIST，Key为字段名，Val为字段值
        /// </summary>
        /// <param name="table">数据表</param>
        /// <returns></returns>
        public List<Dictionary<string, string>> GetClassList(DataTable table);
        /// <summary>
        /// 将JArray转为Dictionary，子项必须有字段名！！！
        /// </summary>
        /// <param name="arrjson"></param>
        /// <returns></returns>
        public List<Dictionary<string, object>> GetClassList(JArray arrjson);        
        /// <summary>
        /// 将计量单位添加到缓存
        /// </summary>
        public void AddUnitCache();
        /// <summary>
        /// 从缓存中读取API接口所需要的配置
        /// </summary>
        /// <typeparam name="T">配置类</typeparam>
        /// <returns></returns>
        public Task<List<T>> GetAPIList<T>() where T : class, new();
        /// <summary>
        /// 取有源单的推送单据类型
        /// </summary>
        /// <param name="postdoctype">推送的接口类型</param>
        /// <param name="sourtype">源单的单据类型编号</param>
        /// <returns></returns>
        public string GetDesFormID(string postdoctype, string sourtype);
        /// <summary>
        /// JSON转动态对象列表
        /// 真正的动态对象，注意字段要完全匹配!!!
        /// </summary>
        /// <param name="spacename">命名空间名称</param>
        /// <param name="clssname">模型名</param>
        /// <param name="data">能转为JArray的json</param>
        /// <param name="reason">错误消息</param>
        /// <returns></returns>
        public List<T>? DataConvertClass<T>(string spacename, string clssname, string data, ref string reason) where T : class, new();
        /// <summary>
        /// 实体类的必填字段校验，未写完
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ts"></param>
        /// <param name="reason"></param>
        /// <returns></returns>
        public bool CheckRequired<T>(List<T> ts, ref string reason);
        /// <summary>
        /// 通过系统参数获取销售价格资料ID
        /// </summary>
        /// <param name="reason"></param>
        /// <returns></returns>
        public int GetSalePriceID(string reason);
        /// <summary>
        /// ASC进位算法
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <param name="len">总长度</param>
        /// <param name="coverstr">补位符号</param>
        /// <param name="mess">错误消息</param>
        /// <returns></returns>
        public void ASCCarry( ref string s, int len, string coverstr);
        /// <summary>
        /// 不必等待，直接保存请求成功的备份
        /// </summary>
        /// <param name="dataBacks">List<ReciveDataBack></param>
        public void SaveReciveData(object dataBacks);
        
    }
}
