using RMYY_CallCenter_Api.DB;
using RMYY_CallCenter_Api.Models.Dto;
using RMYY_CallCenter_Api.Models.Enum;
using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace RMYY_CallCenter_Api.Controllers.WorkOrder
{
    public class WorkOrderReportController : BaseController
    {

        /// <summary>
        /// 我的提交
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public ActionResult GetSubmitList(string name,string phone,string wocode, string starttime = "", string endtime = "", int pageindex = 1, int pagesize = 10, int state = -1, int type = 0, int loweltype = 0)
        {
            if (User != null)
            {
                int recordCount = 0;
                string sql = $" and prosercode='{User.F_UserCode}'";
                if (!string.IsNullOrEmpty(name))
                {
                    sql += $" and prosername like '%{name}%'";
                }
                if (!string.IsNullOrEmpty(phone))
                {
                    sql += $" and proserphone like '%{phone}%'";
                }
                if (!string.IsNullOrEmpty(wocode))
                {
                    sql += $" and wocode like '%{wocode}%'";
                }
                if (type > 0)
                {
                    sql += $" and  fwotype={type}";
                }
                if (loweltype > 0)
                {
                    sql += $" and  swotype={loweltype}";
                }
                if (state >= 0)
                {
                    sql += $" and  wostate={state}";
                }
                Dictionary<string, object> paras = new Dictionary<string, object>();
                paras.Add("@keyword", sql);
                paras.Add("@sdate", starttime);
                paras.Add("@edate", endtime);
                paras.Add("@pageindex", pageindex);
                paras.Add("@pagesize", pagesize);
                var dataset = DbHelperSQL.RunProcedure("P_GetComWorkOrder", paras, "AllWokerOrder");
                var data = dataset.Tables[0];
                recordCount = Convert.ToInt32(dataset.Tables[1].Rows[0]["recordCount"]);
                var workklist = DataTableExtensions.ToList<TotalWorkeOrderDto>(data).ToList();
                foreach (var item in workklist)
                {
                    item.WstateName = GetWoState(item.fwotype, item.swotype, item.wostate);
                    item.WoTopName = new Bll.T_Sys_GongDan().GetModel(item.fwotype)?.F_Name ?? "";
                    #region  超时时间
                    if (!string.IsNullOrEmpty(item.swotype.ToString()))
                    {
                        var gdmodel = new Bll.T_Sys_GongDan().GetModel(item.swotype);
                        if (gdmodel != null)
                        {
                            DateTime deadline = item.createtime.AddHours(gdmodel.F_TimeOut);
                            TimeSpan ts = deadline.Subtract(DateTime.Now);
                            double tss = double.Parse(Math.Round(ts.TotalSeconds, 0).ToString());
                            if (tss < 0) { tss = -tss; }
                            string gshsj = DateTimeConvert.parseTimeSeconds(tss, 1);

                            if (deadline > DateTime.Now)
                            {

                                item.GapTime = "剩余" + gshsj;
                            }
                            else
                            {

                                item.GapTime = "超期" + gshsj;
                            }
                        }
                    }
                    #endregion
                    #region 操作按钮
                    item.buttons = WorkOrderBase.GetbuttonList(item.swotype,item.wostate);
                    #endregion

                }
                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = workklist,
                    total = recordCount

                };
                return Content(obj.ToJson());
            }
            return Error("无操作权限！");
        }



        /// <summary>
        /// 综合调度工单管理查询分页
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public ActionResult GetComprehensiveList(string keyword = "", string starttime = "", string endtime = "", int pageindex = 1, int pagesize = 10, int state = -1, int type = 0)
        {
            int recordCount = 0;
            string sql = "";
            if (!string.IsNullOrEmpty(keyword))
            {
                sql += $" and wocontent like '%{keyword}%' or wocode like '%{keyword}%' or proserphone like '%{keyword}%'";
            }
            if (type > 0)
            {
                sql += $" and  swotype={type}";
            }
            if (state >= 0)
            {
                sql += $" and  wostate={state}";
            }
            Dictionary<string, object> paras = new Dictionary<string, object>();
            paras.Add("@keyword", sql);
            paras.Add("@sdate", starttime);
            paras.Add("@edate", endtime);
            paras.Add("@pageindex", pageindex);
            paras.Add("@pagesize", pagesize);
            var dataset = DbHelperSQL.RunProcedure("P_GetComWorkOrder", paras, "AllWokerOrder");
            var data = dataset.Tables[0];
            recordCount = Convert.ToInt32(dataset.Tables[1].Rows[0]["recordCount"]);
            var workklist = DataTableExtensions.ToList<TotalWorkeOrderDto>(data).ToList();
            foreach (var item in workklist)
            {
                item.WstateName = GetWoState(item.fwotype, item.swotype, item.wostate);
            }
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = workklist,
                total = recordCount

            };
            return Content(obj.ToJson());
        }

        private string GetWoState(int ftype,int stype,int wstate)
        {
            string WstateName = "";
            if (ftype == 1000 || ftype == 4000)
            {
                WstateName = Utility.EnumHelper.GetDescription((EnumWorkOrderState)wstate);
            }
            else if (ftype == 2000)
            {
                switch (stype)
                {
                    case (int)EnumWorkOrder.Dev:
                        WstateName= Utility.EnumHelper.GetDescription((EnumDevWorkOrderState)wstate);
                        break;
      
                    case (int)EnumWorkOrder.Mat:
                        WstateName= Utility.EnumHelper.GetDescription((EnumMatWorkOrderState)wstate);
                        break; 
                    case (int)EnumWorkOrder.Hos:
                        WstateName= Utility.EnumHelper.GetDescription((EnumHosWorkOrderState)wstate);
                        break;
                    case (int)EnumWorkOrder.BloodDis:
                    case (int)EnumWorkOrder.RedDis:
                    case (int)EnumWorkOrder.OrtherDis:
                    case (int)EnumWorkOrder.UrgDis:
                        WstateName = Utility.EnumHelper.GetDescription((EnumDisWorkOrderState)wstate);
                        break;
                    case (int)EnumWorkOrder.Serve:
                    case (int)EnumWorkOrder.TakeBlood:
                        WstateName = Utility.EnumHelper.GetDescription((EnumAccWorkOrderState)wstate);
                        break;
                }
            }
            else if (ftype == 3000)
            {
                //0 代派工 1 已转派 2 已处理 3已完结 4异常结束 5已拒单 6外协作单子
                switch (wstate)
                {
                    case (int)EnumFaultRepairWorkOrderState.neworder:
                        WstateName = "待接单";
                        break;

                    case (int)EnumFaultRepairWorkOrderState.receive:
                        WstateName = "待接单";
                        break;
                    case (int)EnumFaultRepairWorkOrderState.dealing:
                        WstateName = "已接单";
                        break;
                      
                   case (int)EnumFaultRepairWorkOrderState.arrive:
                        WstateName = "已到达";
                        break;
                    case (int)EnumFaultRepairWorkOrderState.dealed:
                        WstateName = "待评价";
                        break;
                    case (int)EnumFaultRepairWorkOrderState.exceptionended:
                        WstateName = "已完成";
                        break;
                    case (int)EnumFaultRepairWorkOrderState.finish:
                        WstateName = "已完成";
                        break;
                }
            }
            return WstateName;
        }
    }
}