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

namespace CallCenter.QuartzService
{
    public class SatisfactionClose : QuartzJob
    {
        public SatisfactionClose()
        {
            CronExpression = "0 0 0 * * ? *";
            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  in(6,7) and F_IsSms = 1  and F_IsDelete = 0 and(datediff(dd, (select top 1  RecvTime  from T_SMS_RecvSMS where  ( Content like '%您反映的诉求已有回复，请前往http://12345rx.zwfw.anyang.gov.cn:9999/citizenNode/orderDetails.html?%' or Content = '您诉求的诉求已完成，请前往安阳政务服务官网（https://zwfw.anyang.gov.cn），登录后查看办结结果对执行情况进行评价，谢谢' or Content = '您反映的诉求已有回复，请前往安阳政务服务官网（https://zwfw.anyang.gov.cn）登录后查看办结结果并对办理情况进行评价，或者拨打12345市长热线来电咨询，谢谢！')  and F_Name = b.F_WorkOrderId order by    RecvTime desc), GETDATE()) >= 15) and F_DealTime is not null";
            DataTable dt = new DataTable();
            int recordCount = 0;
              dt = CallCenterApi.BLL.PagerBLL.GetListPager(
                "T_Bus_WorkOrder b WITH(NOLOCK)",
                "F_WorkOrderId",
                "*",
                sql,
                "ORDER BY F_CreateTime 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", 1);
                            keyValuePairs.Add("F_CloseUser", "");
                            keyValuePairs.Add("F_CloseTime", DateTime.Now);
                            keyValuePairs.Add("F_FinalOpinion", "15天自动满意");
                            keyValuePairs.Add("F_IsStandard", 1);
                            keyValuePairs.Add("F_StandardIDS", "");
                            keyValuePairs.Add("F_ToBereply", 0);
                            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 = " 15天未评价自动结案了工单，结果：满意";
                            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 = "";
                            if (dr["F_InfoSource"].ToString() == "2458")
                            {
                                strStm_Src_Dsc = "连线政府";
                            }
                            if (dr["F_InfoSource"].ToString() == "1")
                            {
                                strStm_Src_Dsc = "市长热线";
                            }
                            if (dr["F_InfoSource"].ToString() == "2")
                            {
                                strStm_Src_Dsc = "市长信箱";
                            }
                            if (dr["F_InfoSource"].ToString() == "4")
                            {
                                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, "非常满意", "", " 15天未评议自动评议工单", "", "", "", DateTime.Now);
                            object objres = DbHelperSQL.GetSingle(strSql);
                        }

                      
                    }
                  
                }
            }
        }
    }
}
