using CallCenterApi.DB;
using CallCenterApi.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.QuartzService
{
    public class SatisfactionClose : QuartzJob
    {
        public SatisfactionClose()
        {
            CronExpression = "0 0/10 * * * ?";
            DoWork += MyWork_DoWork;
        }
       
        private readonly CallCenterApi.BLL.T_Bus_WorkOrder  workorder = new CallCenterApi.BLL.T_Bus_WorkOrder();
        private void MyWork_DoWork(object sender, EventArgs e)
        {
            //您反映的诉求已有回复，请前往http://12345rx.zwfw.anyang.gov.cn:9999/citizenNode/orderDetails.html?workorderid=XX9941050021122801070查看办结结果并进行评价，谢谢！
            string sql = " and F_WorkState =7 and F_IsSms = 1  and F_IsDelete = 0 and  datediff(minute, F_SmsTime, GETDATE()) >= 2880 and F_SmsTime is not null " +
                "  and F_DealTime is not null  and F_PublicComment=-1 ";
            DataTable dt = new DataTable();
            int recordCount = 0;
              dt = CallCenterApi.BLL.PagerBLL.GetListPager(
                "T_Bus_WorkOrder b WITH(NOLOCK)",
                "F_Id",
                "*",
                sql,
                "ORDER BY F_Id DESC",
                10000,
                1,
                true,
                out recordCount);
            var now = DateTime.Now;
           //var modellist = workorder.GetModelList("F_WorkState =6 and F_IsSms =1  and F_IsDelete =0 and  (datediff(hh,F_DealTime   ,GETDATE()  )>=15) and  F_DealTime is not null");
            if (dt != null && dt.Rows .Count >0)
            {
               foreach (DataRow dr in dt.Rows)
                {
                     CallCenterApi.Model.T_Bus_WorkOrder it = new CallCenterApi.Model.T_Bus_WorkOrder();
                    it = workorder.GetModel(dr["F_WorkOrderId"].ToString());
                    if (it !=null )
                    {
                        string Commentsql = "SELECT COUNT(1) FROM PublicComment WHERE WorkOrderId  ='"+it .F_WorkOrderId +"' AND CreateTime >='"+it .F_DealTime  +"'";
                        var Comment = DbHelperSQL.GetSingle(Commentsql).ToString();
                        if (int .Parse (Comment)>0)
                        {
                            continue;
                        }
                        else
                        {
                            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
                            keyValuePairs.Add("F_WorkState", 9);
                            keyValuePairs.Add("F_IsSatisfie", 1);
                            keyValuePairs.Add("F_IsClosed", 2);
                            keyValuePairs.Add("F_CloseUser", "");
                            keyValuePairs.Add("F_CloseTime", DateTime.Now);
                            keyValuePairs.Add("F_FinalOpinion", "2天自动基本满意");
                            keyValuePairs.Add("F_IsStandard", 1);
                            keyValuePairs.Add("F_StandardIDS", "");
                            keyValuePairs.Add("F_ToBereply", 0);
                            keyValuePairs.Add("F_PublicComment", 1);
                            keyValuePairs.Add("F_Contentment", 2);
                           bool n=  workorder.UpdateWorkOrder(it.F_Id, keyValuePairs);
                            //  workorder.Update(it);
                            CallCenterApi.Model.T_Bus_Operation oper = new CallCenterApi.Model.T_Bus_Operation();
                            oper.F_WorkOrderId = it.F_WorkOrderId;
                            oper.F_State = 9;
                            oper.F_Message = "2天未评价自动结案了工单，结果：基本满意";
                            oper.F_CreateUser = "8000";
                            oper.F_CreateTime = DateTime.Now;
                            oper.F_IsDelete = 0;
                            new CallCenterApi.BLL.T_Bus_Operation().Add(oper);
                            string strStm_Src_Dsc = "";
                            string strSql = string.Format(@"INSERT INTO PublicComment ( [WorkOrderId], [strStm_Src_Dsc], [strWrkOrder_Cst_Ssf_Cd], [strUdt_Psn_ID], [strSsf_Cst_Ass_CntDsc], [strRltv_InsID], [strInpt_Inst_Nm], [strHpCnt], [CreateTime]) 
                   values   ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}') ;select @@IDENTITY ", dr["F_WorkOrderId"].ToString(), strStm_Src_Dsc, "基本满意", "", "2天未评议自动评议工单", "", "", "", DateTime.Now);
                            object objres = DbHelperSQL.GetSingle(strSql);


                            if (n)
                            {
                                CallCenterApi.Model.T_Bus_VisitResult modelT_Bus_VisitResult = new CallCenterApi.Model.T_Bus_VisitResult();

                                modelT_Bus_VisitResult.F_WorkOrderId = it.F_WorkOrderId;
                                modelT_Bus_VisitResult.F_AssignedId = 0;
                                modelT_Bus_VisitResult.F_VisitUser = "市民";
                                modelT_Bus_VisitResult.F_Type = 6;
                                modelT_Bus_VisitResult.F_IsSatisfie = 2;
                                modelT_Bus_VisitResult.F_Result = "市民评议基本满意自动结案";
                                modelT_Bus_VisitResult.F_Remark = "";//重办原因
                                modelT_Bus_VisitResult.F_State = 1;
                                modelT_Bus_VisitResult.F_IsDelete = 0;
                                modelT_Bus_VisitResult.F_CreateUser = "市民";
                                modelT_Bus_VisitResult.F_CreateTime = DateTime.Now;
                                int i = new CallCenterApi.BLL.T_Bus_VisitResult().Add(modelT_Bus_VisitResult);
                                if (i > 0)
                                {
                                    pushcase_info(it .F_WorkOrderId, "case_finish_info");
                                    if (it.F_InfoSource == 2580)
                                    {
                                        case_info(it.F_WorkOrderId, "FinishWorkOrder");
                                    }
                                    pushcase_info(it.F_WorkOrderId, "case_visit_info");
                                    pushcase_info(it.F_WorkOrderId, "case_visit_detail_info");
                                }
                            }
                        }

                      
                    }
                   
            }
        }
       }
        string url = "http://119.0.11.6:8002/Affairs/";
        public void case_info(string workorderid, string parameter)
        {

            WebClient web = new WebClient();
            web.Encoding = Encoding.UTF8;
            string Dataurl = web.DownloadString(url + parameter + "?workorderid=" + workorderid);

        }
        string phshurl = "http://119.0.11.6:8002/Converge/";
        public string pushcase_info(string workorderid, string parameter)
        {
            WebClient web = new WebClient();
            web.Encoding = Encoding.UTF8;
            string Dataurl = web.DownloadString(phshurl + parameter + "?workorderid=" + workorderid);
            return "";
        }

    }
}
