package com.skyversation.poiaddr.controller;

import com.skyversation.poiaddr.config.DbConnection;
import com.skyversation.poiaddr.service.impl.TestDataServiceImpl;
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 TestDataServiceImpl 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 = "level") Integer level, @RequestParam(name = "init") Integer init) {
        // 记录程序开始时间
        long startTime = System.currentTimeMillis();
//      批量处理数据
        if (level == null) {
            level = 1;
        }
        testDataService.iterativeProcessing(pageSize, level, init);// 记录程序结束时间
        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 {
        return DbConnection.getInstance().runSqlStr(sqlStr);
    }
}
