Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37461 ranu 1
package com.spice.profitmandi.dao.model;
2
 
3
import javax.persistence.*;
4
import java.util.Objects;
5
 
6
/**
7
 * Distinct partner count per brand for a single RBM × warehouse × range,
8
 * used by the RBM DRR dashboard drill-down to display "how many partners
9
 * of this RBM billed each brand". Separate from
10
 * {@link RbmBrandSoldModel} because summing per-(brand,status) partner
11
 * counts would double-count partners who billed a brand across multiple
12
 * movement classes. Same auth_user → fofo_store derivation as
13
 * {@link TodayAchievedMovementModel}.
14
 */
15
@Entity
16
@NamedNativeQueries({
17
        @NamedNativeQuery(
18
                name = "RbmDrr.BrandPartnerCountByRbm",
19
                query = "SELECT li.brand                              AS brand," +
20
                        "       COUNT(DISTINCT odr.customer_id)       AS partner_count" +
21
                        " FROM (SELECT au.id AS auth_id, fs.id AS fofo_id, fs.warehouse_id" +
22
                        "       FROM auth.auth_user au" +
23
                        "                JOIN cs.position p ON p.auth_user_id = au.id" +
24
                        "                JOIN cs.partner_position pp ON pp.position_id = p.id" +
25
                        "                JOIN fofo.fofo_store fs ON fs.id = pp.partner_id" +
26
                        "       WHERE pp.partner_id != 0" +
27
                        "         AND p.category_id = 18" +
28
                        "         AND p.escalation_type = 'L1'" +
29
                        "         AND fs.active = 1" +
30
                        "         AND fs.internal = false" +
31
                        "       UNION ALL" +
32
                        "       SELECT au.id AS auth_id, fs.id AS fofo_id, fs.warehouse_id" +
33
                        "       FROM auth.auth_user au" +
34
                        "                JOIN cs.position p ON p.auth_user_id = au.id" +
35
                        "                JOIN cs.partner_position pp ON pp.position_id = p.id" +
36
                        "                JOIN cs.region r ON pp.region_id = r.id" +
37
                        "                JOIN cs.partner_region pr ON pr.region_id = pp.region_id" +
38
                        "                JOIN fofo.fofo_store fs ON fs.id = pr.fofo_id" +
39
                        "       WHERE pp.partner_id = 0" +
40
                        "         AND p.category_id = 18" +
41
                        "         AND fs.active = 1" +
42
                        "         AND fs.internal = false" +
43
                        "         AND p.escalation_type = 'L1') a" +
44
                        " JOIN transaction.order odr ON odr.customer_id = a.fofo_id" +
45
                        " JOIN transaction.lineitem li ON li.order_id = odr.id" +
46
                        " WHERE a.auth_id = :authId" +
47
                        "   AND a.warehouse_id = :warehouseId" +
48
                        "   AND odr.created_timestamp >= :startDate" +
49
                        "   AND odr.created_timestamp <  :endDate" +
50
                        "   AND odr.refund_timestamp IS NULL" +
51
                        "   AND li.brand NOT IN ('Smartdukaan', 'Dummy', 'FOC', 'Live Demo')" +
52
                        " GROUP BY li.brand",
53
                resultSetMapping = "RbmDrrBrandPartnerCountByRbm")
54
})
55
@SqlResultSetMappings({
56
        @SqlResultSetMapping(
57
                name = "RbmDrrBrandPartnerCountByRbm",
58
                classes = {
59
                        @ConstructorResult(
60
                                targetClass = RbmBrandPartnerCountModel.class,
61
                                columns = {
62
                                        @ColumnResult(name = "brand", type = String.class),
63
                                        @ColumnResult(name = "partner_count", type = Integer.class)
64
                                }
65
                        )
66
                }
67
        )
68
})
69
public class RbmBrandPartnerCountModel {
70
 
71
    private String brand;
72
    private Integer partnerCount;
73
 
74
    @Id
75
    @GeneratedValue(strategy = GenerationType.IDENTITY)
76
    private Long id;
77
 
78
    public RbmBrandPartnerCountModel() {
79
    }
80
 
81
    public RbmBrandPartnerCountModel(String brand, Integer partnerCount) {
82
        this.brand = brand;
83
        this.partnerCount = partnerCount;
84
    }
85
 
86
    public String getBrand() {
87
        return brand;
88
    }
89
 
90
    public void setBrand(String brand) {
91
        this.brand = brand;
92
    }
93
 
94
    public Integer getPartnerCount() {
95
        return partnerCount;
96
    }
97
 
98
    public void setPartnerCount(Integer partnerCount) {
99
        this.partnerCount = partnerCount;
100
    }
101
 
102
    @Override
103
    public boolean equals(Object o) {
104
        if (this == o) return true;
105
        if (o == null || getClass() != o.getClass()) return false;
106
        RbmBrandPartnerCountModel that = (RbmBrandPartnerCountModel) o;
107
        return Objects.equals(brand, that.brand) && Objects.equals(partnerCount, that.partnerCount);
108
    }
109
 
110
    @Override
111
    public int hashCode() {
112
        return Objects.hash(brand, partnerCount);
113
    }
114
 
115
    @Override
116
    public String toString() {
117
        return "RbmBrandPartnerCountModel{brand='" + brand + "', partnerCount=" + partnerCount + '}';
118
    }
119
}