package cn.com.ctop.kuaishou.modules.live.controller;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.live.entity.LiveDataMaterial;
import cn.com.ctop.kuaishou.modules.live.service.ILiveDataImportRecordService;
import cn.com.ctop.kuaishou.modules.live.service.ILiveDataMaterialService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 直播数据——素材报表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2022-03-30
 */
@Slf4j
@Api(tags = "直播数据——素材报表")
@RestController
@RequestMapping("/liveData/material")
public class LiveDataMaterialController {
    @Autowired
    private ILiveDataMaterialService liveDataMaterialService;
    @Autowired
    private ILiveDataImportRecordService recordService;

    /**
     * 分页列表查询
     */
    @GetMapping(value = "/list")
    public Result<IPage<LiveDataMaterial>> queryPageList(LiveDataMaterial liveDataMaterial,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        Result<IPage<LiveDataMaterial>> result = new Result<>();
        QueryWrapper<LiveDataMaterial> queryWrapper = QueryGenerator.initQueryWrapper(new LiveDataMaterial(), req.getParameterMap());
        if (!Check.isNull(liveDataMaterial.getAccountId())) {
            queryWrapper.eq("account_id", liveDataMaterial.getAccountId());
        }
        if (!Check.isNull(liveDataMaterial.getStartTime())) {
            queryWrapper.ge("stat_date", liveDataMaterial.getStartTime());
        }

        if (!Check.isNull(liveDataMaterial.getEndTime())) {
            queryWrapper.le("stat_date", liveDataMaterial.getEndTime());
        }

        if (!Check.isNull(liveDataMaterial.getVideoId())) {
            queryWrapper.like("video_id", liveDataMaterial.getVideoId());
        }
        queryWrapper.eq("state", 1);
        queryWrapper.orderByDesc("create_time");
        Page<LiveDataMaterial> page = new Page<LiveDataMaterial>(pageNo, pageSize);
        IPage<LiveDataMaterial> pageList = liveDataMaterialService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 导入EXCEL批量创建
     */
    @PostMapping(value = "/importExcel")
    @ResponseBody
    public Result<Object> importItemExcel(HttpServletResponse response, String userId, Long accountId, @RequestParam("file") MultipartFile file) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        try {
            if (Check.isNull(file)) {
                return Result.error("执行失败，请上传应用Excel文件");
            }
            if (Check.isNull(accountId) || Check.isNull(userId)) {
                return Result.error("缺少参数");
            }
            Result<Object> result = liveDataMaterialService.importItemExcel(accountId, file);
            recordService.addImportRecord(accountId, 1, userId);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("导入EXCEL失败，" + e.getMessage());
        }
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "直播数据——素材报表-通过id查询", notes = "直播数据——素材报表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LiveDataMaterial> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<LiveDataMaterial> result = new Result<>();
        LiveDataMaterial liveDataMaterial = liveDataMaterialService.getById(id);
        if (liveDataMaterial == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(liveDataMaterial);
            result.setSuccess(true);
        }
        return result;
    }
}
