package com.ruoyi.web.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.syzy.domain.BWwwNewsVideo;
import com.ruoyi.syzy.service.IBWwwNewsVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "高考资讯视频")
@RequestMapping("front/newsVideo/")
public class FrontNewsVideoController extends BaseController {

    @Autowired
    private IBWwwNewsVideoService newsVideoService;

    @GetMapping("types")
    @ApiOperation("00 高考资讯视频类型")
    public TableDataInfo types() {
        List<String> arr = newsVideoService.selectBWwwNewsVideoTypes();
        List<JSONObject> list = new ArrayList<JSONObject>(arr.size());
        arr.forEach(row -> {
            JSONObject item = new JSONObject(true);
            item.put("value", row);
            item.put("label", row);
            list.add(item);
        });
        return getDataTable(list);
    }

    @GetMapping("subTypes")
    @ApiOperation("001 高考资讯视频类型")
    public TableDataInfo subTypes(@RequestParam(required = true) String type) {
        List<String> arr = newsVideoService.selectBWwwNewsVideoSubTypes(type);
        List<JSONObject> list = new ArrayList<JSONObject>(arr.size());
        arr.forEach(row -> {
            JSONObject item = new JSONObject(true);
            item.put("value", row);
            item.put("label", row);
            list.add(item);
        });
        return getDataTable(list);
    }

    @GetMapping("list")
    @ApiOperation("01 资讯视频列表")
    public TableDataInfo list(@ApiParam(value = "分类 ", example = "1") @RequestParam String type,String subType,
        @ApiParam(value = "标签", example = "hot") @RequestParam String tag,String title,String from,
        @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
        @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize) {

        return getNewsVideoList( type,subType, tag, title, from, pageNum, pageSize);
    }

    @GetMapping("listNoToken")
    @ApiOperation("01 资讯视频列表")
    public TableDataInfo listNoToken(@ApiParam(value = "分类 ", example = "1") @RequestParam String type,
                              @ApiParam(value = "标签", example = "hot") @RequestParam String tag,String title,String from,
                              @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
                              @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize) {
        return getNewsVideoList( type,null, tag, title, from, pageNum, pageSize);
    }

    private TableDataInfo getNewsVideoList(String type,String subType,String tag,String title, String from,Integer pageNum,Integer pageSize){
        BWwwNewsVideo exam = new BWwwNewsVideo();
        exam.setType(type);
        exam.setSubType(subType);
        exam.setTitle(title);
        exam.setTag(StringUtils.trimToNull(tag));
        exam.setSource(StringUtils.trimToNull(from));
        startPage();
        List<BWwwNewsVideo> arr = newsVideoService.selectBWwwNewsVideoList(exam);
        return getDataTable(arr);
    }

    @GetMapping("saveClicked")
    @ApiOperation("03 保存点击数")
    public AjaxResult videoInfo(@ApiParam("视频id") Long id) {
        BWwwNewsVideo info = newsVideoService.selectBWwwNewsVideoById(id);
        Long clicked= null==info.getClicked()?1L:(info.getClicked()+1);
        info.setClicked(clicked);
        newsVideoService.updateBWwwNewsVideo(info);
        return AjaxResult.success(clicked);
    }
}
