package com.casic.missiles.controller.workbench;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.workbench.ApprovalMessageRequest;
import com.casic.missiles.dto.workbench.ApprovalMessageUpdateRequest;
import com.casic.missiles.dto.workbench.MessageInfoCountRequest;
import com.casic.missiles.dto.workbench.MessageInfoCountResponse;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.workbench.WorkbenchApprovalMessage;
import com.casic.missiles.service.workbench.IWorkbenchApprovalMessageService;
import com.casic.missiles.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 审批通过通知消息 前端控制器
 * </p>
 *
 * @author wangpeng
 * @since 2023-02-24
 */
@Api(tags = "工作台模块-审批提醒")
@RestController
@RequestMapping("/workbench/approvalMessage")
public class WorkbenchApprovalMessageController extends ExportController {
    @Autowired
    private IWorkbenchApprovalMessageService approvalMessageService;

    /**
     * 审批提醒列表
     * 用户待审批列表、审批结果通知（workbench_approval_message表）
     */
    @ApiOperation("审批提醒列表")
    @PostMapping("/listPage")
    @ResponseBody
    public ReturnDTO<Page<WorkbenchApprovalMessage>> approvalMessageList(@RequestBody @Valid ApprovalMessageRequest approvalMessageRequest) throws Exception {
        List<WorkbenchApprovalMessage> list = approvalMessageService.listPage(approvalMessageRequest);
        Page<WorkbenchApprovalMessage> page = PageFactory.defaultPage();
        Page pages = PageUtil.getPages((int) page.getCurrent(), (int) page.getSize(), list);
        return ReturnUtil.success(super.packForBT(pages));
    }

    /**
     * 已读状态变更
     */
    @ApiOperation("已读状态变更")
    @PostMapping("/updateStatus")
    @ResponseBody
    public ReturnDTO updateStatus(@RequestBody @Valid ApprovalMessageUpdateRequest updateRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return approvalMessageService.updateStatus(updateRequest);
    }

    @ApiOperation("工作台顶部工作提醒和待办信息条数")
    @PostMapping("/count")
    @ResponseBody
    public ReturnDTO<MessageInfoCountResponse> infoCount(@RequestBody @Valid MessageInfoCountRequest request) {
        MessageInfoCountResponse response = approvalMessageService.infoCount(request);
        return ReturnUtil.success(response);
    }
}
