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.core.page.PageInfoBT;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.workbench.RemindMessageRequest;
import com.casic.missiles.dto.workbench.RemindMessageUpdateRequest;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.workbench.WorkbenchRemindMessage;
import com.casic.missiles.service.workbench.IWorkbenchRemindMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 所有业务消息提醒 前端控制器
 * </p>
 *
 * @author wangpeng
 * @since 2023-02-23
 */
@Api(tags = "工作台模块-工作提醒")
@Slf4j
@RestController
@RequestMapping("/workbench/remindMessage")
public class WorkbenchRemindMessageController extends ExportController {

    @Autowired
    private IWorkbenchRemindMessageService remindMessageService;

    /**
     * 工作提醒列表
     * 工作提醒=4种到期提醒（workbench_remind_message表）
     * 设备证书到期提醒、装置到期提醒、人员证书到期提醒、检定超期提醒
     */
    @ApiOperation("工作提醒列表")
    @PostMapping("/listPage")
    @ResponseBody
    public ReturnDTO<PageInfoBT<WorkbenchRemindMessage>> remindMessageList(@RequestBody @Valid RemindMessageRequest remindMessageRequest) throws Exception {
        Page<WorkbenchRemindMessage> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(remindMessageService.listPage(page, remindMessageRequest)));
    }

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

}
