using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.report
{
    public class ServerMonitorController : BaseController
    {
        SysInformationHelper sys = new SysInformationHelper();
        /// <summary>
        /// 获取内存(RAM)（已使用） - 使用率
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetRAMUsingInfo()
        {
            try
            {
                string data = ((float)(((sys.PhysicalMemory - sys.MemoryAvailable) * 100) / sys.PhysicalMemory)).ToString();
                return Success("获取成功", data);
            }
            catch (Exception ex)
            {
                return Error("获取失败");
            }
        }

        /// <summary>
        /// 获取CPU（已使用） - 使用率
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetCPUUsingInfo()
        {
            try
            {
                string data = sys.CpuLoad.ToString();
                return Success("获取成功", data);
            }
            catch (Exception ex)
            {
                return Error("获取失败");
            }
        }

        /// <summary>
        /// 获取某一盘使用率（已使用） - 使用率
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetHardDiscUsingInfo()
        {
            try
            {
                string HardDisc = Configs.GetValue("VideoHardDisc");
                if (!string.IsNullOrEmpty(HardDisc))
                {
                    HardDisc = HardDisc.ToUpper();  //转换成大写
                    string data = sys.GetDrivePer1(HardDisc).ToString();
                    return Success("获取成功", data);
                }
                else
                {
                    return Error("获取参数失败");
                }
            }
            catch (Exception ex)
            {
                return Error("获取失败");
            }
        }

        /// <summary>
        /// 删除语音文件（清空语音信息，暂时注释）
        /// </summary>
        /// <param name="startTime"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult DelVideo(string startTime)
        {
            DataTable dt = new DataTable();
            string sql = " ";
            sql += " 1 = 1 and isnull(FilePath,'') <> '' ";
            if (!string.IsNullOrEmpty(startTime))
            {
                sql += " and TalkStartTime <= '" + Convert.ToDateTime(startTime.Trim()) + "' ";
            }
            dt = new BLL.T_Call_CallRecords().GetList(sql).Tables[0];
            List<Model.T_Call_CallRecords> modelList = new BLL.T_Call_CallRecords().DataTableToList(dt);
            if (modelList.Count > 0)
            {
                int DeleteCount = 0;
                for (int i = 0; i < modelList.Count; i++)
                {
                    string CallId = modelList[i].CallId.ToString();
                    string FilePath = modelList[i].FilePath.ToString();
                    bool b = Utils.DeleteFile(FilePath);
                    if (b)
                    {
                        DeleteCount += 1;
                    }
                    #region 清空语音信息 关闭功能 注释
                    ////判断音频是否删除，删除后修改表语音为null
                    //if (b && !string.IsNullOrEmpty(CallId))
                    //{
                    //    string sql2 = "update T_Call_CallRecords set FilePath = null where CallId = " + CallId + "";
                    //    DBUtility.DbHelperSQL.ExecuteSql(sql2); //修改FilePath为null
                    //}
                    #endregion
                }
                return Success("操作完成", DeleteCount);
            }
            else
            {
                return Error("没有需要的操作信息");
            }
        }
    }
}