package thyyxxk.webserver.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.dao.his.MyTestDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.utils.BaiduMapGeocoderUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    private final MyTestDao dao;

    @Autowired
    public TestController(MyTestDao dao) {
        this.dao = dao;
    }

    @PassToken
    @GetMapping("/exec")
    public ResultVo<String> test() {
        List<CodeName> list = dao.selectPatientRawAddress();
        int total = list.size();
        int count = 0;
        for (CodeName item : list) {
            JSONObject cityinfo = BaiduMapGeocoderUtil.getAddressInfoByLngAndLat(item.getName());
            if (null == cityinfo) {
                dao.updateLocation(item.getCode(), null, null, "0");
                continue;
            }
            String adcode = cityinfo.getString("adcode");
            if (null != adcode && !adcode.equals("0") && cityinfo.getString("district") != null) {
                log.info("【{}】：{}", item.getName(), cityinfo);
                dao.updateLocation(item.getCode(), null, null, adcode);
                count += 1;
            }
        }
        String message = String.format("总数据【%d】条，完成【%d】条。", total, count);
        return ResultVoUtil.success(message);
    }
}
