package com.ruixuan.live.controller;

import com.ruixuan.common.annotation.Log;
import com.ruixuan.common.core.controller.BaseController;
import com.ruixuan.common.core.domain.AjaxResult;
import com.ruixuan.common.core.domain.ResultResponse;
import com.ruixuan.common.core.page.TableDataInfo;
import com.ruixuan.common.enums.BusinessType;
import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.poi.ExcelUtil;
import com.ruixuan.live.entity.KuaishouLiveBroadcast;
import com.ruixuan.live.service.IKuaishouLiveBroadcastService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 快手—直播Controller
 *
 * @author ruoyi
 * @date 2022-05-31
 */
@Api(tags = "操盘手——直播业务")
@RestController
@RequestMapping("/live/broadcast")
public class KuaishouLiveBroadcastController extends BaseController {
    @Autowired
    private IKuaishouLiveBroadcastService kuaishouLiveBroadcastService;

    /**
     * 查询快手—直播列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询快手直播账户列表")
    public TableDataInfo list(KuaishouLiveBroadcast kuaishouLiveBroadcast) {
        startPage();
        List<KuaishouLiveBroadcast> list = kuaishouLiveBroadcastService.selectKuaishouLiveBroadcastList(kuaishouLiveBroadcast);
        return getDataTable(list);
    }

    /**
     * 数据回显编辑
     */
    @GetMapping(value = "getForEdit")
    @ApiOperation(value = "快手—直播数据回显编辑")
    public ResultResponse getForEdit(@ApiParam("直播id") @RequestParam(value = "id", required = true) String id) {
        try {
            return kuaishouLiveBroadcastService.getForEdit(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error("查询异常");
        }
    }

    /**
     * 获取快手—直播详细信息
     */
    @GetMapping(value = "getById")
    @ApiOperation(value = "获取快手—直播详细信息")
    public ResultResponse getInfo(@ApiParam("直播id") @RequestParam(value = "id", required = true) String id) {
        return kuaishouLiveBroadcastService.selectKuaishouLiveBroadcastById(id);
    }

    /**
     * 新增快手—直播
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增直播")
    public ResultResponse add(@RequestBody KuaishouLiveBroadcast kuaishouLiveBroadcast) {
        return kuaishouLiveBroadcastService.insertKuaishouLiveBroadcast(kuaishouLiveBroadcast);
    }

    /**
     * 修改快手—直播
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改直播")
    public ResultResponse edit(@RequestBody KuaishouLiveBroadcast kuaishouLiveBroadcast) {
        return kuaishouLiveBroadcastService.updateKuaishouLiveBroadcast(kuaishouLiveBroadcast);
    }


    /**
     * 数据上传
     */
    @PostMapping(value = "/dataUpload")
    @ApiOperation(value = "数据上传")
    public ResultResponse dataUpload(
            @ApiParam("直播id") @RequestParam(value = "id", required = true) String id,
            @ApiParam("实际GMV") @RequestParam(value = "actualGmv", required = true) String actualGmv,
            @ApiParam("文件集") @RequestParam(value = "files", required = false) MultipartFile[] files,
            @ApiParam("数据大盘图片链接") @RequestParam(value = "dapanUrls", required = false) String dapanUrls,
            @ApiParam("画像分析图片链接") @RequestParam(value = "portraitUrls", required = false) String portraitUrls) {

        try {
            if ((Check.isNull(files) || "".equals(files[0].getOriginalFilename())) && Check.isNull(dapanUrls) && Check.isNull(portraitUrls)) {
                return ResultResponse.error("文件和图片不能同时为空，请上传后操作！");
            }
            return kuaishouLiveBroadcastService.dataUpload(id, actualGmv, dapanUrls, portraitUrls, files);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error("上传异常");
        }
    }


    /**
     * 数据上传
     */
    @PostMapping(value = "/replay")
    @ApiOperation(value = "复盘")
    public ResultResponse replay(
            @ApiParam("直播id") @RequestParam(value = "id", required = true) String id,
            @ApiParam("图片") @RequestParam(value = "replayPictureUrls", required = false) String replayPictureUrls,
            @ApiParam("复盘数据") @RequestParam(value = "replayFile", required = false) String replayFile) {

        try {
            if ( Check.isNull(replayPictureUrls) && Check.isNull(replayFile)) {
                return ResultResponse.error("文件和图片不能同时为空，请上传后操作！");
            }
            return kuaishouLiveBroadcastService.replay(id, replayPictureUrls, replayFile);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error("上传异常");
        }
    }

    /**
     * 删除快手—直播
     */
    @Log(title = "快手—直播", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(kuaishouLiveBroadcastService.deleteKuaishouLiveBroadcastByIds(ids));
    }


    /**
     * 导出快手—直播列表
     */
    @Log(title = "快手—直播", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KuaishouLiveBroadcast kuaishouLiveBroadcast) {
        List<KuaishouLiveBroadcast> list = kuaishouLiveBroadcastService.selectKuaishouLiveBroadcastList(kuaishouLiveBroadcast);
        ExcelUtil<KuaishouLiveBroadcast> util = new ExcelUtil<KuaishouLiveBroadcast>(KuaishouLiveBroadcast.class);
        util.exportExcel(response, list, "快手—直播数据");
    }

}
