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

import com.skr.mdm.api.app.service.AppStrategyService;
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.app.AppStrategyQO;
import com.skr.mdm.bean.vo.app.AppStrategyInfoVO;
import com.skr.mdm.bean.vo.app.AppStrategyVO;
import com.skr.mdm.bean.vo.strategy.StrategyUserVO;
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;

/**
 * @author nfq
 * @date 2022-10-27 10:40
 */
@Validated
@RestController
@RequestMapping("strategy/app")
@Api(tags = "应用策略")
public class AppStrategyController {

    @Autowired
    private AppStrategyService appStrategyService;

    @PostMapping("list")
    @ApiOperation(value = "列表")
    @PreAuthorize("hasAnyAuthority('sys:app:strategy:list')")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = AppStrategyVO.class)
    })
    public ResponseResult strategyInfo(@RequestBody @Valid BaseQueryQO baseQueryQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStrategyService.listStrategy(baseQueryQO, loginAdminDTO);
    }

    @PostMapping("add")
    @ApiOperation(value = "创建")
    @PreAuthorize("hasAnyAuthority('sys:app:strategy:add')")
    public ResponseResult addStrategy(@RequestBody @Valid AppStrategyQO appStrategyQO,
                                      @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStrategyService.addStrategy(appStrategyQO, loginAdminDTO);
    }

    @PostMapping("update")
    @ApiOperation(value = "编辑")
    @PreAuthorize("hasAnyAuthority('sys:app:strategy:update')")
    public ResponseResult updateStrategy(@RequestBody @Valid AppStrategyQO appStrategyQO,
                                         @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appStrategyService.updateStrategy(appStrategyQO, loginAdminDTO);
    }

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

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

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

    @PostMapping("user/delete")
    @ApiOperation(value = "作用人删除")
    @PreAuthorize("hasAnyAuthority('sys:app:strategy: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 appStrategyService.deleteStrategyUser(strategyId, equipmentIdList, loginAdminDTO);
    }

    @PostMapping("info")
    @ApiOperation(value = "详细信息")
    @PreAuthorize("hasAnyAuthority('sys:app:strategy:list')")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = AppStrategyInfoVO.class)
    })
    public ResponseResult strategyInfo(@RequestParam @NotNull(message = "策略ID不能为空") Integer strategyId) {
        return appStrategyService.strategyInfo(strategyId);
    }
}
