package com.skr.mdm.controller;

import com.skr.mdm.dto.LoginUserDTO;
import com.skr.mdm.handle.DownloadCounterHandler;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;


/**
 * @Author tty
 * @Date 2022/4/14 16:22
 * @Description 下载状态回调 下载量重置
 */

@RestController
@RequestMapping("down/state")
@Slf4j
@Api(tags = "下载管理")
public class DownLoadStateController {

    @Autowired
    private DownloadCounterHandler downloadCounterHandler;

    @PostMapping("reduction")
    @ApiOperation(value = "下载完成回调")
    public ResponseResult reduction(@RequestParam String accessToken, @ApiIgnore Authentication authentication) {
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        if (downloadCounterHandler.decrease(loginUserDTO.getCompanyId(), loginUserDTO.getId(), accessToken)) {
            return ResponseResult.success();
        }
        return ResponseResult.failure();
    }

    @PostMapping("increase")
    @ApiOperation(value = "下载请求接口")
    public ResponseResult increase(@ApiIgnore Authentication authentication) {
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        downloadCounterHandler.initCompanyDownloadCounter(loginUserDTO.getCompanyId());
        return downloadCounterHandler.increment(loginUserDTO.getCompanyId(), loginUserDTO.getId());
    }
}