package com.zjrs.zwnw.fxfk.controller;

import feign.Util;
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.QyywjkBPO;
import com.zjrs.zwnw.fxfk.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/qyywjk")
public class QyywjkController {
    private static final Logger LOG = LoggerFactory.getLogger(QyywjkController.class);

    @Resource
    private QyywjkBPO qyywjkBPO;

    /**
     * 业务量走势图
     *
     * @param dto 查询条件
     * @return 最多10条list
     */
    @ApiOperation(value = "业务量走势图", notes = "业务量走势图")
    @PostMapping("/getQyywzst")
    public AjaxResponse getQyywzst(@RequestBody @Valid QyywzstInputDTO dto, BindingResult bindingResult) {
        LOG.info("业务量走势图开始");
        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<QyywzstOutputDTO> list = qyywjkBPO.getQyywzst(dto);
            resultMap.put("zstdata",list);
            ajaxResponse.setData("data", resultMap);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("业务量走势图结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "业务量TOP5", notes = "业务量TOP5")
    @PostMapping("/getTop5")
    public AjaxResponse getTop5(@RequestBody @Valid QyywTop5InputDTO dto, BindingResult bindingResult) {
        LOG.info("业务量TOP5开始");
        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<QyywTop5OutputDTO> list = qyywjkBPO.getTop5(dto);
            resultMap.put("top5data",list);
            ajaxResponse.setData("data", resultMap);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("业务量TOP5结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "业务量统计总接口", notes = "业务量统计总接口")
    @PostMapping("/getAll")
    public AjaxResponse getAll(@RequestBody QyywTjInputDTO dto) {
        LOG.info("业务量统计总接口开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(Util.isBlank(dto.getSsdq())){
            dto.setSsdq("440800");
        }

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

}
