File size: 4,090 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Data;
using Dapper;
using SCM.Application.Common;
using SCM.Domain.Controle;

namespace SCM.Infrastructure.Persistence.Repositories;

public sealed class PrecedenciaRepository : IPrecedenciaRepository
{
    private readonly IUnitOfWork _uow;

    public PrecedenciaRepository(IUnitOfWork uow) => _uow = uow;

    private IDbConnection Conn => _uow.Connection;
    private IDbTransaction? Tx => _uow.CurrentTransaction;

    private const string Columns = "id, item_cq_id, item_prd_id, tipo_precedencia";

    public async Task<IReadOnlyList<PrecedenciaItem>> GetByItemCqAsync(Guid itemCqId, CancellationToken ct = default)
    {
        const string sql = $"SELECT {Columns} FROM precedencias_item WHERE item_cq_id = @Id";
        var rows = await Conn.QueryAsync<PrecedenciaItem>(
            new CommandDefinition(sql, new { Id = itemCqId }, Tx, cancellationToken: ct));
        return rows.ToList();
    }

    public async Task<IReadOnlyList<PrecedenciaItem>> GetSucessoresDoPrdAsync(Guid itemPrdId, CancellationToken ct = default)
    {
        const string sql = $"SELECT {Columns} FROM precedencias_item WHERE item_prd_id = @Id";
        var rows = await Conn.QueryAsync<PrecedenciaItem>(
            new CommandDefinition(sql, new { Id = itemPrdId }, Tx, cancellationToken: ct));
        return rows.ToList();
    }

    public async Task<bool> ItemExistsAsync(Guid itemProgramadoId, CancellationToken ct = default)
    {
        var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition(
            "SELECT COUNT(*) FROM itens_programados WHERE id = @Id",
            new { Id = itemProgramadoId }, Tx, cancellationToken: ct));
        return count > 0;
    }

    public async Task<TipoAtividade?> GetTipoAtividadeAsync(Guid itemProgramadoId, CancellationToken ct = default)
    {
        var tipo = await Conn.ExecuteScalarAsync<int?>(new CommandDefinition(
            "SELECT tipo_atividade FROM itens_programados WHERE id = @Id",
            new { Id = itemProgramadoId }, Tx, cancellationToken: ct));
        return tipo.HasValue ? (TipoAtividade)tipo.Value : null;
    }

    public async Task<decimal> GetQuantidadeItemAsync(Guid itemProgramadoId, CancellationToken ct = default)
    {
        return await Conn.ExecuteScalarAsync<decimal>(new CommandDefinition(
            "SELECT COALESCE(quantidade, 0) FROM itens_programados WHERE id = @Id",
            new { Id = itemProgramadoId }, Tx, cancellationToken: ct));
    }

    public async Task ReplaceAsync(Guid itemCqId, IReadOnlyList<PrecedenciaItem> precedencias, CancellationToken ct = default)
    {
        await Conn.ExecuteAsync(new CommandDefinition(
            "DELETE FROM precedencias_item WHERE item_cq_id = @Id",
            new { Id = itemCqId }, Tx, cancellationToken: ct));

        const string sql = """
            INSERT INTO precedencias_item (id, item_cq_id, item_prd_id, tipo_precedencia)
            VALUES (@Id, @ItemCqId, @ItemPrdId, @TipoPrecedencia)
            """;
        foreach (var p in precedencias)
            await Conn.ExecuteAsync(new CommandDefinition(sql, new
            {
                p.Id, p.ItemCqId, p.ItemPrdId, p.TipoPrecedencia
            }, Tx, cancellationToken: ct));
    }

    public async Task<IReadOnlyList<PrdEstado>> GetEstadoPredecessorasAsync(Guid itemCqId, CancellationToken ct = default)
    {
        // Produção Normal mais recente de cada PRD predecessora do CQ.
        const string sql = """
            SELECT DISTINCT ON (p.item_programado_id)
                   p.item_programado_id AS item_prd_id, p.qt_prevista, p.qt_executada,
                   p.avanco, p.tipo_lib_cq, p.data
            FROM producoes_controle p
            JOIN precedencias_item pr ON pr.item_prd_id = p.item_programado_id
            WHERE pr.item_cq_id = @Id AND p.classificacao = 0
            ORDER BY p.item_programado_id, p.data DESC
            """;
        var rows = await Conn.QueryAsync<PrdEstado>(
            new CommandDefinition(sql, new { Id = itemCqId }, Tx, cancellationToken: ct));
        return rows.ToList();
    }
}