package com.yuchen.saas.park.service.controller.alarm;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.dto.park.WarningDetailListQueryDTO;
import com.yuchen.saas.park.api.entity.park.PmgBatWarningInfo;
import com.yuchen.saas.park.api.vo.park.WarningDetailCountByLevelVo;
import com.yuchen.saas.park.api.vo.park.WarningDetailCountByStatusVo;
import com.yuchen.saas.park.api.vo.park.WarningDetailListVo;
import com.yuchen.saas.park.service.service.IWarningInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 吕海泉
 * @create 2020/12/7 16:53
 */
@RestController
@AllArgsConstructor
@RequestMapping("/info")
@Api(value = "报警信息管理接口", tags = "报警信息管理接口")
public class WarningInfoController {


    @Autowired
    private IWarningInfoService iWarningInfoService;


    /**
     * 报警信息 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "报警信息 详情", notes = "报警信息 详情")
    public R<PmgBatWarningInfo> detail(PmgBatWarningInfo pmgBatWarningInfo) {
        PmgBatWarningInfo detail = iWarningInfoService.getOne(Condition.getQueryWrapper(pmgBatWarningInfo));
        return R.data(detail);
    }


    /**
     * 报警信息 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "报警信息 分页", notes = "报警信息 分页")
    public R<IPage<PmgBatWarningInfo>> list(PmgBatWarningInfo pmgBatWarningInfo, Query query) {
        IPage<PmgBatWarningInfo> pages = iWarningInfoService.page(Condition.getPage(query),
                Condition.getQueryWrapper(pmgBatWarningInfo).orderByDesc("event_time"));
        return R.data(pages);
    }


    /**
     * 报警信息 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "报警信息 新增", notes = "报警信息 新增")
    public R save(@Valid @RequestBody PmgBatWarningInfo pmgBatWarningInfo) {
        return R.status(iWarningInfoService.save(pmgBatWarningInfo));
    }

    /**
     * 报警信息 修改
     */
    @PutMapping("/update")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "报警信息 修改", notes = "报警信息 修改")
    public R update(@Valid @RequestBody PmgBatWarningInfo pmgBatWarningInfo) {
        return R.status(iWarningInfoService.updateById(pmgBatWarningInfo));
    }

    /**
     * 报警信息 删除
     */
    @DeleteMapping("/remove")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "报警信息 删除", notes = "报警信息 删除")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(iWarningInfoService.deleteLogic(Func.toLongList(ids)));
    }
    /**
     * 报警信息 批量新增
     */
    @PostMapping("/saveList")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增", notes = "pmgBatWarningInfo")
    public R saveList(@Valid @RequestBody List<PmgBatWarningInfo> pmgBatWarningInfoList) {
        return R.status(iWarningInfoService.saveBatch(pmgBatWarningInfoList));
    }

    /**
     * 分页获取告警详情列表
     */
    @GetMapping("/getWarningDetailPage")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页获取告警详情列表", notes = "分页获取告警详情列表")
    public R<IPage<WarningDetailListVo>> getWarningDetailPage(@Valid WarningDetailListQueryDTO queryDTO) {
        if(queryDTO.getParkId()==null){
            queryDTO.setParkId(Long.parseLong(AuthUtil.getUser().getParkId()));
        }
        return R.data(iWarningInfoService.getWarningDetailList(queryDTO));
    }

    /**
     * 根据告警等级统计告警数量
     */
    @GetMapping("/countWarningDetailByLevel")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据告警等级统计告警数量", notes = "根据告警等级统计告警数量")
    public R<List<WarningDetailCountByLevelVo>> countWarningDetailByLevel(@Valid WarningDetailListQueryDTO queryDTO) {
        if(queryDTO.getParkId()==null){
            queryDTO.setParkId(Long.parseLong(AuthUtil.getUser().getParkId()));
        }
        return R.data(iWarningInfoService.countWarningDetailByLevel(queryDTO));
    }

    /**
     * 根据告警处理状态统计数量
     */
    @GetMapping("/countTodayWarningDetailByStatus")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据告警处理状态统计数量", notes = "根据告警处理状态统计数量")
    public R< List<WarningDetailCountByStatusVo>> countTodayWarningDetailByStatus(@RequestParam(required = false) Long parkId) {
        if(parkId==null){
            parkId= Long.parseLong(AuthUtil.getUser().getParkId());
        }
        return R.data(iWarningInfoService.countTodayWarningDetailByStatus(parkId));
    }

}

