using HH.YiDaSyncNC.Dtos;
using HH.YiDaSyncNC.Dtos.NC;
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
{
    class ZC_ZJHX_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_fa_card = ConfigurationManager.AppSettings["fa_card"];
        public static string _Static_pk_org_jt = ConfigurationManager.AppSettings["pk_org_jt"];

        public ZC_ZJHX_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);

            var idList = _dingTalkService.GetFormIdList("{\"radioField_lycrvvtk\":\"N\",\"textField_ltxyt75i\":\"同步成功\"}").ToList();

            LogHelper.WriteLog(JsonConvert.SerializeObject(idList.Count), "idList");
            LogHelper.WriteLog(JsonConvert.SerializeObject(idList), "idListValue");

            var fa_cardList = _db_nc.Queryable<fa_card>().ToList();
            var fa_cardhistoryList = _db_nc.Queryable<fa_cardhistory>().ToList();

            var i = 1;

            foreach (var item in idList)
            {
                string createTime = "";
                string modifyTime = "";
                var ydDataSource = _dingTalkService.GetFormDataById(item, out createTime, out modifyTime);

                // 如果NC单据号为空，说明没有传入过NC，此时跳过
                if (ydDataSource.FirstOrDefault(f => f.Key == "textField_ltxyt75m").Value != null && !string.IsNullOrWhiteSpace(ydDataSource["textField_ltxyt75m"].ToString()))
                {

                }
                else
                {
                    continue;
                }

                LogHelper.WriteLog("", "【序号 " + i + " / " + idList.Count + "】" + item + " 开始回写");
                i++;


                var fa_card = fa_cardList.FirstOrDefault(f => f.pk_card == ydDataSource["textField_ltxyt75m"].ToString());
                var fa_cardhistory = fa_cardhistoryList.FirstOrDefault(f => f.pk_card == ydDataSource["textField_ltxyt75m"].ToString() && f.laststate_flag == "Y");

                var fa_cardhistoryHis = fa_cardhistoryList.Where(f => f.pk_card == ydDataSource["textField_ltxyt75m"].ToString() && f.usedmonth != "0").ToList();

                if (fa_card != null && fa_cardhistory != null)
                {

                    var updateData = new Dictionary<string, string>();

                    // 原值
                    updateData.Add("numberField_lty0oyv8", fa_card.currmoney == null ? "0" : fa_card.currmoney.Value.ToString("#0.00"));

                    //// 折旧额
                    //updateData.Add("numberField_lu2eb47d", fa_cardhistory.accudep_cal);

                    // 净值
                    updateData.Add("numberField_lu2ejqd4", fa_card.currmoney == null ? "0" : (fa_card.currmoney.Value - Convert.ToDecimal(fa_cardhistory.accudep_cal)).ToString("#0.00"));

                    // 计提期数
                    updateData.Add("numberField_lutjo2dt", fa_cardhistoryHis.OrderByDescending(ob => ob.usedmonth).FirstOrDefault()==null?"0" : fa_cardhistoryHis.OrderByDescending(ob => ob.usedmonth).FirstOrDefault().usedmonth);

                    var ydRes = _dingTalkService.UpdateFormDataById(updateData, item);

                }

            }
        }
    }
}
