Subversion Repositories SmartDukaan

Rev

Rev 34468 | Rev 34492 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
34468 vikas.jang 1
package com.spice.profitmandi.dao.service;
2
 
3
import com.spice.profitmandi.common.model.ProfitMandiConstants;
4
import com.spice.profitmandi.common.util.Utils;
5
import com.spice.profitmandi.dao.entity.auth.AuthUser;
6
import com.spice.profitmandi.dao.entity.catalog.Bid;
7
import com.spice.profitmandi.dao.entity.catalog.Liquidation;
8
import com.spice.profitmandi.dao.entity.dtr.User;
9
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
10
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
11
import com.spice.profitmandi.dao.repository.catalog.BidRepository;
12
import com.spice.profitmandi.dao.repository.catalog.LiquidationRepository;
13
import com.spice.profitmandi.dao.repository.cs.CsService;
14
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
15
import com.spice.profitmandi.service.AuthService;
16
import com.spice.profitmandi.service.NotificationService;
17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.mail.javamail.JavaMailSender;
21
import org.springframework.stereotype.Component;
22
 
23
import java.util.ArrayList;
24
import java.util.List;
25
 
26
@Component
27
public class BidServiceImpl implements BidService {
28
    private static final Logger LOGGER = LogManager.getLogger(BidServiceImpl.class);
29
    @Autowired
30
    AuthRepository authRepository;
31
    @Autowired
32
    AuthService authService;
33
    @Autowired
34
    JavaMailSender mailSender;
35
    @Autowired
36
    UserRepository userRepository;
37
    @Autowired
38
    CsService csService;
39
    @Autowired
40
    BidRepository bidRepository;
41
    @Autowired
42
    LiquidationRepository liquidationRepository;
43
    @Autowired
44
    NotificationService notificationService;
45
 
46
    @Override
47
    public void sendMailToRBM(double netAmountInHand, double totalPayableAmount, int fofoId) throws Exception {
48
        User user = userRepository.selectById(fofoId);
49
        int rbmId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, fofoId);
50
        AuthUser authUser = authRepository.selectById(rbmId);
51
 
52
        String[] emailTo = new String[] { authUser.getEmailId() };
53
        //String[] emailTo = new String[] { "vjangra856@gmail.com" };
54
 
55
        String[] ccTo = {user.getEmailId()};
56
        String subject = "Dispatch On Hold(Bidding)";
57
        String message = String.format(
58
                "Dear Team,\n\n"
59
                        + "This is to inform you that the material for %s, valued at Rs.%s, "
60
                        + "is on hold until the payment against the stock is received. Kindly "
61
                        + "top up your wallet with an amount of Rs.%s to proceed with the dispatch.",
62
                user.getFirstName() + " " + user.getLastName(), totalPayableAmount, (totalPayableAmount - netAmountInHand)
63
        );
64
 
65
        Utils.sendMailWithAttachments(mailSender, emailTo, ccTo, subject, message);
66
    }
67
 
68
    @Override
69
    public void sendSummaryMailToUserAndManagers(int userId, List<Bid> bids, Liquidation liquidation) throws Exception {
70
        String content = this.generateContent(bids, liquidation, ProfitMandiConstants.BID_ENUM.CLOSED);
71
        AuthUser authUser = authRepository.selectById(userId);
72
        String[] emailTo = {authUser.getEmailId()};
73
 
74
        String[] ccTo = authService.getAllManagers(userId).stream().map(AuthUser::getEmailId).toArray(String[]::new);
75
        String subject = "Liquidation Report";
76
        String message = "Dear Team,\n\n"
77
                + "Liquidation has been completed. Here is the summary:\n\n"
78
                + content;
79
 
80
        LOGGER.info("mailMessage: {}",message);
81
        Utils.sendMailWithAttachments(mailSender, emailTo, ccTo, subject, message);
82
    }
83
 
84
    @Override
85
    public void notifyPartner(int userId, List<Bid> bids, Liquidation liquidation, ProfitMandiConstants.BID_ENUM status) throws Exception {
86
        User user = userRepository.selectById(userId);
87
        List<Bid> topBid = bidRepository.selectBidByLiquidationId(bids.get(0).getLiquidationId());
88
        String message = this.generateBidStatusMessage(user.getFirstName()+" "+user.getLastName(), bids.get(0), topBid.get(0).getBiddingAmount());
34470 vikas.jang 89
        notificationService.sendWhatsappMessage(message, "Bid Update!", user.getMobileNumber());
34468 vikas.jang 90
    }
91
 
92
    private String generateContent(List<Bid> bids, Liquidation liquidation, ProfitMandiConstants.BID_ENUM status){
93
        StringBuilder sb = new StringBuilder();
94
        if (status.equals(ProfitMandiConstants.BID_ENUM.CLOSED)){
95
            sb.append("<h3>Congratulations your BID has ").append(status).append("</h3>");
96
        } else {
97
            sb.append("<h3>Sorry your BID has ").append(status).append("</h3>");
98
        }
99
        sb.append("<h4>Bidding Summary</h3>");
100
        sb.append("<p><strong>Liquidation ID:</strong> ").append(liquidation.getId()).append("</p>");
101
        sb.append("<p><strong>Catalog ID:</strong> ").append(liquidation.getCatalogId()).append("</p>");
102
        sb.append("<p><strong>Total Quantity Available:</strong> ").append(liquidation.getQuantity()).append("</p>");
103
        sb.append("<p><strong>End Date:</strong> ").append(liquidation.getEndDate()).append("</p>");
104
 
105
        sb.append("<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>");
106
        sb.append("<thead>");
107
        sb.append("<tr>")
108
                .append("<th>Bidder ID (Fofo)</th>")
109
                .append("<th>Item Name</th>")
110
                .append("<th>Bid Amount</th>")
111
                .append("<th>Quantity</th>")
112
                .append("<th>Status</th>")
113
                .append("<th>Bid Time</th>")
114
                .append("</tr>");
115
        sb.append("</thead>");
116
        sb.append("<tbody>");
117
 
118
        for (Bid bid : bids) {
119
            sb.append("<tr>")
120
                    .append("<td>").append(bid.getFofoId()).append("</td>")
121
                    .append("<td>").append(bid.getItemName() != null ? bid.getItemName() : "-").append("</td>")
122
                    .append("<td>").append(bid.getBiddingAmount()).append("</td>")
123
                    .append("<td>").append(bid.getQuantity()).append("</td>")
124
                    .append("<td>").append(bid.getStatus().name()).append("</td>")
125
                    .append("<td>").append(bid.getCreatedAt()).append("</td>")
126
                    .append("</tr>");
127
        }
128
 
129
        sb.append("</tbody>");
130
        sb.append("</table>");
131
 
132
        return sb.toString();
133
    }
134
 
135
    public void cancelYesterdayProcessBid(int id) throws Exception {
136
        Bid bid = bidRepository.selectById(id);
137
        bid.setStatus(ProfitMandiConstants.BID_ENUM.CANCELLED);
138
        Liquidation liquidation = liquidationRepository.selectLiquidationById(bid.getLiquidationId());
139
        List<Bid> bids = bidRepository.selectBidByLiquidationId(bid.getLiquidationId());
140
        this.sendSummaryMailToUserAndManagers(liquidation.getCreatedBy(), bids, liquidation);
141
        List<Bid> partnerBid = new ArrayList<>();
142
        partnerBid.add(bid);
143
        this.notifyPartner(bid.getFofoId(), partnerBid, liquidation, ProfitMandiConstants.BID_ENUM.CANCELLED);
144
    }
145
 
146
    private String generateBidStatusMessage(String fofoName, Bid bidData, double topBidAmount) {
147
        StringBuilder message = new StringBuilder();
148
        message.append("Hi ").append(fofoName).append(",👋\n\n");
149
 
150
        switch (bidData.getStatus()) {
151
            case CLOSED:
152
                message.append("Your bid for ").append(bidData.getItemName()).append(" has been confirmed!🎉\n\n")
153
                    .append("🧾Bid Details:\n")
154
                    .append("* Bid Amount: ₹").append(String.format("%.2f", bidData.getBiddingAmount())).append("\n")
155
                    .append("* Quantity: ").append(bidData.getQuantity()).append("\n")
156
                    .append("* Confirmation Date: ").append(bidData.getCreatedAt()).append("\n\n")
157
                    .append("Thank you for participating. We'll dispatch your order soon.");
158
                break;
159
 
160
            case CANCELLED:
161
                message.append("We regret to inform you that your bid for ").append(bidData.getItemName()).append(" was not selected this time.\n\n")
162
                    .append("🧾Bid Details:\n")
163
                    .append("* Bid Amount: ₹").append(String.format("%.2f", bidData.getBiddingAmount())).append("\n")
164
                    .append("* Quantity: ").append(bidData.getQuantity()).append("\n")
165
                    .append("* Bid Date: ").append(bidData.getCreatedAt()).append("\n")
166
                        .append("Qualified Bid Amount: ").append(topBidAmount).append("\n\n")
167
                    .append("Thank you for your participation. Stay tuned for more opportunities!");
168
                break;
169
 
170
            case PENDING:
171
                message.append("Your bid for ").append(bidData.getItemName()).append(" is currently pending.⏳\n\n")
172
                    .append("Bid Details:\n")
173
                    .append("* Bid Amount: ₹").append(String.format("%.2f", bidData.getBiddingAmount())).append("\n")
174
                    .append("* Quantity: ").append(bidData.getQuantity()).append("\n")
175
                    .append("* Bid Date: ").append(bidData.getCreatedAt()).append("\n\n")
176
                    .append("We'll notify you once the bidding period ends.");
177
                break;
178
        }
179
 
180
        return message.toString();
181
    }
182
}