package com.zjrs.zwnw.fxfk.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.zwnw.fxfk.bpo.SsywjkBPO;
import com.zjrs.zwnw.fxfk.dto.FbqssllDTO;
import com.zjrs.zwnw.fxfk.dto.Zbtop10DTO;
import com.zjrs.zwnw.fxfk.dto.Zbtop10ListDTO;
import com.zjrs.zwnw.sxgl.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
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 jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("实时业务监控")
@RestController
@RequestMapping("/api/fxfk/ssywjk")
public class SsywjkController {
    private static final Logger LOG = LoggerFactory.getLogger(SsywjkController.class);

    @Resource
    private SsywjkBPO ssywjkBPO;

    /**
     * 根据所属地区查询在办业务量TOP10
     *
     * @param dto 查询条件
     * @return 最多10条list
     */
    @ApiOperation(value = "根据所属地区查询在办业务量TOP10", notes = "根据所属地区查询在办业务量TOP10")
    @PostMapping("/getZbtop10List")
    public AjaxResponse getZbtop10List(@RequestBody @Valid Zbtop10DTO dto, BindingResult bindingResult) {
        LOG.info("根据所属地区查询在办业务量TOP10开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            Map<String, Object> resultMap = new HashMap<String, Object>();
            List<Zbtop10ListDTO> list = ssywjkBPO.getZbtop10List(dto);
            resultMap.put("top10data",list);
            ajaxResponse.setData("data", resultMap);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("根据所属地区查询在办业务量TOP10结束");
        return ajaxResponse;
    }
    /**
     * 查询各县市区业务量分布趋势
     *
     * @param dto 查询条件
     * @return
     */
    @ApiOperation(value = "查询各县市区业务量分布趋势", notes = "查询各县市区业务量分布趋势")
    @PostMapping("/getFbqs")
    public AjaxResponse getFbqs(@RequestBody @Valid FbqssllDTO dto, BindingResult bindingResult) {
        LOG.info("查询各县市区业务量分布趋势开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        String type = dto.getType();

        if(!"1".equals(type) && !"3".equals(type) && !"7".equals(type) ){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("统计时间请传1，3或7,分别代表近一月、近三月、近一星期");
            return ajaxResponse;
        }

        try {
            Map<String,Object> resultMap = ssywjkBPO.getFbqs(dto);
            ajaxResponse.setData("data", resultMap);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("查询各县市区业务量分布趋势结束");
        return ajaxResponse;
    }
    /**
     * 实时业务监控总接口
     *
     * @return
     */
    @ApiOperation(value = "实时业务监控总接口", notes = "实时业务监控总接口")
    @PostMapping("/getAll")
    public AjaxResponse getAll() {
        LOG.info("实时业务监控总接口开始");
        AjaxResponse ajaxResponse = new AjaxResponse();

        try {
            Map<String,Object> resultMap = ssywjkBPO.getAll();
            ajaxResponse.setData("data", resultMap);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("实时业务监控总接口结束");
        return ajaxResponse;
    }
}
