package com.bonus.material.cityScreen.controller;

import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.cityScreen.DTO.CityScreenDTO;
import com.bonus.material.cityScreen.VO.*;
import com.bonus.material.cityScreen.mapper.CityScreenMapper;
import com.bonus.material.cityScreen.service.CityScreenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：tqzhang
 * @Date：2025/9/14 - 15:46
 */

@Api(value = "地市大屏", tags = {"地市大屏"})
@RestController
@RequestMapping("/city_screen")
public class CityScreenController extends BaseController {

    @Resource
    private CityScreenService cityScreenService;
    @Resource
    private CityScreenMapper cityScreenMapper;

    @ApiOperation("基础头部数据")
    @GetMapping("/getBasicHead")
    public AjaxResult getBasicHead(CityScreenDTO dto) {
        BasicTitleVO titleVO = cityScreenService.getBasicHead(dto);
        return AjaxResult.success(titleVO);
    }

    @ApiOperation("装备数量价值")
    @GetMapping("/equipmentQuantityValue")
    public AjaxResult equipmentQuantityValue(CityScreenDTO dto) {
        List<EquipmentQuantityVO> list = cityScreenService.equipmentQuantityValue(dto);
        return AjaxResult.success(list);
    }

    @ApiOperation("装备状态")
    @GetMapping("/equipmentStatus")
    public AjaxResult equipmentStatus(CityScreenDTO dto) {
        List<EchartsVO> list = cityScreenService.equipmentStatus(dto);
        return AjaxResult.success(list);
    }

    @ApiOperation("维修报警")
    @GetMapping("/maintenanceAlarm")
    public AjaxResult maintenanceAlarm(CityScreenDTO dto) {
        startPage();
        List<AlarmVO> list = cityScreenService.maintenanceAlarm(dto);
        DateVO<AlarmVO> dateVO = new DateVO<>();
        dateVO.setList(list);
        try {
            int total = cityScreenMapper.countMaintenanceAlarm(dto);
            dateVO.setTotal(total);
        } catch (Exception e) {
            throw new ServiceException("获取维修报警数据失败");
        }
        return AjaxResult.success(dateVO);
    }

    @ApiOperation("退役报警")
    @GetMapping("/retirementAlarm")
    public AjaxResult retirementAlarm(CityScreenDTO dto) {
        startPage();
        List<AlarmVO> list = cityScreenService.retirementAlarm(dto);
        DateVO<AlarmVO> dateVO = new DateVO<>();
        dateVO.setList(list);
        try {
            int total = cityScreenMapper.countRetirementAlarm(dto);
            dateVO.setTotal(total);
        } catch (Exception e) {
            throw new ServiceException("获取退役报警数据失败");
        }
        return AjaxResult.success(dateVO);
    }

    @ApiOperation("装备在用率")
    @GetMapping("/equipmentInServiceRate")
    public AjaxResult equipmentInServiceRate(CityScreenDTO dto) {
        List<EchartsVO> list = cityScreenService.equipmentInServiceRate(dto);
        return AjaxResult.success(list);
    }

    @ApiOperation("装备周转率")
    @GetMapping("/equipmentTurnoverRate")
    public AjaxResult equipmentTurnoverRate(CityScreenDTO dto) {
        startPage();
        List<EquipmentTurnoverRateVO> list = cityScreenService.equipmentTurnoverRate(dto);
        DateVO<EquipmentTurnoverRateVO> dateVO = new DateVO<>();
        dateVO.setList(list);
        try {
            int total = cityScreenMapper.countEquipmentTurnoverRate(dto);
            dateVO.setTotal(list.size());
        } catch (Exception e) {
            throw new ServiceException("获取装备周转率数据失败");
        }
        return AjaxResult.success(dateVO);
    }

    @ApiOperation("工程在用装备情况")
    @GetMapping("/equipmentInUseInTheProject")
    public AjaxResult equipmentInUseInTheProject(CityScreenDTO dto) {
        startPage();
        List<EquipmentUseVO> list = cityScreenService.equipmentInUseInTheProject(dto);
        DateVO<EquipmentUseVO> dateVO = new DateVO<>();
        dateVO.setList(list);
        try {
            int proNum = cityScreenMapper.countProNum(dto);
            int useNum = cityScreenMapper.countUseNum(dto);
            dateVO.setTotal(proNum);
            dateVO.setUseNum(useNum);
        } catch (Exception e) {
            throw new ServiceException("获取工程在用装备情况数据失败");
        }
        return AjaxResult.success(dateVO);
    }

    @ApiOperation("地图数据")
    @GetMapping("/mapData")
    public AjaxResult mapData(CityScreenDTO dto) {
        // 查询工程列表数据
        List<ProVO> list = cityScreenService.proList(dto);
        MapVO mapVO = new MapVO();
        mapVO.setProList(list);
        try {
            //仓库装备数
            int deviceNum = cityScreenMapper.countDeviceNum(dto);
            mapVO.setWarehouseEquipmentNum(deviceNum);
            //市外装备数
            int outDeviceNum = cityScreenMapper.countOutDeviceNum(dto);
            mapVO.setOutCityEquipmentNum(outDeviceNum);
        } catch (Exception e) {
            throw new ServiceException("获取地图数据失败");
        }
        return AjaxResult.success(mapVO);
    }
}
