package com.chuanxia.mcp.controller;

/**
 * @author liyuntian
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuanxia.mcp.aop.Login;
import com.chuanxia.mcp.bean.dto.DomainQueryDto;
import com.chuanxia.mcp.bean.model.CdnDomain;
import com.chuanxia.mcp.bean.vo.Result;
import com.chuanxia.mcp.service.CdnDoaminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;


/**
 * @author liyuntian
 */
@RestController
@Api("cdn域名管理")
@RequestMapping("cdnDomain")
@RequiredArgsConstructor
public class CdnDomainController {
    private final CdnDoaminService doaminService;

    @PostMapping("listPage")
    @ApiOperation("cdn域名列表(分页)")
    @Login
    public Result<IPage<CdnDomain>> listPage(@RequestBody DomainQueryDto queryDto) {
        return doaminService.listPage(queryDto);
    }

    @GetMapping("getList")
    @ApiOperation("cdn域名列表(不分页,用于区域绑定)")
    @Login
    public Result<List<CdnDomain>> getList() {
        return Result.success(doaminService.list());
    }

    @PostMapping("saveDomain")
    @ApiOperation("新增域名")
    @Login
    public Result<String> saveDomain(@RequestBody CdnDomain domain) {
        return doaminService.saveDomain(domain);
    }

    @PostMapping("updateDomain")
    @ApiOperation("修改域名")
    @Login
    public Result updateDomain(@RequestBody CdnDomain domain) {
        return doaminService.updateDomain(domain);
    }

    @GetMapping("getCdnByVideoId")
    @ApiOperation("根据VideoId,网速,运营商,获取cdn")
    public Result getCdnByVideoId(@RequestParam("videoId") String videoId,
                                  @RequestParam(value = "internetSpeed", required = false, defaultValue = "120") BigDecimal internetSpeed,
                                  @RequestParam(value = "operatorName", defaultValue = "", required = false) String operatorName,
                                  @RequestParam(value = "type", required = false, defaultValue = "") String type,
                                  HttpServletRequest request) {
        return doaminService.getCdnByVideoId(videoId, internetSpeed, operatorName, type, request);
    }

    @GetMapping("downLoadM3u8File")
    @ApiOperation("根据m3u8id,下载对应播放m3u8地址")
    public Result downLoadM3u8File(@RequestParam("cdn") String cdn,
                                   @RequestParam("id") Integer id,
                                   HttpServletResponse response) {
        return doaminService.downLoadM3u8File(cdn,id,response);
    }
}
