using Com.Bocom.OpenApi.Utils;
using Com.Bocom.OpenApi;
using Kivii.Web;
using Kivii;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kivii.Linq;

namespace Njzj.Bocoms.Openapis.Transforms
{
    public class WriteOffResultNotifyCallBack : RestfulExecution<WriteOffResultNotify>
    {
        public string biz_content { get; set; }
        public string msg_id { get; set; }
        public string timestamp { get; set; }
        public string encrypt_key { get; set; }
        public string sign { get; set; }
        public override object OnExecution(IRequest req, IResponse res)
        {
            Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------");
            Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------");
            var rtns = new RestfulExecutionResponse<WriteOffResultNotify>();
            rtns.Results = new List<WriteOffResultNotify>();

            if (biz_content.IsNullOrEmpty()) return rtns;
            if (msg_id.IsNullOrEmpty()) return rtns;
            if (timestamp.IsNullOrEmpty()) return rtns;
            if (encrypt_key.IsNullOrEmpty()) return rtns;
            if (sign.IsNullOrEmpty()) return rtns;
            var dic = new Dictionary<string, string>();
            dic.Add("biz_content", biz_content);
            dic.Add("msg_id", msg_id);
            dic.Add("timestamp", timestamp);
            dic.Add("encrypt_key", encrypt_key);
            dic.Add("sign", sign);
            Console.WriteLine("------------------------------------WriteOffResultNotifyCallBack-----------------------------------------");
            Console.WriteLine("------------------------------------Start-----------------------------------------");
            try
            {
                var result = ApiUtils.ParseCommunicationJsonWithBocomSign(dic, "utf-8", Configs.MY_PRIVATE_KEY, Configs.APIGW_PUBLIC_KEY);

                var conn = KiviiContext.GetOpenedDbConnection<WriteOffResultNotify>();
                conn.InitEntityType<EntityLog<WriteOffResultNotify>>();
                //var log = conn.Single<EntityLog<WriteOffResultNotify>>(o => true);
                //if (log == null) return rtns;

                result.BillId = result.biz_content.bill_id;
                result.BizKvid = Guid.Parse(result.biz_content.cont_id);
                conn.Insert(result);

                var resp = "{\"rsp_code\":\"0\",\"biz_state\":\"S\",\"rsp_msg\":\"核销处理成功\"}";

                //var rspBizContent = "{\"rsp_head\":{\"response_msg\":\"非白名单客户\",\"response_code\":\"CIPP0004MT0017\"},\"rsp_body\":{\"context_no\":\"P21201026566300000019\",\"account_exist_flag\":\"EF00\"}";
                var rsp = ApiUtils.SignRspBizContent(resp, result.encrypt_key, Configs.MY_PRIVATE_KEY);
                rtns.Results.Add(result);
                return rsp;
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                Console.WriteLine("------------------------------------WriteOffResultNotifyCallBack-----------------------------------------");
                Console.WriteLine("------------------------------------End-----------------------------------------");
                Console.WriteLine("-----------------------------------------------------------------------------------------------------");
                Console.WriteLine("-----------------------------------------------------------------------------------------------------");
            }

        }
    }


    [RequiresAnyRole(SystemRoles.Everyone)]
    public class WriteOffResultNotifyQuery : RestfulQuery<WriteOffResultNotify>
    {
    }
}
