using static StackExchange.Redis.Role;

namespace OASystem.API.OAMethodLib
{
    public class IOOperatorHelper
    {

        /// <summary>
        /// 写文件（覆盖）
        /// </summary>
        /// <param name="content"></param>
        /// <param name="dir"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public string Write_CoverFile(string content, string dir, string fileName)
        {
            if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir);   //该路径不存在时，在当前文件目录下创建文件夹"导出.."
            //不存在该文件时先创建
            String filePath = dir + fileName;
            System.IO.StreamWriter file1 = new System.IO.StreamWriter(filePath, false);     //文件已覆盖方式添加内容
            file1.Write(content);                                                              //保存数据到文件
            file1.Close();                                                                  //关闭文件
            file1.Dispose();                                                                //释放对象
            return filePath;
        }

        public string Read(string filePath)
        {
            string result = string.Empty;

            try
            {
                // 读取文本文件
                using (StreamReader sr = new StreamReader(filePath))
                {
                    string line;
                    // ReadLine()一行一行的循环读取
                    //当然可以直接ReadToEnd()读到最后
                    while ((line = sr.ReadLine()) != null)
                    {
                        result += line;
                    }
                }
            }
            catch (Exception e)
            {

            }

            result = JsonConvert.DeserializeObject<string>(result);

            return result;
        }

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="sourceFilePath"></param>
        /// <param name="RecycleBinPath"></param>
        /// <returns></returns>
        public void MoveFile(string sourceFileFullPath, string RecycleBinPath)
        {
            //移动文件
            if (!string.IsNullOrEmpty(sourceFileFullPath) && System.IO.File.Exists(sourceFileFullPath))
            {
                string fileName = Path.GetFileName(sourceFileFullPath);
                System.IO.File.Move(sourceFileFullPath, RecycleBinPath + fileName);
            }
        }
    }
}
