Subversion Repositories SmartDukaan

Rev

Rev 36374 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.entity.catalog;

import com.spice.profitmandi.common.util.FormattingUtils;
import com.spice.profitmandi.dao.convertor.LocalDateTimeAttributeConverter;
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
import com.spice.profitmandi.dao.enumuration.catalog.SchemeType;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * This class basically contains scheme details
 *
 * @author ashikali
 */
@NamedQueries({ @NamedQuery(name = "scheme.selectSchemesByRetailerIdsSchemeIds", query = "select sr.schemeId from"
                + " SchemeRegion sr join PartnerRegion pr on pr.regionId=sr.regionId where pr.fofoId in :fofoIds and sr.schemeId in :schemeIds"),

                @NamedQuery(name = "Scheme.selectSchemeByModelsPartnerTypeFofoId", query = "select s from Scheme s"
                                + " join SchemeItem si on si.schemeId = s.id" + " join SchemeRegion sr on sr.schemeId = s.id"
                                + " join PartnerRegion pr on pr.regionId=sr.regionId"
                                + " where pr.fofoId in :fofoIds and s.partnerType in :partnerTypes" + " and si.catalogId in :catalogIds"
                                + " and :onDate between s.startDateTime and s.endDateTime" +
                                // " and s.endDateTime >= :onDate" +
                                " and s.activeTimestamp is not null"),

                @NamedQuery(name = "Scheme.selectSchemesByBrandsAndFofoRegion", query = "select distinct s from Scheme s"
                                + " join SchemeItem si on si.schemeId = s.id"
                                + " join Item i on i.catalogItemId = si.catalogId"
                                + " join SchemeRegion sr on sr.schemeId = s.id"
                                + " where i.brand in :brands"
                                + " and s.activeTimestamp is not null"
                                + " and s.partnerType in :partnerTypes"
                                + " and s.startDateTime <= :onDate"
                                + " and COALESCE(s.expireTimestamp, s.endDateTime) >= :onDate"
                                + " and (si.endDate is null or si.endDate >= :onDate)"
                                + " and (sr.regionId = 5 or sr.regionId in :regionIds)"
                                + " order by s.id desc"),

                @NamedQuery(name = "Scheme.selectSchemesByBrands", query = "select distinct s from Scheme s"
                                + " join SchemeItem si on si.schemeId = s.id"
                                + " join Item i on i.catalogItemId = si.catalogId"
                                + " where i.brand in :brands"
                                + " and s.activeTimestamp is not null"
                                + " and s.partnerType in :partnerTypes"
                                + " and s.startDateTime <= :onDate"
                                + " and COALESCE(s.expireTimestamp, s.endDateTime) >= :onDate"
                                + " and (si.endDate is null or si.endDate >= :onDate)"
                                + " order by s.id desc"),

                @NamedQuery(name = "Scheme.selectActiveSchemesByFofoRegion", query = "select distinct s from Scheme s"
                                + " join SchemeItem si on si.schemeId = s.id"
                                + " join SchemeRegion sr on sr.schemeId = s.id"
                                + " where s.activeTimestamp is not null"
                                + " and s.partnerType in :partnerTypes"
                                + " and s.startDateTime <= :onDate"
                                + " and COALESCE(s.expireTimestamp, s.endDateTime) >= :onDate"
                                + " and (si.endDate is null or si.endDate >= :onDate)"
                                + " and (sr.regionId = 5 or sr.regionId in :regionIds)"
                                + " order by s.id desc"),

                @NamedQuery(name = "Scheme.selectAllActiveSchemes", query = "select distinct s from Scheme s"
                                + " join SchemeItem si on si.schemeId = s.id"
                                + " where s.activeTimestamp is not null"
                                + " and s.partnerType in :partnerTypes"
                                + " and s.startDateTime <= :onDate"
                                + " and COALESCE(s.expireTimestamp, s.endDateTime) >= :onDate"
                                + " and (si.endDate is null or si.endDate >= :onDate)"
                                + " order by s.id desc")

                /*
                 * ,
                 * 
                 * @NamedQuery(name="scheme.getImeiMarginsPaidOnBillingMonth", query=
                 * "select new com.spice.profitmandi.dao.repository.catalog.ImeiMarginModel(" +
                 * ") from LineItemImei li1" +
                 * "         left join LineItemImei li2 on (li1.serialNumber = li2.serialNumber and li1.id < li2.id)"
                 * + "         join LineItem li on li.id = li1.lineItemId" +
                 * "         join Order o on o.id = li.orderId" +
                 * "         join Item ci on li.itemId = ci.id" +
                 * "         join SchemeItem si on si.catalogId = ci.catalogItemId" +
                 * "         join Scheme s1 on s1.id = si.schemeId" +
                 * "         join SchemeRegion sr on sr.schemeId = s1.id" +
                 * "         join PartnerRegion pr on (pr.regionId = sr.regionId and pr.fofoId in (0, o.id))"
                 * + "         left join PartnerTypeChange ptc on ptc.fofoId = o.retailerId" +
                 * "         left join PartnerTypeChange ptc2 on (ptc2.fofoId = ptc.fofoId and ptc.createTimestamp < ptc2.createTimestamp)"
                 * + "where li2.id is null" + "  and ptc2.createTimestamp is null" +
                 * "  and o.billing_timestamp between :startDate and :endDate" +
                 * "  and '2022-12-01' between s1.start_date_time and s1.end_date_time" +
                 * "  and ((ptc.partnerType is null and s1.partner_type in ('ALL', 'NEW'))" +
                 * "    or s1.partnerType in (ptc.partnerType, 'ALL'))" +
                 * "  and s1.activeTimestamp is not null" + "  and s1.amountType = 'PERCENTAGE'"
                 * + "group by li.serial_number) as set1\n" + "")
                 */

})

@NamedNativeQueries({

                @NamedNativeQuery(name = "scheme.getActiveScheme", query = "select si.catalog_id from catalog.scheme s join "
                                + " fofo.scheme_item si on s.id = si.scheme_id join catalog.item i on i.catalog_item_id = si.catalog_id"
                                + " where s.active_timestamp is not null and start_date_time <= :startDate and end_date_time >= :startDate"
                                + " and (si.start_date is null or :startDate between si.start_date and si.end_date)"
                                + " and s.type = 'SPECIAL_SUPPORT' and i.hsnCode = '85171300' order by active_timestamp desc "),

                @NamedNativeQuery(name = "scheme.selectMissedActivationSale", query = "select cs.id as scheme_id, ai.serial_number, sr.order_id, ii.id as inventory_item_id, ai.activation_timestamp"
                                + " from fofo.activated_imei ai "
                                + "   join fofo.inventory_item ii on ai.serial_number = ii.serial_number "
                                + "   join catalog.item i on i.id = ii.item_id "
                                + "   join catalog.scheme cs on ai.activation_timestamp >= cs.start_date_time and ai.activation_timestamp <= cs.end_date_time  "
                                + "   join fofo.scheme_item si on (si.scheme_id = cs.id and i.catalog_item_id = si.catalog_id and (si.start_date is null or ai.activation_timestamp between si.start_date and si.end_date)) "
                                + "       join fofo.scan_record sr on (sr.inventory_item_id = ii.id)"
                                + "   join fofo.scheme_region sre on sre.scheme_id = cs.id"
                                + "   join cs.partner_region pr on (pr.region_id = sre.region_id and (pr.fofo_id = ii.fofo_id or pr.fofo_id = 0))"
                                + "     left join fofo.scheme_in_out sio on (sio.scheme_id = cs.id and sio.inventory_item_id = ii.id) "
                                + " where cs.active_timestamp is not null " + " and cs.type ='SPECIAL_SUPPORT'"
                                + " and sr.type = 'SALE'" + " and (sio.id is null or sio.status='REJECTED') "
                                + " and ai.checked = false", resultSetMapping = "missedActivationSaleMapping") })

@SqlResultSetMappings({ @SqlResultSetMapping(name = "missedActivationSaleMapping", classes = {
                @ConstructorResult(targetClass = SchemesImeisModel.class, columns = {
                                @ColumnResult(name = "serial_number", type = String.class),
                                @ColumnResult(name = "activation_timestamp", type = LocalDateTime.class),
                                @ColumnResult(name = "order_id", type = Integer.class),
                                @ColumnResult(name = "inventory_item_id", type = Integer.class),
                                @ColumnResult(name = "scheme_id", type = Integer.class), }) }) })
@Entity
@Table(name = "catalog.scheme")
public class Scheme implements Serializable {

        public PartnerType getPartnerType() {
                return partnerType;
        }

        public void setPartnerType(PartnerType partnerType) {
                this.partnerType = partnerType;
        }

        private static final long serialVersionUID = 1L;

        public Scheme() {
        }

        @Id
        @Column(name = "id")
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;

        @Column(name = "`name`")
        private String name;

        @Column(name = "description")
        private String description;

        @Column(name = "`type`")
        @Enumerated(EnumType.STRING)
        private SchemeType type;

        @Column(name = "amount_type")
        @Enumerated(EnumType.STRING)
        private AmountType amountType;

        @Column(name = "base_plus_gst_calc")
        private boolean basePlusGstCalc;

        @Column(name = "amount")
        private float amount;

        @Convert(converter = LocalDateTimeAttributeConverter.class)
        @Column(name = "start_date_time")
        private LocalDateTime startDateTime = LocalDateTime.now();

        @Convert(converter = LocalDateTimeAttributeConverter.class)
        @Column(name = "end_date_time")
        private LocalDateTime endDateTime = LocalDateTime.now();

        @Convert(converter = LocalDateTimeAttributeConverter.class)
        @Column(name = "create_timestamp")
        private LocalDateTime createTimestamp = LocalDateTime.now();

        @Convert(converter = LocalDateTimeAttributeConverter.class)
        @Column(name = "active_timestamp")
        private LocalDateTime activeTimestamp = null;

        @Convert(converter = LocalDateTimeAttributeConverter.class)
        @Column(name = "expire_timestamp")
        private LocalDateTime expireTimestamp = null;

        @Column(name = "created_by")
        private int createdBy;

        //Dummy hardcode
        @Transient
        private int target;

        public int getTarget() {
                return target;
        }

        public void setTarget(int target) {
                this.target = target;
        }

        @Column
        private boolean cashback;

        @Transient
        private Set<Integer> retailerIds;

        @Transient
        private String amountModel;

        @Transient
        private float schemeValue;

        public float getSchemeValue() {
                return schemeValue;
        }

        public void setSchemeValue(float schemeValue) {
                this.schemeValue = schemeValue;
        }

        public String getSchemePeriod() {
                return FormattingUtils.formatDateMonthNonHyphenated(this.getStartDateTime()) + " - " + FormattingUtils.formatDateMonthNonHyphenated(this.getEndDateTime());
        }

        @Column(name = "partner_type")
        @Enumerated(EnumType.STRING)
        private PartnerType partnerType;

        @Override
        public String toString() {
                return "Scheme{" + "id=" + id + ", name='" + name + '\'' + ", description='" + description + '\'' + ", type="
                                + type + ", amountType=" + amountType + ", gstReversal=" + basePlusGstCalc + ", amount=" + amount
                                + ", startDateTime=" + startDateTime + ", endDateTime=" + endDateTime + ", createTimestamp="
                                + createTimestamp + ", activeTimestamp=" + activeTimestamp + ", expireTimestamp=" + expireTimestamp
                                + ", createdBy=" + createdBy + ", cashback=" + cashback + ", retailerIds=" + retailerIds
                                + ", amountModel='" + amountModel + '\'' + ", schemeValue=" + schemeValue + ", partnerType="
                                + partnerType + ", catalogStringMap=" + catalogStringMap + '}';
        }

        @Override
        public boolean equals(Object o) {
                if (this == o)
                        return true;
                if (o == null || getClass() != o.getClass())
                        return false;
                Scheme scheme = (Scheme) o;
                return id == scheme.id && basePlusGstCalc == scheme.basePlusGstCalc && Float.compare(scheme.amount, amount) == 0
                                && createdBy == scheme.createdBy && cashback == scheme.cashback
                                && Float.compare(scheme.schemeValue, schemeValue) == 0 && Objects.equals(name, scheme.name)
                                && Objects.equals(description, scheme.description) && type == scheme.type
                                && amountType == scheme.amountType && Objects.equals(startDateTime, scheme.startDateTime)
                                && Objects.equals(endDateTime, scheme.endDateTime)
                                && Objects.equals(createTimestamp, scheme.createTimestamp)
                                && Objects.equals(activeTimestamp, scheme.activeTimestamp)
                                && Objects.equals(expireTimestamp, scheme.expireTimestamp)
                                && Objects.equals(retailerIds, scheme.retailerIds) && Objects.equals(amountModel, scheme.amountModel)
                                && partnerType == scheme.partnerType && Objects.equals(catalogStringMap, scheme.catalogStringMap);
        }

        @Override
        public int hashCode() {
                return Objects.hash(id, name, description, type, amountType, basePlusGstCalc, amount, startDateTime,
                                endDateTime, createTimestamp, activeTimestamp, expireTimestamp, createdBy, cashback, retailerIds,
                                amountModel, schemeValue, partnerType, catalogStringMap);
        }

        @Transient
        private Map<Integer, String> catalogStringMap = new HashMap<>();

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getAmountModel() {
                return amountModel;
        }

        public void setAmountModel(String amountModel) {
                this.amountModel = amountModel;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getDescription() {
                return description;
        }

        public void setDescription(String description) {
                this.description = description;
        }

        public boolean isCashback() {
                return cashback;
        }

        public void setCashback(boolean cashback) {
                this.cashback = cashback;
        }

        public SchemeType getType() {
                return type;
        }

        public void setType(SchemeType type) {
                this.type = type;
        }

        public AmountType getAmountType() {
                return amountType;
        }

        public void setAmountType(AmountType amountType) {
                this.amountType = amountType;
        }

        public float getAmount() {
                return amount;
        }

        public void setAmount(float amount) {
                this.amount = amount;
        }

        public LocalDateTime getStartDateTime() {
                return startDateTime;
        }

        public void setStartDateTime(LocalDateTime startDateTime) {
                this.startDateTime = startDateTime;
        }

        public LocalDateTime getEndDateTime() {
                return endDateTime;
        }

        public void setEndDateTime(LocalDateTime endDateTime) {
                this.endDateTime = endDateTime;
        }

        public LocalDateTime getCreateTimestamp() {
                return createTimestamp;
        }

        public void setCreateTimestamp(LocalDateTime createTimestamp) {
                this.createTimestamp = createTimestamp;
        }

        public int getCreatedBy() {
                return createdBy;
        }

        public void setCreatedBy(int createdBy) {
                this.createdBy = createdBy;
        }

        public LocalDateTime getActiveTimestamp() {
                return activeTimestamp;
        }

        public void setActiveTimestamp(LocalDateTime activeTimestamp) {
                this.activeTimestamp = activeTimestamp;
        }

        public LocalDateTime getExpireTimestamp() {
                return expireTimestamp;
        }

        public void setExpireTimestamp(LocalDateTime expireTimestamp) {
                this.expireTimestamp = expireTimestamp;
        }

        public String getFormattedStartDateTime() {
                if (startDateTime == null) {
                        return null;
                }
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
                return startDateTime.format(formatter);
        }

        public String getFormattedEndDateTime() {
                if (endDateTime == null) {
                        return null;
                }
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
                return endDateTime.format(formatter);
        }

        public String getFormattedCreateTimestamp() {
                if (createTimestamp == null) {
                        return null;
                }
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
                return createTimestamp.format(formatter);
        }

        public String getFormattedActiveTimestamp() {
                if (activeTimestamp == null) {
                        return null;
                }
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
                return activeTimestamp.format(formatter);
        }

        public String getFormattedExpireTimestamp() {
                if (expireTimestamp == null) {
                        return null;
                }
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
                return expireTimestamp.format(formatter);
        }

        public Set<Integer> getRetailerIds() {
                return retailerIds;
        }

        public void setRetailerIds(Set<Integer> retailerIds) {
                this.retailerIds = retailerIds;
        }

        public String getRetailerIdsString() {
                if (retailerIds == null || retailerIds.isEmpty()) {
                        return "";
                }
                Set<String> stringRetailerIds = new HashSet<>();
                for (int retailerId : retailerIds) {
                        stringRetailerIds.add(String.valueOf(retailerId));
                }
                return String.join(", ", stringRetailerIds);
        }

        public boolean isBasePlusGstCalc() {
                return basePlusGstCalc;
        }

        public void setBasePlusGstCalc(boolean gstReversal) {
                this.basePlusGstCalc = gstReversal;
        }

        public Map<Integer, String> getCatalogStringMap() {
                return catalogStringMap;
        }

        public void setCatalogStringMap(Map<Integer, String> catalogStringMap) {
                this.catalogStringMap = catalogStringMap;
        }

        public boolean isWithinRange(LocalDateTime testDate) {
                return !(testDate.isBefore(this.startDateTime) || testDate.isAfter(endDateTime));
        }
}