using System.Threading.Tasks;
using Datory;
using GxPress.Common.Exceptions;
using System.Transactions;
namespace GxPress.Service.Implement.Missive
{
    public partial class MissiveService
    {
        /// <summary>
        /// 站内信撤回
        /// </summary>
        /// <param name="missiveId"></param>
        /// <returns></returns>
        public async Task<bool> RecallAsync(int missiveId)
        {
            //删除
            var missive = await _missiveRepository.GetAsync(missiveId);
            if (missive == null)
                throw new BusinessException("站内信不存在！");
            // var createdDate = Convert.ToDateTime(missive.CreatedDate);
            // if (createdDate.AddMinutes(5) > DateTime.Now)
            //     throw new BusinessException("发送数据超过5分钟不能撤回！");
            using (TransactionScope transactionScope = new TransactionScope())
            {
                //删除middle下面的站内信
                await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), missive.Id).Where(nameof(Entity.Middle.Middle.FolderType), 5).Where(nameof(Entity.Middle.Middle.IsAdmin), false));
                //修改为草稿箱
                 await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), missive.Id).Where(nameof(Entity.Middle.Middle.FolderType), 5).Where(nameof(Entity.Middle.Middle.IsAdmin), true).Set(nameof(Entity.Middle.Middle.NoticeAddresseeType),2));
                 
                transactionScope.Complete();
            }
            return true;
        }
    }
}