package com.izouma.nineth.web;

import com.izouma.nineth.domain.Message;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.dto.MessageDTO;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.MessageRepo;
import com.izouma.nineth.service.MessageService;
import com.izouma.nineth.utils.ObjUtils;
import com.izouma.nineth.utils.SecurityUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

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

    @PreAuthorize("hasAnyRole('ADMIN','MESSAGE')")
    @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);
            orig.setReply(true);
            orig.setStatus("REPLIED");
            orig.setRepliedAt(LocalDateTime.now());
            return messageRepo.save(orig);
        }
        return messageRepo.save(record);
    }

    @ApiOperation("管理员用")
    @PreAuthorize("hasAnyRole('ADMIN','MESSAGE')")
    @PostMapping("/all")
    public Page<Message> all(@RequestBody PageQuery pageQuery) {
        return messageService.all(pageQuery);
    }

    @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.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<Message> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @ApiOperation("用户用")
    @PostMapping("/my")
    public Page<Message> my(@RequestBody PageQuery pageQuery) {
        pageQuery.getQuery().put("userId", SecurityUtils.getAuthenticatedUser().getId());
        return messageService.all(pageQuery);
    }

    @ApiOperation("用户提交留言")
    @PostMapping("/create")
    public Message create(@RequestBody MessageDTO record) {
        Message message = new Message(record);

        User user = SecurityUtils.getAuthenticatedUser();
        message.setUserId(user.getId());
        message.setNickname(user.getNickname());
        //未回复
        message.setStatus("NO_REPLY");
//        message.setPhone(user.getPhone());
        return messageRepo.save(message);
    }

    @PreAuthorize("hasAnyRole('ADMIN','MESSAGE')")
    @PostMapping("/changeStatus")
    public void changeStatus(@RequestParam Long id, @RequestParam String status) {
        Message orig = messageRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        orig.setStatus(status);
        messageRepo.save(orig);
    }
}

