package com.skr.mdm.api.content.controller;

import com.skr.mdm.api.content.service.ContentDocumentService;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.bean.qo.BaseQueryQO;
import com.skr.mdm.bean.qo.StrategyUserQO;
import com.skr.mdm.bean.qo.content.DocumentIssuedQO;
import com.skr.mdm.bean.vo.content.ContentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@RequestMapping("content/document")
@Api(tags = "文档管理")
@Slf4j
public class ContentDocumentController {

    @Autowired
    private ContentDocumentService contentDocumentService;

    @PostMapping("upload")
    @ApiOperation("上传")
    @PreAuthorize("hasAnyAuthority('sys:content:document:upload')")
    public ResponseResult uploadAndSaveFile(MultipartFile file,
                                            @RequestParam @NotNull(message = "全局类型不能为空") Integer globalType,
                                            @ApiIgnore Authentication authentication) throws Exception {
        LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
        return contentDocumentService.uploadAndSaveFile(file, globalType, admin);
    }

    @PostMapping("list")
    @ApiOperation("列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ContentVO.class)
    })
    @PreAuthorize("hasAnyAuthority('sys:content:document')")
    public ResponseResult list(@RequestBody @Valid BaseQueryQO baseQueryQO,
                               @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return contentDocumentService.list(baseQueryQO, loginAdminDTO);
    }

    @PostMapping("push")
    @ApiOperation("推送")
    @PreAuthorize("hasAnyAuthority('sys:content:document:push')")
    public ResponseResult issuedDocument(@RequestBody @Valid DocumentIssuedQO documentIssuedQO,
                                         @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return contentDocumentService.issuedDocument(documentIssuedQO, loginAdminDTO);
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    @PreAuthorize("hasAnyAuthority('sys:content:document:delete')")
    public ResponseResult deleteDocument(@RequestParam @NotEmpty(message = "文档ID不能空") List<Integer> strategyIdList,
                                         @ApiIgnore Authentication authentication) throws Exception {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return contentDocumentService.deleteDocument(strategyIdList, loginAdminDTO);
    }

    @PostMapping(value = "user/list")
    @ApiOperation("作用人列表")
    @PreAuthorize("hasAnyAuthority('sys:content:document:user:list')")
    public ResponseResult listDocumentUser(@RequestBody @Valid StrategyUserQO strategyUserQO) {
        return contentDocumentService.listDocumentUser(strategyUserQO);
    }

    @PostMapping(value = "user/delete")
    @ApiOperation("作用人删除")
    @PreAuthorize("hasAnyAuthority('sys:content:document:user:delete')")
    public ResponseResult deleteDocumentUser(@RequestParam @NotNull(message = "ID不能为空") Integer strategyId,
                                             @RequestParam @NotEmpty(message = "设备ID不能为空") List<Integer> equipmentIdList,
                                             @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return contentDocumentService.deleteDocumentUser(strategyId, equipmentIdList, loginAdminDTO);
    }

}
