package com.ruoyi.web.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.content.VistorContextHolder;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.syzy.domain.BWwwNews;
import com.ruoyi.syzy.domain.BWwwNewsRef;
import com.ruoyi.syzy.service.IBWwwNewsRefService;
import com.ruoyi.syzy.service.IBWwwNewsService;
import com.ruoyi.web.service.CommService;
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.Collections;
import java.util.List;

@RestController
@Api(tags = "资讯-高考")
@RequestMapping("front/news/")
public class FrontNewsController extends BaseController {

    @Autowired
    private IBWwwNewsService newsService;
    @Autowired
    private IBWwwNewsRefService newsRefService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private CommService commService;
    @Autowired
    private ISysNoticeService noticeService;
    @GetMapping("types")
    @ApiOperation("00 高考资讯类型")
    public TableDataInfo types() {
        return getTypes();
    }

    private TableDataInfo getTypes(){
        List<SysDictData> arr = dictDataService.selectDictDataByType("news_type");
        List<JSONObject> list = new ArrayList<JSONObject>(arr.size());
        arr.forEach(row -> {
            JSONObject item = new JSONObject(true);
            item.put("value", row.getDictValue());
            item.put("label", row.getDictLabel());
            list.add(item);
        });
        return getDataTable(list);
    }

    @GetMapping("typesNoToken")
    @ApiOperation("00-1 高考资讯类型")
    public TableDataInfo typesNoToken() {
        return getTypes();
    }


    @GetMapping("getMainList")
    @ApiOperation("00 通关指南")
    public TableDataInfo getMainList(@ApiParam(value = "省份 ", example = "湖南") @RequestParam(required = false) String location) {
        BWwwNewsRef cond = new BWwwNewsRef();
        cond.setLocation(commService.getLocation(location));
        return getDataTable(newsRefService.selectBWwwNewsRefList(cond));
    }

    @GetMapping("getMainListNoToken")
    @ApiOperation("00 通关指南免登陆")
    public TableDataInfo getMainListNoToken(@ApiParam(value = "省份 ", example = "湖南") @RequestParam(required = false) String location) {
        BWwwNewsRef cond = new BWwwNewsRef();
        cond.setLocation(commService.getLocation(location));
        return getDataTable(newsRefService.selectBWwwNewsRefList(cond));
    }

    @GetMapping("getMainListV2")
    @ApiOperation("00 通关指南免登陆")
    @Anonymous
    public TableDataInfo getMainListV2() {
        BWwwNewsRef cond = new BWwwNewsRef();
        cond.setLocation(commService.getLocation(null));
        cond.setSubType("官方公告");
        List<BWwwNewsRef> list = newsRefService.selectBWwwNewsRefList(cond);
        return getDataTable(list);
    }
    @GetMapping("getSystemNotice")
    @ApiOperation("通知公告")
    @Anonymous
    public TableDataInfo getSystemNotice(SysNotice notice) {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    @GetMapping("getMainCourseDateNoToken")
    @ApiOperation("00 考试日程免登陆")
    public TableDataInfo getMainCourseDateNoToken() {
        return getMainCourseDate();
    }

    @GetMapping("getMainCourseDate")
    @ApiOperation("00 考试日程")
    public TableDataInfo getMainCourseDate() {
        String arrString = sysConfigService.selectConfigByKey("main.course.date");
        if (StringUtils.isBlank(arrString)){
            arrString= "[ { \"title\":  \"高考报名\", \"time\":  \"2023年10月底\", \"fireTime\":  \"2023-10-01\", \"sort\":  1, \"description\":  \"\" }, { \"title\":  \"院校发布招生简章\", \"time\":  \"2024年1月初\", \"fireTime\":  \"2024-02-01\", \"sort\":  2, \"description\":  \"\" }, { \"title\":  \"报考及填报志愿\", \"time\":  \"2月27~3月5日\", \"fireTime\":  \"2024-02-27\", \"sort\":  3, \"description\":  \"登录湖南省普通高校招生考试考生综合信息平台或潇湘高考APP，可填报1-2所院校\" }, { \"title\":  \"第一志愿考试\", \"time\":  \"3月16~17日\", \"fireTime\":  \"2024-03-16\", \"sort\":  4, \"description\":  \"\" }, { \"title\":  \"第一志愿录取\", \"time\":  \"3月中下旬\", \"fireTime\":  \"2024-03-16\", \"sort\":  5, \"description\":  \"\" }, { \"title\":  \"第二志愿考试\", \"time\":  \"4月13~14日\", \"fireTime\":  \"2024-04-13\", \"sort\":  6, \"description\":  \"\" }, { \"title\":  \"第二志愿录取\", \"time\":  \"4月中下旬\", \"fireTime\":  \"2024-04-15\", \"sort\":  7, \"description\":  \"\" } ]";
        }
        return getDataTable(JSONObject.parseArray(arrString));
    }

    @GetMapping("list")
    @ApiOperation("01 资讯列表")
    public TableDataInfo list(@ApiParam(value = "分类 ", example = "1")  String type,Boolean top,String ids,
                              @ApiParam(value = "标签", example = "hot")  String tag,String title,
                              @ApiParam(value = "省份 ", example = "湖南") @RequestParam(required = false) String location,
                              @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
                              @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize) {
        return llist(ids,top,type,tag,title,commService.getLocation(location), pageNum,pageSize);
    }

    private TableDataInfo llist(String ids,Boolean top,String type, String tag,String title,String location, Integer pageNum,Integer pageSize){
        BWwwNews exam = new BWwwNews();
        exam.setIsTop(null==top?null:(top?1:0));
        exam.setType(type);
        exam.setTitle(title);
        exam.setTag(StringUtils.trimToNull(tag));
        exam.setLocation(commService.getLocation(location));
        if(StringUtils.isNotBlank(ids)){
            String[] strArray = ids.split(",");
            List<String> strList = new ArrayList<>(strArray.length);
            Collections.addAll(strList, strArray);
            exam.setIds(strList);
        }
        startPage();
        List<BWwwNews> arr = newsService.selectBWwwNewsList(exam);
        return getDataTable(arr);
    }

    @GetMapping("listNoToken")
    @ApiOperation("01-2 资讯列表")
    public TableDataInfo listNoToken(@ApiParam(value = "分类 ", example = "1")  String type,Boolean top,String ids,
                                     @ApiParam(value = "标签", example = "hot")  String tag,String title,
                                     @ApiParam(value = "省份 ", example = "湖南") @RequestParam(required = false) String location,
                                     @ApiParam(value = "页数", example = "1") Integer pageNum,
                                     @ApiParam(value = "页大小", example = "15") Integer pageSize) {
        return llist(ids,top,type,tag,title,commService.getLocation(location), pageNum,pageSize);
    }

    @GetMapping("info")
    @ApiOperation("02 资讯详情")
    @Anonymous
    public AjaxResult videoInfo(@ApiParam("课程id") Long id) {
        saveClicked(id);
        BWwwNews info = newsService.selectBWwwNewsById(id);
        return AjaxResult.success(info);
    }

    //    @GetMapping("saveClicked")
//    @ApiOperation("03 保存点击数")
    public AjaxResult saveClicked( Long id) {
        BWwwNews info = newsService.selectBWwwNewsById(id);
        Long clicked= null==info.getClicked()?1L:(info.getClicked()+1);
        info.setClicked(clicked);
        newsService.updateBWwwNews(info);
        return AjaxResult.success(clicked);
    }



}
