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

import com.skr.mdm.api.system.service.ClientUpgradeService;
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.system.ClientUpgradeAddUpdateQO;
import com.skr.mdm.bean.qo.system.ClientUpgradeRepushQO;
import com.skr.mdm.bean.vo.app.AppPushStatisticsVO;
import com.skr.mdm.bean.vo.app.MdmApkInfoVO;
import com.skr.mdm.bean.vo.system.ClientUpgradeUserVO;
import com.skr.mdm.bean.vo.system.ClientUpgradeVO;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;

/**
 * @author nfq
 * @date 2023-02-15 15:30
 */
@Validated
@RestController
@RequestMapping("client/upgrade")
@Api(tags = "客户端升级管理")
public class ClientUpgradeController {

    @Autowired
    private ClientUpgradeService clientUpgradeService;

    @ApiOperation(value = "列表", response = ClientUpgradeVO.class)
    @PostMapping("list")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:list')")
    public ResponseResult listClientUpgrade(@RequestBody @Valid BaseQueryQO baseQueryQO,
                                            @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return clientUpgradeService.listClientUpgrade(baseQueryQO, loginAdminDTO);
    }

    @ApiOperation(value = "作用人列表", response = ClientUpgradeUserVO.class)
    @PostMapping("user/list")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:user:list')")
    public ResponseResult listClientUpgradeUser(@RequestBody @Valid StrategyUserQO strategyUserQO) {
        return clientUpgradeService.listClientUpgradeUser(strategyUserQO);
    }

    @ApiOperation(value = "客户端升级统计", response = AppPushStatisticsVO.class)
    @PostMapping(value = "statistics")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:user:list')")
    public ResponseResult statisticsAppPush(@RequestParam @NotNull(message = "客户端升级ID不能为空") Integer clientUpgradeId) {
        return clientUpgradeService.statisticsClientUpgrade(clientUpgradeId);
    }

    @ApiOperation(value = "解析apk", response = MdmApkInfoVO.class)
    @PostMapping("analysis/apk")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:add')")
    public ResponseResult analyticApp(@RequestParam("file") MultipartFile file,
                                      @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return clientUpgradeService.analyticApp(file, loginAdminDTO);
    }

    @ApiOperation(value = "新增")
    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:add')")
    public ResponseResult addUpgradeApp(@RequestBody @Valid ClientUpgradeAddUpdateQO clientUpgradeAddUpdateQO,
                                        @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return clientUpgradeService.addUpgradeApp(clientUpgradeAddUpdateQO, loginAdminDTO);
    }

    @ApiOperation(value = "编辑")
    @PostMapping("update")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:update')")
    public ResponseResult updateUpgradeApp(@RequestBody @Valid ClientUpgradeAddUpdateQO clientUpgradeAddUpdateQO,
                                           @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return clientUpgradeService.updateUpgradeApp(clientUpgradeAddUpdateQO, loginAdminDTO);
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:delete')")
    public ResponseResult deleteUpgradeApp(@NotEmpty(message = "客户端升级ID不能为空") @RequestParam List<Integer> clientUpgradeIdList,
                                           @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return clientUpgradeService.deleteUpgradeApp(clientUpgradeIdList, loginAdminDTO);
    }

    @ApiOperation(value = "作用人删除")
    @PostMapping("user/delete")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:user:delete')")
    public ResponseResult deleteUpgradeAppUser(@NotNull(message = "客户端升级ID不能为空") @RequestParam Integer clientUpgradeId,
                                               @NotEmpty(message = "设备ID不能为空") @RequestParam List<Integer> equipmentIdList,
                                               @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return clientUpgradeService.deleteUpgradeAppUser(clientUpgradeId, equipmentIdList, loginAdminDTO);
    }

    @ApiOperation(value = "推送")
    @PostMapping("push")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:push')")
    public ResponseResult pushUpgradeApp(@RequestBody @Valid StrategyIssuedQO strategyIssuedQO,
                                         @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return clientUpgradeService.pushUpgradeApp(strategyIssuedQO, loginAdminDTO);
    }

    @ApiOperation(value = "作用人重新推送")
    @PostMapping("repush")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:repush')")
    public ResponseResult repushUpgradeApp(@RequestBody @Valid ClientUpgradeRepushQO clientUpgradeRepushQO,
                                           @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return clientUpgradeService.repushUpgradeApp(clientUpgradeRepushQO, loginAdminDTO);
    }

    @ApiOperation(value = "暂停/恢复")
    @PostMapping("pause")
    @PreAuthorize("hasAnyAuthority('sys:config:upgrade:pause')")
    public ResponseResult pauseUpgrade(@NotNull(message = "客户端升级ID不能为空") @RequestParam Integer clientUpgradeId,
                                       @NotNull(message = "暂停状态不能为空") @RequestParam Integer isPause,
                                       @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return clientUpgradeService.pauseUpgrade(clientUpgradeId, isPause, loginAdminDTO);
    }
}
