Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.model;

import javax.persistence.*;
import java.util.Objects;

/**
 * Distinct partner count per brand for a single RBM × warehouse × range,
 * used by the RBM DRR dashboard drill-down to display "how many partners
 * of this RBM billed each brand". Separate from
 * {@link RbmBrandSoldModel} because summing per-(brand,status) partner
 * counts would double-count partners who billed a brand across multiple
 * movement classes. Same auth_user → fofo_store derivation as
 * {@link TodayAchievedMovementModel}.
 */
@Entity
@NamedNativeQueries({
        @NamedNativeQuery(
                name = "RbmDrr.BrandPartnerCountByRbm",
                query = "SELECT li.brand                              AS brand," +
                        "       COUNT(DISTINCT odr.customer_id)       AS partner_count" +
                        " FROM (SELECT au.id AS auth_id, fs.id AS fofo_id, fs.warehouse_id" +
                        "       FROM auth.auth_user au" +
                        "                JOIN cs.position p ON p.auth_user_id = au.id" +
                        "                JOIN cs.partner_position pp ON pp.position_id = p.id" +
                        "                JOIN fofo.fofo_store fs ON fs.id = pp.partner_id" +
                        "       WHERE pp.partner_id != 0" +
                        "         AND p.category_id = 18" +
                        "         AND p.escalation_type = 'L1'" +
                        "         AND fs.active = 1" +
                        "         AND fs.internal = false" +
                        "       UNION ALL" +
                        "       SELECT au.id AS auth_id, fs.id AS fofo_id, fs.warehouse_id" +
                        "       FROM auth.auth_user au" +
                        "                JOIN cs.position p ON p.auth_user_id = au.id" +
                        "                JOIN cs.partner_position pp ON pp.position_id = p.id" +
                        "                JOIN cs.region r ON pp.region_id = r.id" +
                        "                JOIN cs.partner_region pr ON pr.region_id = pp.region_id" +
                        "                JOIN fofo.fofo_store fs ON fs.id = pr.fofo_id" +
                        "       WHERE pp.partner_id = 0" +
                        "         AND p.category_id = 18" +
                        "         AND fs.active = 1" +
                        "         AND fs.internal = false" +
                        "         AND p.escalation_type = 'L1') a" +
                        " JOIN transaction.order odr ON odr.customer_id = a.fofo_id" +
                        " JOIN transaction.lineitem li ON li.order_id = odr.id" +
                        " WHERE a.auth_id = :authId" +
                        "   AND a.warehouse_id = :warehouseId" +
                        "   AND odr.created_timestamp >= :startDate" +
                        "   AND odr.created_timestamp <  :endDate" +
                        "   AND odr.refund_timestamp IS NULL" +
                        "   AND li.brand NOT IN ('Smartdukaan', 'Dummy', 'FOC', 'Live Demo')" +
                        " GROUP BY li.brand",
                resultSetMapping = "RbmDrrBrandPartnerCountByRbm")
})
@SqlResultSetMappings({
        @SqlResultSetMapping(
                name = "RbmDrrBrandPartnerCountByRbm",
                classes = {
                        @ConstructorResult(
                                targetClass = RbmBrandPartnerCountModel.class,
                                columns = {
                                        @ColumnResult(name = "brand", type = String.class),
                                        @ColumnResult(name = "partner_count", type = Integer.class)
                                }
                        )
                }
        )
})
public class RbmBrandPartnerCountModel {

    private String brand;
    private Integer partnerCount;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    public RbmBrandPartnerCountModel() {
    }

    public RbmBrandPartnerCountModel(String brand, Integer partnerCount) {
        this.brand = brand;
        this.partnerCount = partnerCount;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Integer getPartnerCount() {
        return partnerCount;
    }

    public void setPartnerCount(Integer partnerCount) {
        this.partnerCount = partnerCount;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RbmBrandPartnerCountModel that = (RbmBrandPartnerCountModel) o;
        return Objects.equals(brand, that.brand) && Objects.equals(partnerCount, that.partnerCount);
    }

    @Override
    public int hashCode() {
        return Objects.hash(brand, partnerCount);
    }

    @Override
    public String toString() {
        return "RbmBrandPartnerCountModel{brand='" + brand + "', partnerCount=" + partnerCount + '}';
    }
}