package me.zhengjie.modules.thirdparty.v1;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.base.BaseResponse;
import me.zhengjie.base.QueryPageParams;
import me.zhengjie.modules.dm.messageSendLog.domain.DmMessageSendLog;
import me.zhengjie.modules.dm.messageSendLog.domain.SendMsgParam;
import me.zhengjie.modules.dm.messageSendLog.service.DmMessageSendLogService;
import me.zhengjie.modules.dm.messageSendLog.util.SendMessageUtils;
import me.zhengjie.modules.system.service.dto.DeptQueryNoAuthCriteria;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "消息接口")
@RequestMapping("/api/thirdparty/v1/message")
public class MessageSendLogApiController {

    private final DmMessageSendLogService dmMessageSendLogService;


    @Log("发送IM消息")
    @ApiOperation("发送IM消息")
    @AnonymousPostMapping(value = "/sendChatMessage")
    public BaseResponse<Object> sendChatMessage(@RequestBody QueryPageParams<DmMessageSendLog> params) {
        SecurityUtils.CheckApiAuth(params);
        Boolean isSuccess = SendMessageUtils.sendChatMessage(params.getQuery());
        if(isSuccess){
            dmMessageSendLogService.create(params.getQuery());
        } else {
            return new BaseResponse<>("发送失败",-1,"fail");
        }
        return new BaseResponse<>("发送成功");
    }

    @Log("发送短信")
    @ApiOperation("发送短信")
    @AnonymousPostMapping(value = "/sendMsg")
    public BaseResponse<Object> sendMsg(@RequestBody QueryPageParams<SendMsgParam> params) {
        SecurityUtils.CheckApiAuth(params);
        Boolean isSuccess = SendMessageUtils.sendMsg(params.getQuery());
        if(isSuccess){
            return new BaseResponse<>("发送成功");
        } else {
            return new BaseResponse<>("发送失败");
        }
    }

    @Log("查询消息")
    @ApiOperation("查询消息")
    @AnonymousPostMapping(value = "/findByUsername")
    public BaseResponse<Object> findByUsername(@RequestBody QueryPageParams<String> params) {
        SecurityUtils.CheckApiAuth(params);
        return new BaseResponse<>(dmMessageSendLogService.findByUsername(params.getQuery()));
    }

    @Log("消息已读")
    @ApiOperation("消息已读")
    @AnonymousPostMapping(value = "/messageRead")
    public BaseResponse<Object> messageRead(@RequestBody QueryPageParams<List<String>> params) {
        SecurityUtils.CheckApiAuth(params);
        for (String id : params.getQuery()) {
            dmMessageSendLogService.updateById(id);
        }
        System.out.println();
        return new BaseResponse<>(true);
    }

}
