using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;

namespace PMS.NetCore.Service
{
    /// <summary>
    /// 昆山项目发缴费短信
    /// </summary>
    public class KunshanPayCostSmsJob : IJob
    {
        NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                try
                {
                    var thisDate = DateTime.Now.ToString("yyyy-MM-dd");
                    //thisDate = "2020-12-01";
                    //NlogHelper.quartzLogger.Info("开始");
                    var sql = $" SELECT b.billitemname ,a.nneedpaymny ,a.nrevmny ,a.dneeddate,a.dbegindate,a.denddate,c.firsttel  FROM fdc_pm_fare_b a INNER JOIN fdc_pm_billsitem b ON a.billitemcode=b.billitemcode  INNER JOIN fdc_pm_owner c ON a.ownerid=c.ownerid WHERE     ( a.flag=1 OR a.flag IS NULL) AND a.nneedpaymny>a.nrevmny AND CONVERT(VARCHAR(10),a.dneeddate,120)='{thisDate}' ";
                    //logger.Info($"主项计划sql:{sql}");
                    var manager = new actionsManager();
                    var dt = manager.Db.Ado.GetDataTable(sql);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        //费用类型
                        var billitemname = dt.Rows[i]["billitemname"]?.ToString();
                        //手机号
                        var phone = dt.Rows[i]["firsttel"]?.ToString();
                        //应交日期
                        var dneeddate = dt.Rows[i]["dneeddate"].ToString();
                        //开始日期
                        var dbegindate = dt.Rows[i]["dbegindate"]?.ToString();
                        //结束日期
                        var denddate = dt.Rows[i]["denddate"]?.ToString();
                        ////1：租金，2：水费，3：电费，4：服务费，5：押保，6：违约金，7：创收，8：水电费，9：物业费
                        var type = "";
                        if(billitemname=="租金")
                        {
                            type = "1";
                        }
                        else if (billitemname == "水费")
                        {
                            type = "2";
                        }
                        else if (billitemname == "电费")
                        {
                            type = "3";
                        }
                        else if (billitemname == "服务费")
                        {
                            type = "4";
                        }
                        else if (billitemname == "押保")
                        {
                            type = "5";
                        }
                        else if (billitemname == "违约金")
                        {
                            type = "6";
                        }
                        else if (billitemname == "创收")
                        {
                            type = "7";
                        }
                        else if (billitemname == "水电费")
                        {
                            type = "8";
                        }
                        else if (billitemname == "物业费")
                        {
                            type = "9";
                        }
                        if(type.IsNotNullAndEmpty())
                        {
                            
                            var closingDate = Convert.ToDateTime(dbegindate).ToString("yyyy年MM月") + "至" + Convert.ToDateTime(denddate).ToString("yyyy年MM月");
                            var outingDate = Convert.ToDateTime(dneeddate).ToString("yyyy年MM月dd日");
                            string url = AppSettingsHelper.Configuration["kunshanUrl"];
                            var postvalues = new NameValueCollection();
                            postvalues.Add("telephone", phone);
                            postvalues.Add("type", type);
                            postvalues.Add("closingDate", closingDate);
                            postvalues.Add("outingDate", outingDate);
                            var res = WebClientHelper.ClientPost(url, postvalues);
                            NlogHelper.quartzLogger.Info($"手机号：{phone}发送短信执行完毕");
                            var outData = JsonConvert.DeserializeObject<OutData>(res);
                            sql = $"insert into sms(Phone,status,msg,contents) values ('{phone}','{outData.status}','{outData.msg}','{outData.contents}')";
                            manager.Db.Ado.ExecuteCommand(sql);
                        }
                       

                    }

                }
                catch (Exception ero)
                {
                    NlogHelper.quartzLogger.Error(" 发邮件执行出错:" + ero, true);
                }

            });

        }
    }

    public class OutData
    {
        public string status { get; set; }
        public string msg { get; set; }
        public string contents { get; set; }
    }
}
