package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.crawler.modules.oceanengine.entity.DouyinHot;
import cn.com.ctop.crawler.modules.oceanengine.entity.EffectCase;
import cn.com.ctop.crawler.modules.oceanengine.entity.HotMaterial;
import cn.com.ctop.crawler.modules.oceanengine.service.IDouyinHotService;
import cn.com.ctop.crawler.modules.oceanengine.service.IEffectCaseService;
import cn.com.ctop.crawler.modules.oceanengine.service.IHotMaterialService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jeecg-boot
 */
@RestController
@RequestMapping("/ctop/hot")
@Slf4j
public class HotInformationCtroller {
    @Autowired
    private IDouyinHotService douyinHotService;
    @Autowired
    private IEffectCaseService effectCaseService;
    @Autowired
    private IHotMaterialService hotMaterialService;

    @ResponseBody
    @GetMapping("/douyin/feeds")
    public Result<IPage<DouyinHot>> getVideoList(HttpServletRequest req) {
        Result<IPage<DouyinHot>> result = new Result<>();
        Map<String, String[]> paramMap = req.getParameterMap();
        String userName = paramMap.get("userName") != null ? paramMap.get("userName")[0] : null;
        String start = paramMap.get("start") != null ? paramMap.get("start")[0] : null;
        String end = paramMap.get("end") != null ? paramMap.get("end")[0] : null;
        String orderByColumn = paramMap.get("orderByColumn") != null ? paramMap.get("orderByColumn")[0] : null;
        String orderByType = paramMap.get("orderByType") != null ? paramMap.get("orderByType")[0] : null;
        Integer pageSize = paramMap.get("pageSize") != null ? Integer.parseInt(paramMap.get("pageSize")[0]) : null;
        Integer pageNo = paramMap.get("pageNo") != null ? Integer.parseInt(paramMap.get("pageNo")[0]) : null;
        IPage<DouyinHot> list = douyinHotService.queryHotFeeds(userName, start, end, orderByColumn, orderByType, pageNo, pageSize);
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }

    @ResponseBody
    @GetMapping("/effectcase/feeds")
    public Result<IPage<EffectCase>> getEffectcaseList(HttpServletRequest req) {
        Result<IPage<EffectCase>> result = new Result<>();
        Map<String, String[]> paramMap = req.getParameterMap();
        String userName = paramMap.get("userName") != null ? paramMap.get("userName")[0] : null;
        String start = paramMap.get("start") != null ? paramMap.get("start")[0] : null;
        String end = paramMap.get("end") != null ? paramMap.get("end")[0] : null;
        String orderByColumn = paramMap.get("orderByColumn") != null ? paramMap.get("orderByColumn")[0] : null;
        String orderByType = paramMap.get("orderByType") != null ? paramMap.get("orderByType")[0] : null;
        Integer pageSize = paramMap.get("pageSize") != null ? Integer.parseInt(paramMap.get("pageSize")[0]) : null;
        Integer pageNo = paramMap.get("pageNo") != null ? Integer.parseInt(paramMap.get("pageNo")[0]) : null;
        IPage<EffectCase> list = effectCaseService.queryHotFeeds(userName, start, end, orderByColumn, orderByType, pageNo, pageSize);
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }

    @ResponseBody
    @GetMapping("/material/feeds")
    public Result<IPage<HotMaterial>> getMaterialList(HttpServletRequest req) {
        Result<IPage<HotMaterial>> result = new Result<>();
        Map<String, String[]> paramMap = req.getParameterMap();
        String userName = paramMap.get("userName") != null ? paramMap.get("userName")[0] : null;
        String title = paramMap.get("title")!=null?paramMap.get("title")[0]:null;
        String start = paramMap.get("start") != null ? paramMap.get("start")[0] : null;
        String end = paramMap.get("end") != null ? paramMap.get("end")[0] : null;
        String orderByColumn = paramMap.get("orderByColumn") != null ? paramMap.get("orderByColumn")[0] : null;
        String orderByType = paramMap.get("orderByType") != null ? paramMap.get("orderByType")[0] : null;
        Integer pageSize = paramMap.get("pageSize") != null ? Integer.parseInt(paramMap.get("pageSize")[0]) : null;
        Integer pageNo = paramMap.get("pageNo") != null ? Integer.parseInt(paramMap.get("pageNo")[0]) : null;
        IPage<HotMaterial> list = hotMaterialService.queryHotFeeds(title,userName, start, end, orderByColumn, orderByType, pageNo, pageSize);
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }

    @ResponseBody
    @GetMapping("/video/url")
    public Map<String, Object> getVideoUrl(@RequestParam(name = "id") String id, @RequestParam(name = "type") Integer type) {
        try {
            return effectCaseService.getVideoUrl(id, type);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_SERVER_ERROR);
            return result;
        }
    }
}
