FeiShuEventController.java 4.88 KB
package com.pipihelper.project.feishu.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pipihelper.project.feishu.dto.FeiShuConfig;
import com.pipihelper.project.feishu.dto.FeiShuEventDTO;
import com.pipihelper.project.feishu.dto.employee.FeiShuEmployeeDTO;
import com.pipihelper.project.feishu.dto.employee.SystemFieldsDTO;
import com.pipihelper.project.feishu.entity.Employee;
import com.pipihelper.project.feishu.service.EmployeeService;
import com.pipihelper.project.feishu.service.FeiShuApiService;
import com.pipihelper.project.feishu.service.FeiShuEventService;
import com.pipihelper.project.feishu.utils.FeiShuEventDataDecrypter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @author: charles
 * @date: 2022年03月30日 17:39
 */
@RestController
@RequestMapping("/pipitest/feishu")
public class FeiShuEventController {

    @Resource
    private FeiShuConfig feiShuConfig;

    @Resource
    private FeiShuEventService feiShuEventService;

    @Autowired
    private FeiShuApiService feiShuApiService;


    @Autowired
    private EmployeeService employeeService;


    @PostMapping("/event")
    public JSONObject event(@RequestBody String s) throws Exception {
        JSONObject reqJsonObject = JSON.parseObject(s);
        System.out.println(s);
        FeiShuEventDataDecrypter d = new FeiShuEventDataDecrypter(feiShuConfig.getEncryptKey());
        JSONObject encryptJsonObject = JSON.parseObject(d.decrypt(reqJsonObject.getString("encrypt")));
        System.out.println(encryptJsonObject);
        if (encryptJsonObject.containsKey("challenge")) {
            return encryptJsonObject;
        }
        FeiShuEventDTO feiShuEventDTO = encryptJsonObject.toJavaObject(FeiShuEventDTO.class);
        if (!feiShuEventDTO.getHeader().getToken().equalsIgnoreCase(feiShuConfig.getVerificationToken())) {
            return null;
        }
        if ("im.message.receive_v1".equalsIgnoreCase(feiShuEventDTO.getHeader().getEvent_type())) {
//            feiShuEventService.imMessageReceiveV1(feiShuEventDTO);
        } else if ("im.message.message_read_v1".equalsIgnoreCase(feiShuEventDTO.getHeader().getEvent_type())) {
//            feiShuEventService.imMessageMessageReadV1(feiShuEventDTO);
        }
        return null;
    }

//    @PostMapping("msg_card")
//    public JSONObject msgCardEvent(@RequestBody String s) throws Exception {
//        JSONObject reqJsonObject = JSON.parseObject(s);
//        System.out.println(s);
//        if (reqJsonObject.containsKey("challenge")) {
//            return reqJsonObject;
//        }
//        FeiShuMsgCardEventDTO feiShuMsgCardEventDTO = reqJsonObject.toJavaObject(FeiShuMsgCardEventDTO.class);
//        String actionType = feiShuMsgCardEventDTO.getAction().getValue().getKey().split("\\.")[0];
//        switch (actionType) {
//            case "TEST_DATA":
//
////            default:
////        }
//        }
//
//    }

    @PostMapping("/employee-list")
    public Object event() {
        List<FeiShuEmployeeDTO> dtos = feiShuApiService.getEmployeeList();

        List<Employee> employeeList = employeeService.findAll();
        Map<String, Employee> employeeMap = employeeList.stream().collect(Collectors.toMap(Employee::getOpenId, Function.identity()));

        List<String> openIdList = dtos.stream().map(FeiShuEmployeeDTO::getUserId).collect(Collectors.toList());
        employeeList.forEach(it -> {
            if (!openIdList.contains(it.getOpenId())) {
                employeeService.deleteById(it.getId());
            }
        });

        dtos.forEach(it -> {
            Employee currentEmployee = employeeMap.get(it.getUserId());
            if (currentEmployee != null) {
                return;
            }
            Employee employee = new Employee();
            SystemFieldsDTO systemFieldsDTO = it.getSystemFields();
            employee.setOpenId(it.getUserId());
            if (systemFieldsDTO != null) {
                employee.setGender(systemFieldsDTO.getGender());
                employee.setStatus(systemFieldsDTO.getStatus());
                employee.setDepartmentId(systemFieldsDTO.getDepartmentId());
                employee.setName(systemFieldsDTO.getName());
                employee.setMobile(systemFieldsDTO.getMobile());
            }
            employeeService.create(employee);
        });
        return dtos;
    }

    private static class A {
        Integer index;
        String openId;
        String name;
        String departMentName;
        String timeRange;
    }
}