package com.izouma.jmrh.web;

import com.izouma.jmrh.domain.Message;
import com.izouma.jmrh.dto.MessageDTO;
import com.izouma.jmrh.dto.PageQuery;
import com.izouma.jmrh.enums.MessageType;
import com.izouma.jmrh.exception.BusinessException;
import com.izouma.jmrh.repo.MessageRepo;
import com.izouma.jmrh.service.MessageService;
import com.izouma.jmrh.utils.ObjUtils;
import com.izouma.jmrh.utils.SecurityUtils;
import com.izouma.jmrh.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("/message")
@RestController
@AllArgsConstructor
@ApiOperation("联系对接")
public class MessageController extends BaseController {
    private MessageRepo    messageRepo;
    private MessageService messageService;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public Message save(@RequestBody Message record) {
        if (record.getId() != null) {
            Message orig = messageRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return messageRepo.save(orig);
        }
        return messageRepo.save(record);
    }

    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<MessageDTO> all(@RequestParam String sessionId, @RequestParam(required = false) MessageType type,
                                Pageable pageable) {
        return messageService.all(sessionId, type, pageable);
    }

    @GetMapping("/get/{id}")
    public Message get(@PathVariable Long id) {
        return messageRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        messageRepo.deleteById(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestParam String sessionId, @RequestParam(required = false) MessageType type,
                      Pageable pageable) throws IOException {
        List<MessageDTO> data = all(sessionId, type, pageable).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/unreadNum")
    public long unreadNum() {
        return messageRepo.countByToUserIdAndReadFalse(SecurityUtils.getAuthenticatedUser().getId());
    }

    @GetMapping("/readAll")
    public void realAll(@RequestParam(required = false) String sessionId) {
        messageService.readAll(SecurityUtils.getAuthenticatedUser().getId(), sessionId);
    }

    @PostMapping("/send")
    public void send(@RequestParam String sessionId, @RequestParam Long toUserId, @RequestParam MessageType type, @RequestParam String content) {
        messageService.send(SecurityUtils.getAuthenticatedUser().getId(), toUserId, sessionId, type, content);
    }
}
