ProjectSalaryNotificationJob.java 6.28 KB
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);
    }


}