package com.crunii.micro.service.sysmgr.controller;

import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.dto.Result;
import com.crunii.micro.common.excel.excel.ExcelExportUtil;
import com.crunii.micro.domain.dto.Worker;
import com.crunii.micro.service.sysmgr.api.domain.dto.SendMessageTaskQueryDTO;
import com.crunii.micro.service.sysmgr.api.domain.dto.YcqBatchReadMessageDTO;
import com.crunii.micro.service.sysmgr.api.domain.dto.YcqMessageQueryKeywordDTO;
import com.crunii.micro.service.sysmgr.api.domain.dto.YcqPageUserMessageRecordDTO;
import com.crunii.micro.service.sysmgr.api.domain.vo.SendMessageTaskPageVO;
import com.crunii.micro.service.sysmgr.api.domain.vo.YcqExportMessageRecordVO;
import com.crunii.micro.service.sysmgr.api.domain.vo.YcqPageMessageRecordVO;
import com.crunii.micro.service.sysmgr.api.domain.vo.YcqPageUserMessageRecordVO;
import com.crunii.micro.service.sysmgr.service.YcqMessageRecordService;
import com.crunii.micro.web.mvc.CurrWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 * 消息记录
 * @author 15294
 */
@Api(value = "消息记录")
@Slf4j
@RestController
@RequestMapping("/auth/YcqMessageRecordController")
public class YcqMessageRecordController {

    @Resource
    private YcqMessageRecordService service;

    /**
     * 消息管理详情-根据条件分页查询消息记录(不分权限)
     * @param req
     * @return
     */
    @PostMapping("/pageByCondition")
    public Result<PageRsp<YcqPageMessageRecordVO>> pageByCondition(@RequestBody PageReq<YcqMessageQueryKeywordDTO> req){
        return Result.ofSuccess(service.pageByCondition(req));
    }

    /**
     * 消息管理详情-消息记录导出
     * @param request
     * @param response
     * @throws IOException
     */
    @PostMapping("/export")
    public void export(@RequestBody YcqMessageQueryKeywordDTO request, HttpServletResponse response) throws IOException {
        List<YcqExportMessageRecordVO> list = service.list(request);
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        ExcelExportUtil.exportToOutputStream(response,"message_record", list);
    }

    /**
     * 消息管理-分页条件查询消息任务数据
     * @param req
     * @return
     */
    @ApiOperation("消息管理-分页条件查询消息任务数据")
    @PostMapping("/pageTaskByCondition")
    public Result<PageRsp<SendMessageTaskPageVO>> pageTaskByCondition(@RequestBody @ApiParam PageReq<SendMessageTaskQueryDTO> req) {
        return Result.ofSuccess(service.pageTaskByCondition(req));
    }

    /**
     * 消息二级页面-分页条件用户消息数据
     * @param req
     * @return
     */
    @ApiOperation("消息二级页面-分页条件用户消息数据")
    @PostMapping("/pageUserMessage")
    public Result<PageRsp<YcqPageUserMessageRecordVO>> pageUserMessage(@RequestBody PageReq<YcqPageUserMessageRecordDTO> req, @CurrWorker @ApiIgnore Worker worker){
        return Result.ofSuccess(service.pageUserMessage(req, worker.getLoginId()));
    }

    /**
     * 消息二级页面-批量已读
     * @param req
     * @return
     */
    @ApiOperation("消息二级页面-批量已读")
    @PostMapping("/batchRead")
    public Result<Boolean> batchRead(@RequestBody List<Long> ids, @CurrWorker @ApiIgnore Worker worker){
        return Result.ofSuccess(service.batchRead(ids, worker.getLoginId()));
    }
}
