package com.malk.mc.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.malk.core.McProject;
import com.malk.mc.entity.McRequestRecord;
import com.malk.mc.mapper.McRequestRecordMapper;
import com.malk.mc.service.McProjectService;
import com.malk.server.common.McR;
import com.malk.utils.PublicUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/project/")
public class McProjectController {

    @Autowired
    private McProjectService mcProjectService;
    @Autowired
    private McRequestRecordMapper mcRequestRecordMapper;

    @PostMapping("add")
    public McR add(@RequestBody JSONObject param){
        if(PublicUtil.isNull(param,"projectId","appType","systemToken")){
            return McR.errorNullPointer();
        }
        McProject.addYida(param.getString("projectId"),new String[]{param.getString("appType"),param.getString("systemToken")});
        return McR.success(McProject.getYida(param.getString("projectId")));
    }

    @GetMapping("queryAll")
    public McR queryAll(){
        return McR.success(McProject.getYida());
    }

    @GetMapping("clear")
    public McR clear(){
        McProject.clearYida();
        return McR.success(McProject.getYida());
    }

    @GetMapping("init")
    public McR init(){
        mcProjectService.init();
        return McR.success(McProject.getYida());
    }

    @GetMapping("logs")
    public McR getLog(@RequestParam(required = false, defaultValue = "0") int currentPage,
                      @RequestParam(required = false, defaultValue = "0") int pageSize,
                      @RequestParam(required = false) String pid,
                      @RequestParam(required = false) String uri,
                      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startDate,
                      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endDate){
        Page<McRequestRecord> page = new Page<>(currentPage, pageSize);
        QueryWrapper<McRequestRecord> queryWrapper = new QueryWrapper<>();
        if(!PublicUtil.isNull(pid)){
            queryWrapper.eq("pid", pid);
        }
        if(!PublicUtil.isNull(uri)){
            queryWrapper.like("request_uri", uri);
        }
        if (startDate != null && endDate != null) {
            queryWrapper.between("timestamp", startDate, endDate);
        }
        mcRequestRecordMapper.selectPage(page,queryWrapper);
        return McR.success(page);
    }

    @PostMapping("test")
    public McR test(@RequestBody JSONObject param){
        return McR.success(LocalDateTime.now()+JSONObject.toJSONString(param));
    }

}
