package com.skyversation.poiaddr.controller;

import com.skyversation.poiaddr.entity.YyskAddressStandardization;
import com.skyversation.poiaddr.service.impl.YyskAddressStandardizationServiceImpl;
import com.skyversation.poiaddr.util.ExcelReaderUtils;
import com.skyversation.poiaddr.util.SerializationUtils;
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.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 法人库处理类
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/corporate_library")
public class CorporateLibraryController {

    @Resource
    private YyskAddressStandardizationServiceImpl yyskAddressStandardizationService;


    /**
     * （开发中）
     * 定时器：每天都全量更新法人库的地名地址数据
     * 每次分页查询每批次处理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, @RequestParam(name = "tableName") String tableName) {
        // 记录程序开始时间
        long startTime = System.currentTimeMillis();
        yyskAddressStandardizationService.iterativeProcessing(pageSize, init, tableName);// 记录程序结束时间
        long endTime = System.currentTimeMillis();
        return "处理完成！用时" + (endTime - startTime) / 1000 + "秒！";
    }

    /**
     * 1、解析xlsx得到所有数据
     * 2、根据主键address和标准化地址standardAddress直接跑规则，属于rule_4的要替换掉同时添加updateTag
     * 3、标准化地址还要做一层去空和街镇异常处理，有变动的也添加updateTag
     *
     * @return
     */
    @PostMapping(value = "/dataQingXi", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object dataQingXi() {
        // 记录程序开始时间
        long startTime = System.currentTimeMillis();
        SerializationUtils.test();
        long endTime = System.currentTimeMillis();
        return "处理完成！用时" + (endTime - startTime) / 1000 + "秒！";
    }


    @PostMapping(value = "/updateByTag", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object updateByTag() {
        // 记录程序开始时间
        long startTime = System.currentTimeMillis();
        try {
            List<Map<String, Object>> fileDatas = ExcelReaderUtils.readExcel("output/yysk_new_wcb_address_standardization.xlsx");
            List<YyskAddressStandardization> sjArrDzbzhSjWcbryDzxxList = new ArrayList<>();
            for (Map<String, Object> item : fileDatas) {
                if (item.get("updateTag") != null && Boolean.parseBoolean(item.get("updateTag").toString())) {
                    YyskAddressStandardization yyskAddressStandardization = new YyskAddressStandardization();
                    yyskAddressStandardization.setAddress(item.get("address").toString());
                    yyskAddressStandardization.setMatchLevel(item.get("match_level").toString());
                    yyskAddressStandardization.setStandardAddress(item.get("standard_address").toString());
                    sjArrDzbzhSjWcbryDzxxList.add(yyskAddressStandardization);
                }
            }
            System.out.println("要处理的数据条数：" + sjArrDzbzhSjWcbryDzxxList.size());
            yyskAddressStandardizationService.updateDatas2(sjArrDzbzhSjWcbryDzxxList, "yysk_wcb_address_standardization");
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        return "处理完成！用时" + (endTime - startTime) / 1000 + "秒！";
    }
}
