package org.jeecg.modules.newWarning.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.newWarning.entity.TabQi;
import org.jeecg.modules.newWarning.service.TabQiService;
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 org.jeecg.common.api.vo.Result;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/newWarn/tabQi")
@Slf4j
public class TabQiController {

    @Resource
    private TabQiService tabQiService;

    /**
     * 获取平滑区间数据
     * @param tabQi
     * @return
     */
    @GetMapping("/getSmoothInterval")
    public Result<?> getSmoothInterval(TabQi tabQi,
                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                       HttpServletRequest req){
        JSONObject smoothInterval=new JSONObject();
        smoothInterval = tabQiService.getSmoothInterval(tabQi);
        return Result.OK(smoothInterval);
    }

    @GetMapping("/delRedisJh")
    public Result<?> delRedisJh(@RequestParam("tableName")String tableName){
        tabQiService.delRedisJh(tableName);
        return Result.OK();
    }

    @GetMapping("/getChatData")
    public Result<?> getChatData(TabQi tabQi,
                                 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                 HttpServletRequest req){
        Page<TabQi> page = new Page<TabQi>(pageNo, pageSize);
        QueryWrapper<TabQi> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(tabQi.getStartDate()!=null||(!"".equals(tabQi.getStartDate()))){
            queryWrapper.apply(("UNIX_TIMESTAMP(TIME) >= UNIX_TIMESTAMP('" + sdf.format(tabQi.getStartDate())  + "')"));
        }
        queryWrapper.orderByAsc("TIME");
        Page<TabQi> res = tabQiService.selectListPage(tabQi.getJhTableName(), queryWrapper,page);
        return Result.OK(res.getRecords());
    }

    @GetMapping("/testGet")
    public Result<?> testGet(){
        TabQi ltd_牛页1_3_803HF = tabQiService.selectOneOrcale("LTD_牛页1_3_803HF");
        return Result.OK(ltd_牛页1_3_803HF);
    }
}
