package com.zhiqiyun.open.mvc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dliyun.oap.framework.service.RouterService;
import com.dliyun.oap.framework.service.ServiceMethodHandler;
import com.zhiqiyun.open.core.models.bz.BaseDataInfo;
import com.zhiqiyun.open.core.service.AppKeyInfoService;
import com.zhiqiyun.open.core.service.BaseDataInfoService;
import com.zhiqiyun.open.mvc.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class IndexController {
    @Autowired
    private RouterService routerService;

    @Autowired
    private AppKeyInfoService appKeyInfoService;

    @Autowired
    private BaseDataInfoService baseDataInfoService;

    @ResponseBody
    @PostMapping("/bz/base/findSelectByKeyword")
    public Result findSelectBaseInfoByKeyword(String keyword) throws IOException {
        QueryWrapper<BaseDataInfo> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("id", keyword);
            queryWrapper.or();
            queryWrapper.like("name", keyword);
            queryWrapper.or();
            queryWrapper.like("brief", keyword);
            queryWrapper.or();
            queryWrapper.like("address", keyword);
            queryWrapper.or();
            queryWrapper.like("contacts", keyword);
            queryWrapper.or();
            queryWrapper.like("contact_number", keyword);
        }
        queryWrapper.last("limit 10");
        queryWrapper.orderByDesc("id");

        List<BaseDataInfo> listData = this.baseDataInfoService.list(queryWrapper);
        return Result.instance(Result.Code.SUCCESS).setData(listData);
    }

    @ResponseBody
    @PostMapping("/bz/base/findByMapBounds")
    public Result findByMapBounds(BigDecimal maxlng, BigDecimal maxlat, BigDecimal minlng, BigDecimal minlat) {
        QueryWrapper<BaseDataInfo> queryWrapper = new QueryWrapper<>();
        if (maxlng != null) {
            queryWrapper.le("lng", maxlng);
        }
        if (maxlat != null) {
            queryWrapper.le("lat", maxlat);
        }
        if (minlng != null) {
            queryWrapper.ge("lng", minlng);
        }
        if (minlat != null) {
            queryWrapper.ge("lat", minlat);
        }

        List<BaseDataInfo> listData = this.baseDataInfoService.list(queryWrapper);
        return Result.instance(Result.Code.SUCCESS).setData(listData);
    }

    @GetMapping("/")
    public void index(HttpServletResponse response) throws IOException {
        response.sendRedirect("/index.html");
    }

    @ResponseBody
    @PostMapping("/dashboard")
    public Result dashboard() throws IOException {
        Map<String, ServiceMethodHandler> serviceMethodHandlerMap = this.routerService.getOapContext().getAllServiceMethodHandlers();
        int serviceMethodHandlerCount = serviceMethodHandlerMap.size();

        long appKeyCount = this.appKeyInfoService.count();

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("serviceMethodHandlerCount", serviceMethodHandlerCount);
        dataMap.put("appKeyCount", appKeyCount);

        return Result.instance(Result.Code.SUCCESS).setData(dataMap);
    }
}
