package com.izouma.jmrh.web;

import com.izouma.jmrh.domain.Conversation;
import com.izouma.jmrh.domain.ResourceSupplyAndDemand;
import com.izouma.jmrh.dto.ConversationDTO;
import com.izouma.jmrh.dto.PageQuery;
import com.izouma.jmrh.enums.Exist;
import com.izouma.jmrh.exception.BusinessException;
import com.izouma.jmrh.repo.ConversationRepo;
import com.izouma.jmrh.service.ConversationService;
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.web.bind.annotation.*;

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

@RestController
@RequestMapping("/conversation")
@AllArgsConstructor
public class ConversationController extends BaseController {
    private ConversationService conversationService;
    private ConversationRepo    conversationRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public Conversation save(@RequestBody Conversation record) {
        if (record.getId() == null) throw new BusinessException("id不能为空");
        Conversation orig = conversationRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
        ObjUtils.merge(orig, record);
        return conversationRepo.save(orig);
    }

    @PostMapping("/create")
    public void create(@RequestParam Long sndId, @RequestParam String orgName,
                       @RequestParam String contactName, @RequestParam String contactPhone,
                       @RequestParam(required = false) String email, @RequestParam String content) {
        conversationService.create(SecurityUtils.getAuthenticatedUser().getId(),
                sndId, orgName, contactName, contactPhone, email, content);
    }

    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<Conversation> all(PageQuery pageQuery) {
        //过滤已经终止并删除的对接
        ArrayList<Exist> exists = new ArrayList<>();
        exists.add(Exist.PUBLISHERS_DELETED);
        exists.add(Exist.ON_DELETED);
        pageQuery.getQuery().put("exist", exists);
        return conversationRepo.findAll(toSpecification(pageQuery, Conversation.class), toPageRequest(pageQuery));
    }

    @GetMapping("/get/{id}")
    public ConversationDTO get(@PathVariable Long id) {
        return conversationService.get(id);
    }

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

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

    @GetMapping("/my")
    @ApiOperation("我的对话")
    public List<ConversationDTO> my() {
        return conversationService.my(SecurityUtils.getAuthenticatedUser().getId());
    }
}

