package com.galaxis.manatee.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.galaxis.manatee.dao.DingTalkRobotDao;
import com.galaxis.manatee.entity.ding.DingTalkRobot;
import com.galaxis.manatee.entity.ding.DingTalkRobotRequest;
import com.galaxis.manatee.entity.ding.DingTalkRobotResponse;
import com.galaxis.manatee.manager.DingTalkRobotWebHook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zcj
 * @version 0.1
 * @date 2020/1/9 1:10 下午
 */
@Slf4j
@RequestMapping("/dingTalkTest")
@RestController
public class DingTalkTestController {


    private ObjectMapper objectMapper=new ObjectMapper();
    private DingTalkRobotDao dingTalkRobotDao;
    private DingTalkRobotWebHook dingTalkRobotWebHook;

    public DingTalkTestController(ObjectMapper objectMapper, DingTalkRobotDao dingTalkRobotDao, DingTalkRobotWebHook dingTalkRobotWebHook) {
        this.objectMapper = objectMapper;
        this.dingTalkRobotDao = dingTalkRobotDao;
        this.dingTalkRobotWebHook = dingTalkRobotWebHook;
    }

    /**
     * 测试中兴机器人
     * @throws Exception 机器人接口调用异常
     */
    @GetMapping("/dingTalkRobotWebHookTest")
    public void dingTalkRobotWebHookTest() throws Exception {
        Map<String,String> params=new HashMap<>(1);
        params.put("content","考勤报告,这是测试");
        DingTalkRobotRequest.At at=DingTalkRobotRequest.At.instance(new ArrayList<>(),false);
        DingTalkRobot dingTalkRobot=dingTalkRobotDao.findByName("中兴关键词机器人");
        DingTalkRobotResponse dingTalkRobotResponse=dingTalkRobotWebHook.invoke(DingTalkRobotRequest.instanceOf(params, DingTalkRobotRequest.MessageType.TEXT,at),dingTalkRobot);
        log.info(objectMapper.writeValueAsString(dingTalkRobotResponse));
    }

    /**
     * 测试保存
     */
    @GetMapping("/saveDingTalkRobot")
    public void saveDingTalkRobot(){
        DingTalkRobot dingTalkRobot=new DingTalkRobot();
        dingTalkRobot.setName("测试创建");
        dingTalkRobotDao.save(dingTalkRobot);
    }

}