Blame view

ProjectSalaryNotificationJob.java 6.28 KB
涂亚平 committed
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
package com.subsidy.job;

import com.subsidy.common.configure.RestTemplateConfig;
import com.subsidy.common.configure.WechatConfig;
import com.subsidy.common.constant.CourseNotification;
import com.subsidy.dto.wewchat.PushMsgPO;
import com.subsidy.mapper.MemberMapper;
import com.subsidy.mapper.ProjectMapper;
import com.subsidy.mapper.ProjectSalaryHistoryMapper;
import com.subsidy.mapper.ProjectSalaryNoticeMapper;
import com.subsidy.model.*;
import com.subsidy.vo.member.GetMemberListVO;
import com.subsidy.vo.wechat.AccessTokenVO;
import com.subsidy.vo.wechat.PushMsgVO;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程通知
 * </p>
 *
 * @author DengMin
 * @since 2022/2/14
 */
@Component
public class ProjectSalaryNotificationJob implements Job {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ProjectSalaryNoticeMapper projectSalaryNoticeMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private WechatConfig wechatConfig;

    @Autowired
    private RestTemplateConfig restTemplateConfig;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {

        Map<String, Object> map = jobExecutionContext.getJobDetail().getJobDataMap();
        Map<String, Object> params = (Map<String, Object>) map.get("params");
        ProjectDO projectDO = projectMapper.selectById((Long) params.get("projectId"));
        if (params != null) {
            ProjectSalaryNoticeDO projectSalaryNoticeDO = projectSalaryNoticeMapper.selectById((Long) params.get("id"));
            if (null != projectSalaryNoticeDO) {
                //需要发送通知的人(已提交)
                List<GetMemberListVO> list = memberMapper.getMemberList((Long) params.get("projectId"), (String) params.get("salaryMonth"));
                if (list != null && list.size() > 0) {
                    for (GetMemberListVO memberMonthSalaryVO : list) {
                        if (memberMonthSalaryVO != null) {
                            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wechatConfig.getAppId() + "&secret=" + wechatConfig.getAppSecret();
                            ResponseEntity<AccessTokenVO> response = restTemplateConfig.restTemplate()
                                    .exchange(url, HttpMethod.GET, null, AccessTokenVO.class);

                            PushMsgPO pushMsgDTO = new PushMsgPO();
                            pushMsgDTO.setOpenId(memberMonthSalaryVO.getOpenId());
                            pushMsgDTO.setProjectName(projectDO.getProjectName());
                            pushMsgDTO.setSalary(memberMonthSalaryVO.getSalary());
                            pushMsgDTO.setAccountType("工资");
                            pushMsgDTO.setSalaryDate(memberMonthSalaryVO.getSalaryDate());
                            pushMsgDTO.setSalaryMark(memberMonthSalaryVO.getSalaryMark());
                            String token = response.getBody().getAccess_token();
//                            pushMsg(pushMsgDTO,token);
                        }
                    }
                }
                projectSalaryNoticeDO.setStatus(CourseNotification.SENT);
                projectSalaryNoticeMapper.updateById(projectSalaryNoticeDO);
            }
        }
    }


    public void pushMsg(PushMsgPO pushMsgPO, String token){

//    https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html

        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wechatConfig.getAppId() + "&secret=" + wechatConfig.getAppSecret();
        ResponseEntity<AccessTokenVO> response = restTemplateConfig.restTemplate()
                .exchange(url, HttpMethod.GET, null, AccessTokenVO.class);

        String msgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;

//        pushMsgPO.setOpenId("ooRcY6cvS36qli-3sOVdkGJ_AODY");
        pushMsgPO.setOpenId(pushMsgPO.getOpenId());
        pushMsgPO.setAccountType(pushMsgPO.getAccountType());
        pushMsgPO.setProjectName(pushMsgPO.getProjectName());
        pushMsgPO.setSalary(pushMsgPO.getSalary());
        pushMsgPO.setSalaryDate(pushMsgPO.getSalaryDate());
//        pushMsgPO.setSalaryDate("2024年08月08日");

        String input = "{\n" +
                "    \"touser\":\"" + pushMsgPO.getOpenId() + "\",\n" +
                "    \"template_id\":\"" + wechatConfig.getTemplateId() + "\",\n" +
                "    \"url\":\"" + wechatConfig.getMsgUrl() + "\",\n" +
                "    \"data\":{\n" +
                "        \"thing3\":{\n" +
                "            \"value\":\"" + pushMsgPO.getAccountType() + "\",\n" +
                "            \"color\":\"#173177\"\n" +
                "        },\n" +
                "        \"thing2\":{\n" +
                "            \"value\":\"" + pushMsgPO.getProjectName() + "\",\n" +
                "            \"color\":\"#173177\"\n" +
                "        },\n" +
                "        \"amount4\":{\n" +
                "            \"value\":\"" + pushMsgPO.getSalary() + "\",\n" +
                "            \"color\":\"#173177\"\n" +
                "        },\n" +
                "        \"thing6\":{\n" +
                "            \"value\":\"" + pushMsgPO.getSalaryMark() + "\",\n" +
                "            \"color\":\"#173177\"\n" +
                "        },\n" +
                "        \"time8\":{\n" +
                "            \"value\":\"" + pushMsgPO.getSalaryDate() + "\",\n" +
                "            \"color\":\"#173177\"\n" +
                "        }" +
                "    }\n" +
                "}";

        HttpHeaders headers = new HttpHeaders();

        ResponseEntity<PushMsgVO> responseEntity = restTemplateConfig.restTemplate()
                .exchange(msgUrl, HttpMethod.POST, new HttpEntity<>(input.getBytes(), headers), PushMsgVO.class);
        System.out.println(responseEntity);
    }


}