using HH.YiDaSyncNC.Dtos.NC;
using HH.YiDaSyncNC.Dtos.YIDA;
using HH.YiDaSyncNC.Helper;
using HH.YiDaSyncNC.Models;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace HH.YiDaSyncNC.DataSync
{
    public class FKYF_SyncService
    {
        public string _AppType;
        public string _SystemToken;
        public string _UserId;
        public string _FormId;
        public SqlSugarClient _db;
        public SqlSugarClient _db_nc;
        public string _NCApiUrl;

        public static string _Static_PK_bd_project = ConfigurationManager.AppSettings["bd_project"];
        public static string _Static_pk_org_jt = ConfigurationManager.AppSettings["pk_org_jt"];

        public FKYF_SyncService(string AppType, string SystemToken, string UserId, string FormId, SqlSugarClient db, SqlSugarClient db_nc, string NCApiUrl)
        {
            _AppType = AppType;
            _SystemToken = SystemToken;
            _UserId = UserId;
            _FormId = FormId;
            _db = db;
            _db_nc = db_nc;
            _NCApiUrl = NCApiUrl;
        }


        public void DataSync()
        {
            //DingTalkService _dingTalkService = new DingTalkService(_AppType, _SystemToken, _UserId, _FormId);

            //// 宜搭全量数据Id
            //var serchKeyListJson = new
            //{
            //    radioField_lycrvvtk = "Y",
            //    selectField_lrnh633d = "预付",

            //};
            //var idList = _dingTalkService.GetFormIdList(JsonConvert.SerializeObject(serchKeyListJson));
            ////LogHelper.WriteLog(JsonConvert.SerializeObject(idList.Count), "idList");
            //#region 获取NC中选项数据的Id
            //var bd_customerDic = _db_nc.Queryable<bd_customer>().ToList();
            //var bd_incomeDic = _db_nc.Queryable<bd_income>().ToList();
            //var org_orgsDic = _db_nc.Queryable<org_orgs>().ToList();
            //var bd_projectDic = _db_nc.Queryable<bd_project>().ToList();
            //var bd_materialDic = _db_nc.Queryable<bd_material>().ToList();
            //var bd_measdocDic = _db_nc.Queryable<bd_measdoc>().ToList();
            //var org_deptDic = _db_nc.Queryable<org_dept>().ToList();
            //var bd_psndocDic = _db_nc.Queryable<bd_psndoc>().ToList();
            //var sm_userDic = _db_nc.Queryable<sm_user>().ToList();
            //var org_accountingbookDic = _db_nc.Queryable<org_accountingbook>().ToList();
            //#endregion
            //foreach (var item in idList)
            //{
            //    string createTime = "";
            //    string modifyTime = "";

            //    var ydDataSource = _dingTalkService.GetFormDataById(item, out createTime, out modifyTime);
            //    string jsonData = JsonConvert.SerializeObject(ydDataSource);
            //    YiDa_SKDNC thisYiDaData = JsonConvert.DeserializeObject<YiDa_SKDNC>(jsonData);
            //    if (string.IsNullOrWhiteSpace(thisYiDaData.selectField_lvywoqmb)) continue;
            //    if (string.IsNullOrWhiteSpace(thisYiDaData.textField_lwitiobt)) continue;
            //    var detailObjFPList1 = thisYiDaData.tableField_lsx02qv5; //发票明细
            //    if (detailObjFPList1 is null || detailObjFPList1.Count == 0) continue;

            //    DateTime 日期 = DateTime.Now;// UnixTimeHelper.ConvertMillisecondsToDateTime(thisYiDaData.dateField_lviv4sfy);

            //    string 组织 = "";
            //    if (!string.IsNullOrWhiteSpace(thisYiDaData.selectField_lvywoqmb))
            //    {
            //        var tempData = org_orgsDic.FirstOrDefault(f => f.name == thisYiDaData.selectField_lvywoqmb);
            //        if (tempData != null)
            //        {
            //            组织 = tempData.code; // 组织
            //        }
            //    }

            //    string 单据号 = "";
            //    if (!string.IsNullOrWhiteSpace(thisYiDaData.serialNumberField_lwiz0n9f))
            //    {

            //        单据号 = thisYiDaData.serialNumberField_lwiz0n9f; // 组织
            //    }

            //    string 制单人 = "OA-NC";

            //    // 表头
            //    var headModel = new FKYF_SyncServiceModel.DataModel.voucher_header()
            //    {
            //        billdate = 日期.ToString("yyyy-MM-dd"),
            //        billmaker = 制单人,
            //        billno = 单据号,
            //        billperiod = 日期.ToString("MM"),
            //        billyear = 日期.ToString("yyyy"),
            //        creationtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
            //        creator = 制单人,
            //        local_money = thisYiDaData.numberField_lvua3j18.ToString(),
            //        money = thisYiDaData.numberField_lvua3j18.ToString(),
            //        pk_org = 组织,
            //        pu_org = 组织,
            //        pu_deptid = "",
            //        pu_psndoc = "",

            //    };

            //    //表体
            //    var detailList = new List<FKYF_SyncServiceModel.DataModel.voucher_body>();
            //    if (thisYiDaData.tableField_lrngrtgu != null)
            //    {
            //        var detailObjList1 = thisYiDaData.tableField_lrngrtgu; //明细
            //        int index = 1;

            //        foreach (var objItem in detailObjList1)
            //        {
            //            var thisBody = new FKYF_SyncServiceModel.DataModel.voucher_body()
            //            {
            //                sett_org = 组织,
            //                supplier = thisYiDaData.textField_lwitiobt,
            //                pk_subjcode = objItem.textField_lwitioch,
            //                subjcode = objItem.textField_lwitioce,
            //                billdate = 日期.ToString("yyyy-MM-dd"),
            //                busidate = 日期.ToString("yyyy-MM-dd"),
            //                invoiceno = detailObjFPList1.FirstOrDefault().textField_lsx02qv8,
            //                price = objItem.numberField_lvxa4v09.ToString(),// "单价",
            //                taxprice = objItem.numberField_lvua3j17.ToString(),
            //                quantity_cr = "1",//"贷方数量",
            //                money_cr = objItem.numberField_lvua3j17.ToString(),//"贷方原币金额",
            //                notax_cr = objItem.numberField_lvxa4v09.ToString(),//"贷方原币无税金额",
            //                local_money_cr = objItem.numberField_lvua3j17.ToString(),//"组织本币金额(贷方)",
            //                local_notax_cr = objItem.numberField_lvxa4v09.ToString(),//"组织本币无税金额(贷方)",
            //                caltaxmny = objItem.numberField_lvxa4v09.ToString(),//"计税金额",
            //                local_tax_cr = objItem.numberField_lvxa4v08.ToString() ?? "0",//"税额",
            //                taxrate = "13",
            //                pu_org = 组织,
            //                rowno = index.ToString(),
            //                ordercubasdoc = thisYiDaData.textField_lwitiobt,
            //                pk_fiorg = 组织,
            //                pk_org= 组织,
            //                pu_deptid = "",
            //                pk_psndoc ="",
            //                pk_deptid ="",
            //                material="",
            //            };
            //            detailList.Add(thisBody);
            //            index++;
            //        }


            //        XElement xElement = FKYF_SyncServiceModel.ImportXml_应付单.TemplateHeade(headModel);
            //        xElement.Add(FKYF_SyncServiceModel.ImportXml_应付单.Header(headModel));//添加表头数据
            //        xElement.Element("bill").Element("billhead").Element("bodys").Add(FKYF_SyncServiceModel.ImportXml_应付单.Body(detailList));
            //        //生成凭证
            //        var results_应付单 = NCImportFunc.NCImport2(xElement, _NCApiUrl + "?account=01&groupcode=0");

            //        LogHelper.WriteLog(JsonConvert.SerializeObject(results_应付单), "results_应付单");


            //}
            //}
        }

    }
}
