using System.Threading.Tasks;
using Datory;
using GxPress.EnumConst;
using System.Transactions;
namespace GxPress.Service.Implement
{
    public partial class FlowService
    {
        /// <summary>
        /// 撤回
        /// </summary>
        /// <param name="flowId"></param>
        /// <returns></returns>
        public async Task<bool> RecallAsync(int flowId, int userId)
        {
            try
            {
                using (var transactions = new TransactionScope())
                {
                    var flow = await _flowRepository.GetAsync(flowId);
                    if (flow == null || flow.UserId != userId)
                        throw new Common.Exceptions.BusinessException("非本人操作!");
                    //删除middle
                    await middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Inbox.GetHashCode()).Where(nameof(Entity.Middle.Middle.MiddleId), flowId).WhereNot(nameof(Entity.Middle.Middle.UserId), flow.UserId));
                    //修改状态
                    await middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.Inbox.GetHashCode()).Where(nameof(Entity.Middle.Middle.MiddleId), flowId).Where(nameof(Entity.Middle.Middle.UserId), flow.UserId).Set(nameof(Entity.Middle.Middle.IsRecall), true));
                    transactions.Complete();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}