kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.nanometer.smartlab.email;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nanometer.smartlab.entity.EmailStatus;
import com.nanometer.smartlab.entity.OpeWarehouseReserve;
import com.nanometer.smartlab.entity.SysReagent;
import com.nanometer.smartlab.entity.SysUser;
import com.nanometer.smartlab.entity.enumtype.EmailSendStatus;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.exception.ExceptionEnumCode;
import com.nanometer.smartlab.service.*;
import com.nanometer.smartlab.util.EmailSend;
import com.nanometer.smartlab.util.HttpUtil;
import net.sf.cglib.asm.$ByteVector;
import org.apache.commons.lang.StringUtils;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import org.omg.CORBA.NameValuePair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
/**
 * Created by wjd on 18/6/18.
 */
@Component
public class EmailSendSchedule {
    @Lazy
    @Resource
    private OpeOrderService opeOrderService;
 
    private static Logger logger = Logger.getLogger(EmailSendSchedule.class);
 
    @Resource
    private SysReagentService sysReagentService;
    @Resource
    private OpeWarehouseReserveService opeWarehouseReserveService;
    @Resource
    private SysUserService userService;
    @Resource
    private SysSupplierService sysSupplierService;
 
    @Scheduled(fixedDelayString = "${emailSend.schedule.delay}")
    public void EmailSendSchedule(){
 
        List<EmailStatus> list = null;
        try {
            int count = this.opeOrderService.getUnsendEmailCount();
            if (count > 0) {
                list = opeOrderService.getUnsendEmailList();
            }
        } catch (Exception e) {
            logger.error(e);
        }
 
        if(list != null){
 
            for (EmailStatus email: list) {
                try {
                    EmailSend.send(email.getEmailAddress(), email.getApplyCode());
                    //  发送成功,更新邮件发送状态为:已发送
                    email.setStatus(EmailSendStatus.SEND_SUCCESS);
                    this.opeOrderService.updateEmailStatus(email);
                } catch (BusinessException e) {
                    //  发送失败,更新邮件发送状态为:发送失败
                    email.setStatus(EmailSendStatus.SEND_FAIL);
                    this.opeOrderService.updateEmailStatus(email);
                    logger.warn("操作失败。", e);
                } catch (Exception e) {
                    logger.error(e);
                }
            }
        }
    }
 
 
//        @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次
    @Scheduled(cron = "0 0 8 * * ?")//每天8点执行一次
    public void EmailSendStock(){
        //收件人为人员角色为库管员的邮箱
        List<String> revicerList = userService.getLibrarianEmail();
        //先查试剂表 安全库存大于0,然后去查库存,实际库存小于安全库存的列表
        List<SysReagent> sysReagentList = new ArrayList<>();
        //已经填写安全库存的试剂
        List<SysReagent> alreadyFillIn = sysReagentService.selectReagentSafetyNum();
        if (alreadyFillIn.size() > 0) {
            for (SysReagent reagent : alreadyFillIn) {
                int actualNum = opeWarehouseReserveService.countByReagentId(reagent.getId());
                if (reagent.getSafetynum() > actualNum) {
                    reagent.setActualnum(actualNum);
                    sysReagentList.add(reagent);
                }
            }
        }
        if(sysReagentList.size() > 0 && revicerList.size() > 0 ){
            StringBuffer content = new StringBuffer("<html><body>");
            content.append("<div style=\"line-height:1.7;color:#000000;font-size:14px;font-family:Arial\">");
            content.append("<table border=\"1\">");
            content.append("<tbody>");
            content.append("<tr>");
            content.append("<th>试剂名称</th>");
            content.append("<th>产品编号</th>");
            content.append("<th>CAS</th>");
            content.append("<th>试剂类型</th>");
            content.append("<th>供应商</th>");
            content.append("<th>厂家</th>");
            content.append("<th>规格</th>");
            content.append("<th>包装</th>");
            content.append("<th>安全库存</th>");
            content.append("<th>实际库存</th>");
            content.append("</tr>");
            for (SysReagent sysReagent : sysReagentList){
                content.append("<tr>");
                content.append("<td>"+sysReagent.getName()+"</td>");
                content.append("<td>"+sysReagent.getProductSn()+"</td>");
                content.append("<td>"+sysReagent.getCas()+"</td>");
 
                content.append("<td>"+sysReagent.getReagentType()+"</td>");//
                content.append("<td>"+sysReagent.getSupplierName()+"</td>");
 
                content.append("<td>"+sysReagent.getProductHomeName()+"</td>");
 
                content.append("<td>"+sysReagent.getReagentFormat()+"</td>");//
 
                content.append("<td>"+sysReagent.getReagentUnit()+"</td>");//
                content.append("<td>"+sysReagent.getSafetynum()+"</td>");
                content.append("<td>"+sysReagent.getActualnum()+"</td>");
                content.append("</tr>");
            }
            content.append("</tbody></table></div>");
            content.append("</body></html>");
 
            try {
                EmailSend.sendArrivalEmail(revicerList,content.toString(),"-待补货试剂");
                //  发送成功,更新邮件发送状态为:已发送
            } catch (BusinessException e) {
                //  发送失败,更新邮件发送状态为:发送失败
                logger.warn("操作失败。", e);
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }
 
 
 
    @Value("${reagent.detail.price.api}")
    private String reagentApi;
 
    private final static String send_supplier_name = "国药集团化学试剂苏州有限公司";
    private final static BigDecimal SALE_85 = BigDecimal.valueOf(0.85);
    private final static Map discountMap = new HashMap<BigDecimal,String>(){{
        put(SALE_85, "85折价格");
    }};
 
    /**
    * @Description: 姑苏实验室 发给admin管理员邮箱 国药试剂(价格不同的)信息  执行时间 :每天早上2点
    * @date 2021/8/19 9:06
    */
    @Scheduled(cron = "0 0 2 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次
    public void ReagentPriceSend(){
        //0.限制在姑苏实验室
        if (StringUtils.isBlank(reagentApi) || reagentApi.length() == 0) return;
 
        try {
            //1.获取管理员邮箱
            SysUser admin = userService.getSysUserByAccount("admin");
            if (admin == null ||StringUtils.isBlank(admin.getEmail())) throw new BusinessException(ExceptionEnumCode.PARAM_NULL,"找不到管理员或者管理员邮箱信息");
 
            //2.获取国药试剂
            Long supplierId = sysSupplierService.getSysSupplierIdByname(send_supplier_name);
            if (supplierId==null)
                throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST, "没有找到与(" + send_supplier_name + ")相关供应商,请检查最近信息更新");
 
            List<SysReagent> guoyao_reagents = sysReagentService.getSysReagentListNew(null, null, supplierId, 1, null, null, null);
            if (guoyao_reagents.size() < 1)
                throw new BusinessException(ExceptionEnumCode.PARAM_NULL, "供应商:" + send_supplier_name + ",没有查询到相关试剂");
 
            List<JSONObject> sendDataList = new ArrayList<>();
            for (SysReagent reagent : guoyao_reagents) {
                String productSn = reagent.getProductSn();
                List params = new ArrayList<BasicNameValuePair>() {{
                    add(new BasicNameValuePair("goodsErpCode", productSn));
                }};
                String res = HttpUtil.doGet(reagentApi, params);
                JSONObject resJson = JSONObject.parseObject(res);
                JSONArray data = resJson.getJSONArray("data");
                JSONObject dataJson = data.getJSONObject(0);
                Integer totalRows = dataJson.getInteger("totalRows");
                if (totalRows != 1) continue;
 
                JSONArray listApiGoods = dataJson.getJSONArray("listApiGoods");
                if (listApiGoods.size() != 1) logger.info("产品编号:" + productSn + ",接口查询到多个数据,无法判断");
 
                JSONObject goodJson = listApiGoods.getJSONObject(0);
                String goodsName = goodJson.getString("goodsName");
                BigDecimal goodsCostPrice = goodJson.getBigDecimal("goodsCostPrice");
                BigDecimal price = reagent.getPrice();
                //保留两位
                BigDecimal price_afterDiscount = SALE_85.multiply(goodsCostPrice).setScale(2, RoundingMode.HALF_UP);
                if (price_afterDiscount.compareTo(price) > 0) {
                    sendDataList.add(new JSONObject() {{
                        put("goodsName", goodsName);
                        put("goodsCostPrice", goodsCostPrice);
                        put("priceAfterDiscount", price_afterDiscount);
                        put("currentSystemPrice", price);
                        put("goodsCode", productSn);
                    }});
                }
            }
            //制作表格
                StringBuilder content = new StringBuilder("<html><body>");
                content.append("<div style=\"line-height:1.7;color:#000000;font-size:14px;font-family:Arial\">");
                content.append("<table border=\"1\">");
                content.append("<tbody>");
                content.append("<tr>");
                content.append("<th>产品编号</th>");
                content.append("<th>产品名称</th>");
                content.append("<th>官网售价</th>");
                content.append("<th>").append(discountMap.get(SALE_85)).append("</th>");
                content.append("<th>当前系统价格</th>");
                content.append("</tr>");
                for (JSONObject goodJson : sendDataList){
                    content.append("<tr>")
                            .append("<td>").append(goodJson.getString("goodsCode")).append("</td>")
                            .append("<td>").append(goodJson.getString("goodsName")).append("</td>")
                            .append("<td>").append(goodJson.getBigDecimal("goodsCostPrice")).append("</td>")
                            .append("<td>").append(goodJson.getBigDecimal("priceAfterDiscount")).append("</td>")
                            .append("<td>").append(goodJson.getBigDecimal("currentSystemPrice")).append("</td>")
                            .append("</tr>");
                }
                content.append("</tbody></table></div>");
                content.append("</body></html>");
 
 
                //发送邮件
                EmailSend.sendArrivalEmail(Collections.singletonList(admin.getEmail()),content.toString(),"-国药试剂价格变动");
                    //  发送成功,更新邮件发送状态为:已发送
 
        } catch (BusinessException e) {
            e.printStackTrace();
            logger.warn(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.warn("系统内部错误,请检查代码");
        }
 
 
    }
 
}