package com.goldenwater.slaj.hazard.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.slaj.hazard.domain.TBusiHazCount;
import com.goldenwater.slaj.hazard.domain.TBusiHazCountMon;
import com.goldenwater.slaj.hazard.service.ITBusiHazCountService;
import com.goldenwater.slaj.hazard.service.ITBusiHazCountMonService;

/**
 * 危险源统计
 */
@RestController
@RequestMapping("/hazard/tbusihazcount")
public class TBusiHazCountController extends BaseController {
    @Autowired
    private ITBusiHazCountService tBusiHazCountService;

    @Autowired
    private ITBusiHazCountMonService tBusiHazCountMonService;

    @GetMapping("/list")
    public TableDataInfo list(TBusiHazCount tBusiHazCount) {
        startPage();
        List<TBusiHazCount> list = tBusiHazCountService.selectTBusiHazCountList(tBusiHazCount);
        return getDataTable(list);
    }

    @GetMapping("/{guid}")
    public AjaxResult getInfo(@PathVariable String guid) {
        return AjaxResult.success(tBusiHazCountService.selectTBusiHazCountByGuid(guid));
    }

    @PostMapping
    public AjaxResult add(@RequestBody TBusiHazCount tBusiHazCount) {
        return toAjax(tBusiHazCountService.insertTBusiHazCount(tBusiHazCount));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody TBusiHazCount tBusiHazCount) {
        return toAjax(tBusiHazCountService.updateTBusiHazCount(tBusiHazCount));
    }

    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(tBusiHazCountService.deleteTBusiHazCountByGuids(guids));
    }

    /**
     * 统计汇总查询（按等级统计）
     */
    @GetMapping("/summary")
    public AjaxResult summary(TBusiHazCount query) {
        List<TBusiHazCount> list = tBusiHazCountService.selectTBusiHazCountList(query);
        Map<String, Object> result = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            TBusiHazCount latest = list.get(0);
            result.put("ybsNum", latest.getYbsNum() != null ? latest.getYbsNum() : 0);
            result.put("alrhaz", latest.getAlrhaz() != null ? latest.getAlrhaz() : 0);
            result.put("ybHaz", latest.getYbHaz() != null ? latest.getYbHaz() : 0);
            result.put("zdHaz", latest.getZdHaz() != null ? latest.getZdHaz() : 0);
            result.put("ybGkHaz", latest.getYbGkHaz() != null ? latest.getYbGkHaz() : 0);
            result.put("zdGkHaz", latest.getZdGkHaz() != null ? latest.getZdGkHaz() : 0);
            result.put("hazBsl", latest.getHazBsl() != null ? latest.getHazBsl() : 0);
            result.put("totGkl", latest.getTotGkl() != null ? latest.getTotGkl() : 0);
            result.put("engNum", latest.getEngNum() != null ? latest.getEngNum() : 0);
            result.put("alrEngNum", latest.getAlrEngNum() != null ? latest.getAlrEngNum() : 0);
        }
        return AjaxResult.success(result);
    }

    /**
     * 按等级统计（返回各等级数量）
     */
    @GetMapping("/byGrade")
    public AjaxResult byGrade(TBusiHazCount query) {
        List<TBusiHazCount> list = tBusiHazCountService.selectTBusiHazCountList(query);
        Map<String, Object> result = new HashMap<>();
        int totalYbHaz = 0;
        int totalZdHaz = 0;
        int totalYbGk = 0;
        int totalZdGk = 0;
        if (list != null) {
            for (TBusiHazCount item : list) {
                totalYbHaz += item.getYbHaz() != null ? item.getYbHaz() : 0;
                totalZdHaz += item.getZdHaz() != null ? item.getZdHaz() : 0;
                totalYbGk += item.getYbGkHaz() != null ? item.getYbGkHaz() : 0;
                totalZdGk += item.getZdGkHaz() != null ? item.getZdGkHaz() : 0;
            }
        }
        result.put("generalHazCount", totalYbHaz);
        result.put("majorHazCount", totalZdHaz);
        result.put("generalControlCount", totalYbGk);
        result.put("majorControlCount", totalZdGk);
        result.put("totalHazCount", totalYbHaz + totalZdHaz);
        result.put("totalControlCount", totalYbGk + totalZdGk);
        return AjaxResult.success(result);
    }
}
