using System.Threading.Tasks;
using GxPress.Entity.WorkFlowDto;
using System.Linq;
using Datory;
using System.Collections.Generic;

namespace GxPress.Service.Implement
{
    public partial class FlowService
    {
        public async Task<bool> AddInboxAsyc(FlowDto flow)
        {
            var user = await _userRepository.GetAsync(flow.UserId);
            var process = await _processRepository.GetAsync(flow.ProcessId);
            //获取收件人
            //获取抄送人
            var flowDtos = await _flowTodoRepository.GetListByFlowIdAsync(flow.Id);
            var addressUserIds = flowDtos.Where(n => n.Type == nameof(GxPress.EnumConst.TodoTypeConst.ApproverCheck)).Select(n => n.UserId).ToList();
            var ccUserIds = flowDtos.Where(n => n.Type == nameof(GxPress.EnumConst.TodoTypeConst.CarbonCopy)).Select(n => n.UserId).ToList();
            //收件人
            var addressUsers = await _userRepository.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), addressUserIds));
            //抄送人
            var ccUsers = await _userRepository.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), ccUserIds));
            //中间件
            var middles = new List<Entity.Middle.Middle>();
            var middle = new Entity.Middle.Middle
            {
                MiddleId = flow.Id,
                FolderType = GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode(),
                IsTop = false,
                AttributeValue = 1,
                ParentId = 0,
                UserId = flow.UserId,
                MiddleSonId = 0,
                IsAdmin = false,
                NoticeAddresseeType = 0,
                Title = $"{user.Name}{process.Name}申请",
                IsRead = false,
                TitleLong = "",
                Content = "",
                AddressUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addressUsers.Select(n => n.Name)),
                AddressUser = Common.Tools.StringUtils.ObjectCollectionToString(addressUsers.Select(n => n.Name), 10),
                CcUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(ccUsers.Select(n => n.Name)),
                CcUser = Common.Tools.StringUtils.ObjectCollectionToString(ccUsers.Select(n => n.Name), 10),
            };
            middles.Add(middle);
            var users = addressUsers.ToList();
            users.AddRange(ccUsers);
            foreach (var item in users.Distinct())
            {
                middle = new Entity.Middle.Middle
                {
                    MiddleId = flow.Id,
                    FolderType = GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode(),
                    IsTop = false,
                    AttributeValue = 1,
                    ParentId = 0,
                    UserId = flow.UserId,
                    MiddleSonId = 0,
                    IsAdmin = false,
                    NoticeAddresseeType = 0,
                    Title = $"{user.Name}{process.Name}申请",
                    IsRead = false,
                    TitleLong = "",
                    Content = "",
                    AddressUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(addressUsers.Select(n => n.Name)),
                    AddressUser = Common.Tools.StringUtils.ObjectCollectionToString(addressUsers.Select(n => n.Name), 10),
                    CcUserComplete = Common.Tools.StringUtils.ObjectCollectionToString(ccUsers.Select(n => n.Name)),
                    CcUser = Common.Tools.StringUtils.ObjectCollectionToString(ccUsers.Select(n => n.Name), 10),
                };
                middles.Add(middle);
            }
            await middleRepository.InsertAsync(middles);
            return true;
        }
    }
}