package com.ruoyi.web.controller.gas;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.ElasticConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.framework.config.ElasticSearchClient;
import com.ruoyi.system.domain.SyncPost;
import com.ruoyi.web.service.IGasCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 公共提出接口
 * @Author: huangcheng
 * @Date: 2021/8/30
 * @Version V1.0
 */
@RestController
@RequestMapping("/gas/common")
@Api(tags = "公共接口")
public class GasCommonController {

    @Autowired
    private IGasCommonService gasCommonService;
    @Autowired
    private ElasticSearchClient client;

    @PreAuthorize("@ss.hasPermi('system:gas:sync')")
    @PostMapping("/sync")
    @ApiOperation("第三方表数据同步ES")
    public AjaxResult sync(@RequestBody SyncPost syncPost) {
        gasCommonService.sync(syncPost);
        return AjaxResult.success("同步中...");
    }

    @GetMapping("/illegalShipStatic")
    @ApiOperation("获取硫、黑烟超标数量统计")
    public AjaxResult illegalShipStatic() {
//        Map<String, Object> params = baseEntity.getParams();
//        if (ObjectUtil.isNotEmpty(params.get("beginTime"))) {
//            params.put("beginTime", params.get("beginTime") + " 00:00:00");
//        }
//        if (ObjectUtil.isNotEmpty(params.get("endTime"))) {
//            params.put("endTime", params.get("endTime") + " 23:59:59");
//        }
//        Map<String, Object> so2RangeCondition = new HashMap<>();
//        so2RangeCondition.put("createTime", StrUtil.concat(true, "[", Convert.toStr(params.get("beginTime")), ",", Convert.toStr(params.get("endTime")), "]"));
//        so2RangeCondition.put("peakTime", StrUtil.concat(true, "[", Convert.toStr(params.get("beginTime")), ",", Convert.toStr(params.get("endTime")), "]"));
        long so2Count = client.count(null, null, ElasticConstants.SO2_ALERT, ElasticConstants.AIS_ILLEGAL_SHIP);
        long blackCount = client.count(null, null, ElasticConstants.HEIYAN_SHIP_RECOGNITION);
        Map<String, Object> result = new HashMap<>();
        result.put("so2Count", so2Count);
        result.put("blackCount", blackCount);
        return AjaxResult.success(result);
    }

}
