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

import com.skr.mdm.api.content.service.ContentInformService;
import com.skr.mdm.bean.qo.BaseQueryQO;
import com.skr.mdm.bean.qo.StrategyIssuedQO;
import com.skr.mdm.bean.qo.StrategyUserQO;
import com.skr.mdm.bean.qo.content.ContentInformQO;
import com.skr.mdm.bean.vo.content.ContentInformUserVO;
import com.skr.mdm.bean.vo.content.ContentInformVO;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
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 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("inform")
@Api(tags = "通知管理")
public class ContentInformController {

    @Autowired
    private ContentInformService contentInformService;


    @ApiOperation("添加")
    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('sys:content:inform:add')")
    public ResponseResult addStrategy(@RequestBody @Valid ContentInformQO contentInformQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return contentInformService.addStrategy(contentInformQO, loginAdminDTO);
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @PreAuthorize("hasAnyAuthority('sys:content:inform:update')")
    public ResponseResult updateStrategy(@RequestBody @Valid ContentInformQO contentInformQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return contentInformService.updateStrategy(contentInformQO, loginAdminDTO);
    }

    @ApiOperation("删除")
    @PostMapping("delete")
    @PreAuthorize("hasAnyAuthority('sys:content:inform:delete')")
    public ResponseResult delete(@RequestParam @NotNull(message = "策略ID不能为空") List<Integer> strategyIdList, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return contentInformService.deleteStrategy(strategyIdList, loginAdminDTO);
    }

    @ApiOperation("下发")
    @PostMapping("issued")
    @PreAuthorize("hasAnyAuthority('sys:content:inform:issued')")
    public ResponseResult issued(@RequestBody @Valid StrategyIssuedQO strategyIssuedQO,
                                 @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return contentInformService.issuedStrategy(strategyIssuedQO.getStrategyIdList(), strategyIssuedQO, loginAdminDTO);
    }

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

    @ApiOperation("作用人列表")
    @PostMapping("list/user")
    @PreAuthorize("hasAnyAuthority('sys:content:inform:user')")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = ContentInformUserVO.class)
    })
    public ResponseResult listUser(@RequestBody @Valid StrategyUserQO strategyUserQO) {
        return contentInformService.listStrategyUser(strategyUserQO);
    }

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