Exemplo n.º 1
0
    private async IAsyncEnumerable <string> RunCheck(Session session, IVoucherDetailQuery filt, CsvParser parsed)
    {
        if (filt.IsDangerous())
        {
            throw new SecurityException("检测到弱检索式");
        }

        var res = session.Accountant.SelectVouchersAsync(filt.VoucherQuery);
        var lst = new List <BankItem>(parsed.Items);

        await foreach (var v in res)
        {
            foreach (var d in v.Details)
            {
                if (!d.IsMatch(filt.ActualDetailFilter()))
                {
                    continue;
                }

                var obj1 = lst.FirstOrDefault(b
                                              => (b.Currency == null || d.Currency == b.Currency) &&
                                              (b.Fund - d.Fund !.Value).IsZero() && b.Date == v.Date);
                if (obj1 != null)
                {
                    lst.Remove(obj1);
                    continue;
                }

                var obj2 = lst
                           .Where(b => (b.Currency == null || d.Currency == b.Currency) && (b.Fund - d.Fund !.Value).IsZero())
                           .OrderBy(b => Math.Abs((b.Date - v.Date !.Value).TotalDays))
                           .FirstOrDefault();
                if (obj2 != null)
                {
                    lst.Remove(obj2);
                    continue;
                }

                yield return($"{v.ID.Quotation('^')} {v.Date.AsDate()} {d.Content} {d.Fund.AsCurrency(d.Currency)}\n");
            }
        }

        foreach (var b in lst)
        {
            yield return($"{b.Raw}\n");
        }
    }
Exemplo n.º 2
0
    private async IAsyncEnumerable <string> RunUnmark(Session session, IVoucherDetailQuery filt)
    {
        if (filt.IsDangerous())
        {
            throw new SecurityException("检测到弱检索式");
        }

        var cnt    = 0;
        var cntAll = 0;
        var res    = session.Accountant.SelectVouchersAsync(filt.VoucherQuery);
        var ops    = new List <Voucher>();

        await foreach (var v in res)
        {
            foreach (var d in v.Details)
            {
                if (!d.IsMatch(filt.ActualDetailFilter()))
                {
                    continue;
                }

                cntAll++;
                if (d.Remark == null)
                {
                    continue;
                }

                d.Remark = null;
                cnt++;
            }

            ops.Add(v);
        }

        await session.Accountant.UpsertAsync(ops);

        yield return($"{cntAll} selected\n");

        yield return($"{cnt} unmarked\n");
    }
Exemplo n.º 3
0
    private async IAsyncEnumerable <string> RunMark(Session session, IVoucherDetailQuery filt, CsvParser parsed,
                                                    string marker)
    {
        if (filt.IsDangerous())
        {
            throw new SecurityException("检测到弱检索式");
        }

        var marked    = 0;
        var remarked  = 0;
        var converted = 0;
        var res       = await session.Accountant.SelectVouchersAsync(filt.VoucherQuery).ToListAsync();

        var ops = new List <Voucher>();

        foreach (var b in parsed.Items)
        {
            bool Trial(bool date)
            {
                var resx = date
                    ? res.Where(v => v.Date == b.Date)
                    : res.OrderBy(v => v.Date.HasValue
                        ? Math.Abs((v.Date.Value - b.Date).TotalDays)
                        : double.PositiveInfinity);
                var voucher = resx
                              .FirstOrDefault(v => v.Details.Any(d
                                                                 => (b.Currency == null || d.Currency == b.Currency) &&
                                                                 (d.Fund !.Value - b.Fund).IsZero() && d.IsMatch(filt.ActualDetailFilter())));

                if (voucher == null)
                {
                    return(false);
                }

                var o = voucher.Details.First(d
                                              => (b.Currency == null || d.Currency == b.Currency) &&
                                              (d.Fund !.Value - b.Fund).IsZero() && d.IsMatch(filt.ActualDetailFilter()));

                if (o.Remark == null)
                {
                    marked++;
                }
                else if (o.Remark == marker)
                {
                    remarked++;
                }
                else
                {
                    converted++;
                }

                o.Remark = marker;
                ops.Add(voucher);
                return(true);
            }

            if (Trial(true) || Trial(false))
            {
                continue;
            }

            yield return($"{b.Raw}\n");
        }

        await session.Accountant.UpsertAsync(ops);

        yield return($"{marked} marked\n");

        yield return($"{remarked} remarked\n");

        yield return($"{converted} converted\n");
    }
 /// <summary>
 ///     是否包含弱检索式
 /// </summary>
 /// <param name="query">记账凭证细目映射检索式</param>
 /// <returns>若包含则为<c>true</c>,否则为<c>false</c></returns>
 public static bool IsDangerous(this IVoucherDetailQuery query)
 => query.VoucherQuery.IsDangerous() && query.ActualDetailFilter().IsDangerous();