Newer
Older
huludao / src / main / java / com / newfiber / api / pc / controller / WxUserTemplateController.java
package com.newfiber.api.pc.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.newfiber.api.core.annotation.SysLog;
import com.newfiber.api.core.commons.*;
import com.newfiber.api.pc.dao.wechat.GetNfMonitorWechatUserMapper;
import com.newfiber.api.pc.dao.wechat.WxTemplateMapper;
import com.newfiber.api.pc.dto.ConfigUserTemplate;
import com.newfiber.api.pc.model.entity.WxTemplate;
import com.newfiber.api.pc.model.entity.WxUserTemplate;
import com.newfiber.api.pc.model.nfmonitorwechat.NfWechatUser;
import com.newfiber.api.pc.service.WxTemplateService;
import com.newfiber.api.pc.service.WxUserTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @ClassName WxUserTemplateController
 * @Description TODO
 * @Author 张鸿志
 * @Date 2021年7月26日20:16:41 20:16
 * Version 1.0
 **/
@RestController
@RequestMapping("/wxUserTemplate")
@Api(value = "WxUserTemplateController",tags = "给用户分配指定的模板")
public class WxUserTemplateController {



    @Autowired
    private WxUserTemplateService wxUserTemplateService;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${wx.pushUrl}")
    private String wxPushUrl;

    @Autowired
    private WxTemplateMapper wxTemplateMapper;

    @Autowired
    private WxTemplateService wxTemplateService;

    @Autowired
    private GetNfMonitorWechatUserMapper getNfMonitorWechatUserMapper;


    @PostMapping("/config")
    @ApiOperation("给模板分配用户(包含新增与修改)")
    @SysLog(actionType = "1",value = "给模板分配用户(包含新增与修改)")
    public ResultObj config(@RequestBody ConfigUserTemplate configUserTemplate){
        wxUserTemplateService.config(configUserTemplate);
        return ResultObj.ok();
    }


    @PostMapping("/templatePage")
    @ApiOperation("分页查找模板")
    @SysLog(actionType = "4",value = "分页查找微信模板")
    public ResultObj<PageResultObject<WxTemplate>> selectTemplatePage(@RequestBody PageRequestObject<String> pageRequestObject){
        String templateName = pageRequestObject.getObject();
        EntityWrapper<WxTemplate> entityWrapper = new EntityWrapper<>();
        if(!StringUtils.isEmpty(templateName)){
            entityWrapper.like("title",templateName);
        }
        Page<WxTemplate> wxTemplatePage = wxTemplateService.selectPage(new Page<>(pageRequestObject.getCurrent(), pageRequestObject.getSize()), entityWrapper);
        int count = wxTemplateService.selectCount(entityWrapper);
        return new ResultObj<>(ResultCode.OK,new PageResultObject<>(pageRequestObject.getCurrent(), pageRequestObject.getSize(),(long)count,wxTemplatePage.getRecords()));
    }



    @GetMapping("/syncTemplate")
    @ApiOperation("同步微信模板消息")
    @SysLog(actionType = "3",value = "同步微信模板消息")
    public ResultObj syncTemplate(){
        ResponseEntity<String> forEntity = restTemplate.getForEntity(wxPushUrl + "/wxTemplate/sync", String.class);
        if(forEntity.getStatusCode().is2xxSuccessful()){
            List<WxTemplate> wxTemplates = JSONArray.parseArray(forEntity.getBody(), WxTemplate.class);
            wxTemplateMapper.truncateTable();
            boolean b = wxTemplateService.insertBatch(wxTemplates);
            if(b){
                return ResultObj.ok("同步成功");
            }
        }
        return ResultObj.error("同步失败");
    }


    @PostMapping("/selectNfWechatUserByTemplateId")
    @ApiOperation("根据当前选择的模板id查询所有微信公众号已认证后的所有用户,并针对已配置过的数据返回已确认")
    @SysLog(actionType = "4",value = "根据当前选择的模板id查询所有微信公众号已认证后的所有用户,并针对已配置过的数据返回已确认")
    public ResultObj<List<NfWechatUser>> selectNfWechatUserByTemplateId(@RequestParam("templateId")String templateId){
       if(StringUtils.isEmpty(templateId)){
           throw new CustomException(ResultCode.PARAM_NULL);
       }
        //返回所有已认证过的用户数据
        List<NfWechatUser> nfWechatUsers = getNfMonitorWechatUserMapper.selectWxUser();

        //根据模板id查询该模板id下配置的用户
        EntityWrapper<WxUserTemplate> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("template_id",templateId);
        List<WxUserTemplate> wxUserTemplates = wxUserTemplateService.selectList(entityWrapper);
        for (NfWechatUser nfWechatUser : nfWechatUsers) {
            for (WxUserTemplate wxUserTemplate : wxUserTemplates) {
                //比较两个openId,如果相等,就将nfWechatUser 设置为选中
                if(nfWechatUser.getOpenId().equals(wxUserTemplate.getOpenId())){
                    nfWechatUser.setIsSelect(1);
                    break;
                }
            }
        }
        return new ResultObj<>(ResultCode.OK,nfWechatUsers);
    }

















    public String getWxPushUrl() {
        return wxPushUrl;
    }

    public void setWxPushUrl(String wxPushUrl) {
        this.wxPushUrl = wxPushUrl;
    }
}