zjh
2023-12-27 7077fa8ff7f18b2991deafaba9f9dc65d1d27f51
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
package com.ltkj.web.controller.ssm;
 
import cn.hutool.core.date.DateTime;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltkj.common.core.domain.AjaxResult;
import com.ltkj.hosp.domain.TjCustomer;
import com.ltkj.hosp.domain.TjOrder;
import com.ltkj.hosp.domain.TjSendRecord;
import com.ltkj.hosp.domain.TjSendTemplate;
import com.ltkj.hosp.service.ITjCustomerService;
import com.ltkj.hosp.service.ITjOrderService;
import com.ltkj.hosp.service.ITjSendRecordService;
import com.ltkj.hosp.service.ITjSendTemplateService;
import com.ltkj.web.controller.email.MailConfig;
import com.ltkj.web.controller.message.ALiYunSMSConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: zjh
 * @Date: 2023/3/20 15:13
 */
@RestController
@RequestMapping("/ali/send")
@Api(tags = "短信或邮箱发送接口")
@Slf4j
public class aliController {
    //阿里云短信服务配置类
    @Resource
    ALiYunSMSConfig aLiYunSMSConfig;
    @Resource
    private ITjOrderService tjOrderService;
    @Resource
    private MailConfig mailConfig;
    @Resource
    private ITjSendRecordService sendRecordService;
    @Resource
    private ITjCustomerService tjCustomerService;
    @Resource
    private ITjSendTemplateService templateService;
 
    /**
     * 批量发送邮件
     */
    @PostMapping("/sendEmail")
    @ApiOperation(value = "批量发送邮件")
    public AjaxResult sendEmail(@RequestBody Map<String, Object> map) {
        Integer tempId = (Integer) map.get("tempId");
        List<String> orderIds = (List<String>) map.get("orderIds");
        TjSendTemplate byId = templateService.getById(Long.valueOf(tempId));
        List<TjCustomer> errorCus = new ArrayList();
        if (byId != null) {
            for (String orderId : orderIds) {
                TjSendRecord tjSendRecord = new TjSendRecord();
                LambdaQueryWrapper<TjOrder> wq = new LambdaQueryWrapper<>();
                wq.eq(TjOrder::getOrderId, orderId);
                TjOrder one = tjOrderService.getOne(wq);
                if (one != null) {
                    TjCustomer tjCustomer = tjCustomerService.selectTjCustomerByCusId(one.getUserId());
                    if (tjCustomer.getCusEmail() != null) {
                        String subject = "路泰体检中心";
                        String head = "尊敬的" + tjCustomer.getCusName() + "先生/女士,";
                        String content = head + byId.getTempContent();
                        tjSendRecord.setTjNumber(one.getTjNumber());
                        tjSendRecord.setType("1");
                        tjSendRecord.setSendStatus("1");
                        tjSendRecord.setSendTime(new DateTime());
                        tjSendRecord.setSubject(subject);
                        tjSendRecord.setContent(content);
 
                        SimpleMailMessage message = new SimpleMailMessage();
                        message.setFrom("1138758025@qq.com");
                        // 邮件接收人(可以使用 String[] 发送给多个用户)
                        message.setTo(tjCustomer.getCusEmail());
                        message.setSubject(subject);
                        message.setText(content);
                        try {
                            mailConfig.getMailSender("1138758025@qq.com", "lwsbmlbqgpaqfgdb").send(message);
                            one.setSendEmail("1");
                            tjOrderService.updateById(one);
 
                        } catch (MailException e) {
                            e.printStackTrace();
                        }
                    }
                    errorCus.add(tjCustomer);
                    tjSendRecord.setSendStatus("0");
                    sendRecordService.save(tjSendRecord);
                }
            }
        }
        return AjaxResult.success(errorCus);
    }
 
 
    /**
     * 批量发送短信
     */
    @PostMapping("/sendMessage")
    @ApiOperation(value = "批量发送短信")
    public AjaxResult sendMessage(@RequestBody Map<String, Object> map) {
        Integer tempId = (Integer) map.get("tempId");
        List<String> orderIds = (List<String>) map.get("orderIds");
        TjSendTemplate byId = templateService.getById(Long.valueOf(tempId));
        if (byId != null) {
            for (String orderId : orderIds) {
                TjSendRecord tjSendRecord = new TjSendRecord();
                LambdaQueryWrapper<TjOrder> wq = new LambdaQueryWrapper<>();
                wq.eq(TjOrder::getOrderId, orderId);
                TjOrder one = tjOrderService.getOne(wq);
                if (one != null) {
                    TjCustomer tjCustomer = tjCustomerService.selectTjCustomerByCusId(one.getUserId());
                    if (tjCustomer.getCusEmail() != null) {
                        String subject = "路泰体检中心";
                        String head = "尊敬的" + tjCustomer.getCusName() + "先生/女士,";
                        String content = head + byId.getTempContent();
                        tjSendRecord.setTjNumber(one.getTjNumber());
                        tjSendRecord.setType("2");
                        tjSendRecord.setSendStatus("1");
                        tjSendRecord.setSendTime(new DateTime());
                        tjSendRecord.setSubject(subject);
                        tjSendRecord.setContent(content);
                        one.setSendMessage("1");
                        tjOrderService.updateById(one);
                    }
                    sendRecordService.save(tjSendRecord);
                }
            }
        }
        return AjaxResult.success("短信发送成功!");
    }
 
 
    /**
     * 发送短信
     */
//    @GetMapping("/sendMessage")
//    @ApiOperation(value = "批量发送短信")
//    public AjaxResult sendMessage(){
//        try {
//            // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。
//            // 以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
//            Client client = aLiYunSMSConfig.createClient();
//            SendSmsRequest sendSmsRequest = new SendSmsRequest()
//                    .setSignName("阿里云短信测试")
//                    .setTemplateCode("SMS_154950909")
//                    .setPhoneNumbers("15706053782")
//                    .setTemplateParam("{\"code\":\"1234\"}");
//            RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//            // 复制代码运行请自行打印 API 的返回值
//            client.sendSmsWithOptions(sendSmsRequest, runtime);
//        } catch (TeaException error) {
//            // 如有需要,请打印 error
//            Common.assertAsString(error.message);
//        } catch (Exception _error) {
//            TeaException error = new TeaException(_error.getMessage(), _error);
//            // 如有需要,请打印 error
//            Common.assertAsString(error.message);
//        }
//        return AjaxResult.success();
//    }
}