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

import com.skr.mdm.api.app.service.MdmAppPushService;
import com.skr.mdm.bean.qo.StrategyUserQO;
import com.skr.mdm.bean.qo.app.*;
import com.skr.mdm.bean.vo.app.AppPushStatisticsVO;
import com.skr.mdm.bean.vo.app.AppPushUserVO;
import com.skr.mdm.bean.vo.app.AppPushVO;
import com.skr.mdm.bean.vo.app.AppStoreDeptVO;
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 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-12-20 16:04
 */
@Validated
@RestController
@RequestMapping("app/push")
@Api(tags = "应用推送")
public class MdmAppPushController {

    @Autowired
    private MdmAppPushService appPushService;

    @ApiOperation(value = "列表", response = AppPushVO.class)
    @PostMapping(value = "list")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult listApp(@RequestBody @Valid AppPushListQO appPushListQO,
                                  @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appPushService.listAppPush(appPushListQO, loginAdminDTO);
    }

    @ApiOperation(value = "作用人列表", response = AppPushUserVO.class)
    @PostMapping(value = "user/list")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult listAppPushUser(@RequestBody @Valid StrategyUserQO strategyUserQO) {
        return appPushService.listAppPushUser(strategyUserQO);
    }

    @ApiOperation(value = "指定部门列表", response = AppStoreDeptVO.class)
    @PostMapping(value = "dept/list")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult listAppDept(@RequestBody @Valid StrategyUserQO strategyUserQO) {
        return appPushService.listAppDept(strategyUserQO);
    }

    @ApiOperation(value = "应用推送统计", response = AppPushStatisticsVO.class)
    @PostMapping(value = "statistics")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult statisticsAppPush(@RequestParam @NotNull(message = "应用推送ID不能为空") Integer appPushId) {
        return appPushService.statisticsAppPush(appPushId);
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "add")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult addAppPush(@RequestBody @Valid MdmAppPushAddQO appPushAddQO,
                                     @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appPushService.addAppPush(appPushAddQO, loginAdminDTO);
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "delete")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult deleteAppPush(@RequestParam @NotEmpty(message = "应用推送ID不能为空") List<Integer> appPushIdList,
                                        @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appPushService.deleteAppPush(appPushIdList, loginAdminDTO);
    }

    @ApiOperation(value = "暂停/恢复")
    @PostMapping(value = "pause")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult pauseAppPush(@RequestParam @NotNull(message = "应用推送ID不能为空") Integer appPushId,
                                       @RequestParam @NotNull(message = "暂停状态不能为空") Integer isPause,
                                       @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appPushService.pauseAppPush(appPushId, isPause, loginAdminDTO);
    }

    @ApiOperation(value = "推送")
    @PostMapping(value = "push")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult pushApp(@RequestBody @Valid MdmAppPushQO mdmAppPushQO,
                                  @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appPushService.pushApp(mdmAppPushQO, loginAdminDTO);
    }

    @ApiOperation(value = "重新推送")
    @PostMapping(value = "repush")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult repushApp(@RequestBody @Valid MdmAppRePushQO appRePushQO,
                                    @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appPushService.repushApp(appRePushQO, loginAdminDTO);
    }

    @ApiOperation(value = "强制推送")
    @PostMapping(value = "force")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult forcePushApp(@RequestBody @Valid MdmAppRePushQO appRePushQO,
                                       @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appPushService.forcePushApp(appRePushQO, loginAdminDTO);
    }

    @ApiOperation(value = "删除作用人")
    @PostMapping(value = "delete/user")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult deleteAppUserUser(@RequestParam @NotNull(message = "应用推送ID不能为空") Integer appPushId,
                                            @RequestParam @NotEmpty(message = "设备ID不能为空") List<Integer> equipmentIdList,
                                            @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appPushService.deleteAppUserUser(appPushId, equipmentIdList, loginAdminDTO);
    }

    @ApiOperation(value = "全局配置")
    @PostMapping(value = "global/config")
    @PreAuthorize("hasAnyAuthority('sys:app:push')")
    public ResponseResult configGlobal(@RequestBody @Valid MdmAppPushGlobalQO appPushGlobalQO,
                                       @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return appPushService.configGlobal(appPushGlobalQO, loginAdminDTO);
    }
}
