package com.xindazhou.business.controller.admin.message;

import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.business.controller.admin.message.vo.ServiceMessagePageReqVO;
import com.xindazhou.business.controller.admin.message.vo.ServiceMessageRespVO;
import com.xindazhou.business.controller.admin.message.vo.ServiceMessageSaveReqVO;
import com.xindazhou.business.convert.message.ServiceMessageConvert;
import com.xindazhou.business.dal.dataobject.message.ServiceMessageDO;
import com.xindazhou.business.service.message.ServiceMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 服务消息")
@RestController
@RequestMapping("/admin/service")
@Validated
public class ServiceMessageController {

    @Resource
    private ServiceMessageService serviceMessageService;

    @PostMapping("/message")
    @Operation(summary = "创建服务消息")
    @PreAuthorize("@ss.hasPermission('service:message:create')")
    public CommonResult<Long> createMessage(@Valid @RequestBody ServiceMessageSaveReqVO createReqVO) {
        return success(serviceMessageService.createMessage(createReqVO));
    }

    @PutMapping("/message/{id}")
    @Operation(summary = "修改服务消息")
    @PreAuthorize("@ss.hasPermission('service:message:update')")
    public CommonResult<Boolean> updateMessage(@PathVariable("id") Long id,
                                                @Valid @RequestBody ServiceMessageSaveReqVO updateReqVO) {
        updateReqVO.setId(id);
        serviceMessageService.updateMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/message/{id}")
    @Operation(summary = "删除服务消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('service:message:delete')")
    public CommonResult<Boolean> deleteMessage(@PathVariable("id") Long id) {
        serviceMessageService.deleteMessage(id);
        return success(true);
    }

    @GetMapping("/message/{id}")
    @Operation(summary = "获得服务消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('service:message:query')")
    public CommonResult<ServiceMessageRespVO> getMessage(@PathVariable("id") Long id) {
        ServiceMessageDO message = serviceMessageService.getMessage(id);
        return success(ServiceMessageConvert.INSTANCE.convert(message));
    }

    // 为了兼容前端调用的 /service/messages 路径
    @GetMapping("/messages")
    @Operation(summary = "获得服务消息分页")
    @PreAuthorize("@ss.hasPermission('service:message:query')")
    public CommonResult<PageResult<ServiceMessageRespVO>> getMessages(@Valid ServiceMessagePageReqVO pageReqVO) {
        PageResult<ServiceMessageDO> page = serviceMessageService.getMessagePage(pageReqVO);
        return success(ServiceMessageConvert.INSTANCE.convertPage(page));
    }

}

