package com.skyversation.poiaddr.controller;

import com.skyversation.poiaddr.addquery.AddressQueryEngine;
import com.skyversation.poiaddr.config.DbConnection;
import com.skyversation.poiaddr.service.impl.SjArrDzbzhSjWcbryDzxxServiceImpl;
import com.skyversation.poiaddr.util.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;

@Slf4j
@Validated
@RestController
@RequestMapping("/poiAddress")
public class PoiAddressController {

    @Resource
    private SjArrDzbzhSjWcbryDzxxServiceImpl testDataService;

    //    地址查询
    @GetMapping(value = "/selectAddressInfo/{address}")
    public Object selectAddressInfo(@PathVariable("address") String address) {
        return RequestUtils.request(address);
    }


    /**
     * （开发中）
     * 定时器：每天都全量更新法人库的地名地址数据
     * 每次分页查询每批次处理1W条数据
     * 1、获取数据库连接
     * 2、查询数据
     * 3、得到地名地址字段和值（清洗数据，并地名格式调整）
     * 4、批量多任务处理查询结果并判断网格区划
     * 5、将结果保存为ser文件
     */
    @PostMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object updateDataBaseData(@RequestParam(name = "pageSize") Integer pageSize, @RequestParam(name = "init") Integer init) {
        // 记录程序开始时间
        long startTime = System.currentTimeMillis();
        testDataService.iterativeProcessing(pageSize, init);// 记录程序结束时间
        long endTime = System.currentTimeMillis();
        return "处理完成！用时" + (endTime - startTime) / 1000 + "秒！";
    }

    /**
     * 全量查询表数据并将处理后的结果序列化到本地(待测试)
     *
     * @return
     */
    @PostMapping(value = "/uploadDataBaseDataToLocal", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object uploadDataBaseDataToLocal(@RequestParam(name = "reAddr") String reAddr,@RequestParam(name = "updateBase") Boolean updateBase) {
        // 记录程序开始时间
        long startTime = System.currentTimeMillis();
        testDataService.uploadAllData(reAddr,updateBase);// 记录程序结束时间
        long endTime = System.currentTimeMillis();
        return "处理完成！用时" + (endTime - startTime) / 1000 + "秒！";
    }

    @GetMapping(value = "/uploadDataBase", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object uploadDataBase(){
        long startTime = System.currentTimeMillis();
        testDataService.uploadDataBase();
        long endTime = System.currentTimeMillis();
        return "处理完成！用时" + (endTime - startTime) / 1000 + "秒！";
    }

    /**
     * 直接输入sql语句执行并返回结果
     *
     * @param sqlStr
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/runsSql/{sqlStr}")
    public Object runSql(@PathVariable(name = "sqlStr") String sqlStr) throws SQLException {
        if (sqlStr.contains("update")) {
            return DbConnection.getInstance().updateSql(sqlStr);
        } else {
            return DbConnection.getInstance().runSqlStr(sqlStr);
        }
    }

    /**
     * 直接输地址并返回结果
     *
     * @param address
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/searchAddr")
    public Object searchByAddrStr(@RequestParam(name = "address") String address) {
        return AddressQueryEngine.getInstance().sj_szxSearchByName(address);
    }
}