/// <summary>
 /// 根据相册编号返回相册详情.包括封面图片数据
 /// </summary>
 /// <param name="_AlbumNo"></param>
 /// <returns></returns>
 public Album GetAlbumInfoByNo(string _AlbumNo)
 {
     Album _album = null;
     try
     {
         IAlbumManager manager = new PhotoApp.SQLitBLL.AlbumManager();
         _album = manager.GetAlbumInfoByNo(_AlbumNo);
         _album.AlbumTitleImg = PhotoApp.SQLitBLL.ImgManager.Instance.GetImgData(_album.AlbumTitleImg, false);//获取缩略图数据
     }
     catch (Exception ex)
     {
     }
     return _album;
 }
 /// <summary>
 /// 获取所有相册列表
 /// </summary>
 /// <returns></returns>
 public List<Album> GetAllAlbumlist()
 {
     List<Album> listdata = null;
     IAlbumManager manager = new PhotoApp.SQLitBLL.AlbumManager();
     listdata=manager.GetAlbumList();
     if (listdata != null && listdata.Count > 0)
     {
         foreach (Album _album in listdata)
         {
             _album.AlbumRootPath = "";
             _album.AlbumTitleImg = "";
         }
     }
     return listdata;
 }
        /// <summary>
        /// 添加照片
        /// </summary>
        /// <param name="_AlbumNo">照片编号</param>
        /// <param name="_photoinfo">照片信息</param>
        /// <returns></returns>
        public Model.ManagerStateMsg AddPhoto(string _AlbumNo, Model.PhotoInfo _photoinfo)
        {
            Model.ManagerStateMsg msg = new Model.ManagerStateMsg(false, "方法 AddPhoto 执行出现错误!");
            try
            {
                SQLiteParameter[] Ablumpars = new SQLiteParameter[1];
                Ablumpars[0] = new SQLiteParameter("@AlbumNo", DbType.String);
                Ablumpars[0].Value = _photoinfo.PhotoNO;
                string strAblumTols="select count(PhotoNO) as 'PhotoTols' from Photo where [email protected] group by AlbumNo";
                DataTable dt = SQLiteHelper.Instance.ExecuteDataTable(strAblumTols, Ablumpars);
                int AblumPhotosCount = 0;
                if (dt != null && dt.Rows.Count > 0)
                {
                    AblumPhotosCount = Convert.ToInt32(dt.Rows[0]["PhotoTols"]);
                }

                SQLiteParameter[] pars = new SQLiteParameter[9];
                pars[0] = new SQLiteParameter("@PhotoNO", DbType.String);
                pars[0].Value = _photoinfo.PhotoNO;
                pars[1] = new SQLiteParameter("@AlbumNo", DbType.String);
                pars[1].Value = _AlbumNo;
                pars[2] = new SQLiteParameter("@PhotoName", DbType.String);
                pars[2].Value = _photoinfo.PhotoName;
                pars[3] = new SQLiteParameter("@PhotoRemark", DbType.String);
                pars[3].Value = _photoinfo.PhotoRemark;
                pars[4] = new SQLiteParameter("@IsTitleImg", DbType.Int32);
                if (AblumPhotosCount != 0)
                {
                    pars[4].Value = 0;
                }
                else {
                    pars[4].Value = 1;
                }

                pars[5] = new SQLiteParameter("@PhotoData", DbType.String);
                pars[5].Value = _photoinfo.PhotoData;
                pars[6] = new SQLiteParameter("@PhotoMiniData", DbType.String);
                pars[6].Value = _photoinfo.PhotoMiniData;
                pars[7] = new SQLiteParameter("@PhotoCreateDate", DbType.String);
                pars[7].Value = _photoinfo.PhotoCreateDate;
                pars[8] = new SQLiteParameter("@PhotoUpdateDate", DbType.String);
                pars[8].Value = _photoinfo.PhotoUpdateDate;

                string strSQL = @"insert into Photo (PhotoNO,AlbumNo,PhotoName,PhotoRemark,IsTitleImg,PhotoData,PhotoMiniData,PhotoCreateDate,PhotoUpdateDate)
                 values (@PhotoNO,@AlbumNo,@PhotoName,@PhotoRemark,@IsTitleImg,@PhotoData,@PhotoMiniData,@PhotoCreateDate,@PhotoUpdateDate)";
                int _uprows = SQLiteHelper.Instance.ExecuteNonQuery(strSQL, pars);
                if (_uprows > 0)
                {
                    if (AblumPhotosCount == 0)
                    {
                        AlbumManager manager = new AlbumManager();
                        manager.ChangeTitleImg(_AlbumNo, _photoinfo.PhotoNO);
                    }

                    msg.ReturnValue = _photoinfo;
                    msg.Statevalue = true;
                    msg.MsgInfo = "保存成功";
                }
                else
                {
                    msg.MsgInfo = "写入数据库失败!";
                }

                msg.ReturnValue = _photoinfo;
                msg.Statevalue = true;
                msg.MsgInfo = "保存成功";
            }
            catch (Exception ex)
            {
                msg.Statevalue = false;
                msg.MsgInfo = ex.Message;
            }
            return msg;
        }
        /// <summary>
        /// 删除照片
        /// </summary>
        /// <param name="_AlbumNo"></param>
        /// <param name="_photoNo"></param>
        /// <returns></returns>
        public Model.ManagerStateMsg DeletePhoto(string _AlbumNo, string _photoNo)
        {
            Model.ManagerStateMsg msg = new Model.ManagerStateMsg(false, "方法 EditPhoto 执行出现错误!");

            try
            {
                SQLiteParameter[] pars = null;
                pars = new SQLiteParameter[1];
                pars[0] = new SQLiteParameter("@PhotoNO", DbType.String);
                pars[0].Value = _photoNo;

                string strQuerySQL = "delete from Photo where [email protected]";
                int _upRowscount = SQLiteHelper.Instance.ExecuteNonQuery(strQuerySQL, pars);
                if (_upRowscount > 0)
                {
                    strQuerySQL = "select count(PhotoNO) as 'PhotoTols' from Photo where IsTitleImg=1 and [email protected]";
                    SQLiteParameter[] Titleimgpars = new SQLiteParameter[1];
                    Titleimgpars[0] = new SQLiteParameter("@AlbumNo", DbType.String);
                    Titleimgpars[0].Value = _AlbumNo;
                    DataTable _dt = SQLiteHelper.Instance.ExecuteDataTable(strQuerySQL, Titleimgpars);
                    if (_dt != null && _dt.Rows.Count > 0)
                    {
                    }
                    else
                    {
                        _dt = SQLiteHelper.Instance.ExecuteDataTable("select AlbumNo,PhotoNO from Photo order by PhotoCreateDate limit 0,1", null);
                        if (_dt != null && _dt.Rows.Count > 0)
                        {
                            string strPhotoNO = _dt.Rows[0]["PhotoNO"].ToString();
                            string strAlbumNo=_dt.Rows[0]["AlbumNo"].ToString();
                            AlbumManager manager = new AlbumManager();
                            manager.ChangeTitleImg(strAlbumNo, strPhotoNO);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                msg.Statevalue = false;
                msg.MsgInfo = ex.Message;
            }

            return msg;
        }
        /// <summary>
        /// 创建相册
        /// </summary>
        /// <param name="SubDirs"></param>
        private List<Album> CreateAlbum(string[] SubDirs)
        {
            List<Album> list = new List<Album>();
            if (SubDirs != null && SubDirs.Length > 0)
            {
                IAlbumManager manager = new AlbumManager();
                Album newalbume = null;
                DirectoryInfo _dir=null;
                foreach (string strpath in SubDirs)
                {
                    _dir=new DirectoryInfo(strpath);
                    newalbume = new Album(_dir.Name, "");
                    ManagerStateMsg state = manager.SaveAlbum(newalbume);
                    if (CreateAlbumEvent != null)
                    {
                        if (state.Statevalue)
                        {
                            list.Add((Album)state.ReturnValue);
                        }
                        CreateAlbumEvent(_dir.Name, state.Statevalue);
                    }
                }
            }
            else
            {
                if (SyncEndEvent != null)
                {
                    SyncEndEvent(null, null);
                }
            }

            return list;
        }
        /// <summary>
        /// 添加照片
        /// </summary>
        /// <param name="abums">已创建相册列表</param>
        /// <param name="SubDirs">相册文件夹数组</param>
        private void AddPhotoToAlbum(List<Album> abums,string[] SubDirs)
        {
            if (SubDirs != null && SubDirs.Length > 0)
            {
                IAlbumManager manager = new AlbumManager();
                List<string> ImgList = null;

                DirectoryInfo _dir = null;
                foreach (string AblumPath in SubDirs)
                {
                    _dir = new DirectoryInfo(AblumPath);
                    if (Directory.Exists(AblumPath))
                    {
                        ImgList = Directory.GetFiles(AblumPath, "*.jpg").ToList();
                        ImgList.AddRange(Directory.GetFiles(AblumPath, "*.bmp").ToList());
                        ImgList.AddRange(Directory.GetFiles(AblumPath, "*.png").ToList());

                    }
                    AddPhotosToAlume(ImgList,GetAlbumByName(abums,_dir.Name));//添加照片至相册
                }
            }
            else
            {
                if (SyncEndEvent != null)
                {
                    SyncEndEvent(null, null);
                }
            }
        }