package cn.com.tenlion.systembase.controller.api.keyareacheckrenovation;

import cn.com.tenlion.systembase.pojo.dtos.camera.CameraDTO;
import cn.com.tenlion.systembase.pojo.dtos.keyareacheckrenovation.KeyAreaCheckRenovationDTO;
import cn.com.tenlion.systembase.pojo.vos.keyareacheckrenovation.KeyAreaCheckRenovationVO;
import cn.com.tenlion.systembase.service.keyareacheckrenovation.IKeyAreaCheckRenovationService;
import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.LatLngTransformUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: KeyAreaCheckRenovationController
 * @Description: 重点地区排查整治
 * @Author: CodeFactory
 * @Date: 2021-10-12 16:37:43
 * @Version: 3.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "重点地区排查整治接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/keyareacheckrenovation")
public class KeyAreaCheckRenovationController extends DefaultBaseController {

    @Autowired
    private IKeyAreaCheckRenovationService keyAreaCheckRenovationService;

    @ApiOperation(value = "新增重点地区排查整治", notes = "新增重点地区排查整治接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult save(@RequestBody KeyAreaCheckRenovationVO keyAreaCheckRenovationVO) {
        keyAreaCheckRenovationService.save(keyAreaCheckRenovationVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "删除重点地区排查整治", notes = "删除重点地区排查整治接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult remove(@PathVariable("ids") String ids) {
        keyAreaCheckRenovationService.remove(Arrays.asList(ids.split("\\_")), ids);
        return new SuccessResult();
    }

    @ApiOperation(value = "修改重点地区排查整治", notes = "修改重点地区排查整治接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "keyAreaCheckRenovationId", value = "重点地区排查整治ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{keyAreaCheckRenovationId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("keyAreaCheckRenovationId") String keyAreaCheckRenovationId, @RequestBody KeyAreaCheckRenovationVO keyAreaCheckRenovationVO) {
        keyAreaCheckRenovationService.update(keyAreaCheckRenovationId, keyAreaCheckRenovationVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "重点地区排查整治详情", notes = "重点地区排查整治详情接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "keyAreaCheckRenovationId", value = "重点地区排查整治ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{keyAreaCheckRenovationId}")
        public KeyAreaCheckRenovationDTO get(@PathVariable("keyAreaCheckRenovationId") String keyAreaCheckRenovationId) {
        return keyAreaCheckRenovationService.get(keyAreaCheckRenovationId);
    }

    @ApiOperation(value = "重点地区排查整治列表", notes = "重点地区排查整治列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<KeyAreaCheckRenovationDTO> list() {
        Map<String, Object> params = requestParams();
        return keyAreaCheckRenovationService.list(params);
    }

    @ApiOperation(value = "重点地区排查整治分页列表", notes = "重点地区排查整治分页列表接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
        @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
        @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<KeyAreaCheckRenovationDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return keyAreaCheckRenovationService.listPage(page);
    }

    @ApiOperation(value = "重点地区排查整治统计", notes = "重点地区排查整治统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("count")
    SuccessResultData<Integer> count() {
        Map<String, Object> params = requestParams();
        return new SuccessResultData<>(keyAreaCheckRenovationService.count(params));
    }

    @ApiOperation(value = "重点地区排查整治统计", notes = "重点地区排查整治统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("selectZuoBiao")
    public double[] selectZuoBiao() {
        Map<String, Object> params = requestParams();
        String lon = params.get("longitude").toString();
        String lat = params.get("latitude").toString();
        return LatLngTransformUtil.gps84_To_Gcj02(Double.parseDouble(lat), Double.parseDouble(lon));
    }

    //根据转换后的坐标查出地址
    @ApiOperation(value = "重点地区排查整治统计", notes = "重点地区排查整治统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("selectDiZhi")
    public Object selectDiZhi() {
        Map<String, Object> params = requestParams();
        String lon = params.get("longitude").toString();
        String lat = params.get("latitude").toString();
        String json="";
        String str = "";
        BufferedReader br=null;//读取访问结果
        try {
            URL getwalletAmountUrl = new URL("http://api.map.baidu.com/geocoder/v2/?ak=USVjenOjbDbRDSeVvDwWgMk2DEsNdkDB&location="+lat+","+lon+"&output=json&pois=1");
            System.out.println(getwalletAmountUrl);
            URLConnection context = getwalletAmountUrl.openConnection();
            br = new BufferedReader(new InputStreamReader(
                    context.getInputStream(), "UTF-8"));

            while ((str=br.readLine())!=null) {
                json += str;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if (br != null) {//关闭流
                try {
                    br.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
        return json;
    }

    @ApiOperation(value = "重点地区排查整治统计", notes = "重点地区排查整治统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("device-list-page")
    public SuccessResultList<List<CameraDTO>> deviceListPage(ListPage page) {
        Map<String, Object> params = requestParams();
        String keyAreaId = params.get("keyAreaId") == null ? null : params.get("keyAreaId").toString();
        params = new HashMap<>();
        params.put("deviceType", 33);
        params.put("fromIndex", 0);
        params.put("toIndex", 100000000);
        List<CameraDTO> countList = keyAreaCheckRenovationService.deviceList(params, "count", keyAreaId);
        params.put("fromIndex", page.getPage() == 1 ? 0 : ((page.getPage() - 1) * page.getRows() + 1));
        params.put("toIndex", page.getPage() * page.getRows());
        List<CameraDTO> pageList = keyAreaCheckRenovationService.deviceList(params, "list", keyAreaId);
        SuccessResultList<List<CameraDTO>> resultList = new SuccessResultList<>();
        resultList.setPage(countList.size() % page.getPage());
        resultList.setRows(pageList);
        resultList.setTotal(Long.parseLong("" + countList.size()));
        return resultList;
    }

}