package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.LocationUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TShouhuanAlarmList;
import com.ruoyi.system.domain.TSosSolve;
import com.ruoyi.system.domain.dto.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据大屏Controller
 * 
 * @author zhengjie
 * @date 2023-09-08
 */
@RestController
@RequestMapping("/system/display")
public class TDataDisplayController extends BaseController
{
    @Autowired
    private ITShouhuanAlarmListService tShouhuanAlarmListService;

    @Autowired
    private ITDeviceListService tDeviceListService;

    @Autowired
    private ITShouhuanInfoService itShouhuanInfoService;

    @Autowired
    private ITUserProfileService userProfileService;

    @Autowired
    private LocationUtils locationUtils;

    /**
     * 查询SOS预警处理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DataAlarmListDto dataAlarmListDto)
    {
        startPage();
        List<DataAlarmListDto> list = tShouhuanAlarmListService.selectTShouhuanAlarmList(dataAlarmListDto);
        List<AlarmListDto> alarmListDtoList = new ArrayList<>();
        for (DataAlarmListDto dto : list){
            AlarmListDto alarmListDto = new AlarmListDto();
            alarmListDto.setName(dto.getName());
            alarmListDto.setTelno(dto.getTel_one());
            alarmListDto.setLocation(getCounterLocation(dto.getGps_long() + "," + dto.getGps_lat()));
            alarmListDto.setAlarmMessage(tDeviceListService.checkMessage(dto.getAlarm_type()));
            alarmListDto.setCreatetime(dto.getCreatetime());
            alarmListDtoList.add(alarmListDto);
        }
        return getDataTable1(alarmListDtoList,tShouhuanAlarmListService.selectTShouhuanAlarmListCount());
    }

    /**
     * 总数
     */
    @GetMapping("/count")
    public AjaxResult count()
    {
        return success(tShouhuanAlarmListService.selectTShouhuanAlarmListCount());
    }

    /**
     * 总数
     */
    @GetMapping("/alarm")
    public AjaxResult alarm()
    {
        AlarmStatusDto dto = new AlarmStatusDto();
        dto.setAlarmSOSSolve(tShouhuanAlarmListService.selectTShouhuanAlarmSOSSolve());
        dto.setUn_alarmSOSSolve(tShouhuanAlarmListService.selectTShouhuanAlarmSOS() - tShouhuanAlarmListService.selectTShouhuanAlarmSOSSolve());
        dto.setAlarmRailSolve(tShouhuanAlarmListService.selectTShouhuanAlarmRailSolve());
        dto.setUn_alarmRailSolve(tShouhuanAlarmListService.selectTShouhuanAlarmRail() - tShouhuanAlarmListService.selectTShouhuanAlarmRailSolve());
        dto.setAlarmHeartSolve(tShouhuanAlarmListService.selectTShouhuanAlarmRailSolve());
        dto.setUn_alarmHeartSolve(tShouhuanAlarmListService.selectTShouhuanAlarmHeart() - tShouhuanAlarmListService.selectTShouhuanAlarmHeartSolve());
        dto.setAlarmFallSolve(tShouhuanAlarmListService.selectTShouhuanAlarmFallSolve());
        dto.setUn_alarmFallSolve(tShouhuanAlarmListService.selectTShouhuanAlarmFall() - tShouhuanAlarmListService.selectTShouhuanAlarmFallSolve());
        dto.setAlarmLowSolve(tShouhuanAlarmListService.selectTShouhuanAlarmLowSolve());
        dto.setUn_alarmLowSolve(tShouhuanAlarmListService.selectTShouhuanAlarmLow() - tShouhuanAlarmListService.selectTShouhuanAlarmLowSolve());
        dto.setAlarmStatusSolve(tShouhuanAlarmListService.selectTShouhuanAlarmStatus());
        dto.setUn_alarmStatusSolve(tShouhuanAlarmListService.selectTShouhuanAlarmStatusSolve());
        return success(dto);
    }

    /**
     * 总数
     */
    @GetMapping("/percent")
    public AjaxResult percent()
    {
        AlarmPercentDto dto = new AlarmPercentDto();
        dto.setAlarmSOS(checkPercent(tShouhuanAlarmListService.selectTShouhuanAlarmSOSSolve(),tShouhuanAlarmListService.selectTShouhuanAlarmSOS()));
        dto.setAlarmRail(checkPercent(tShouhuanAlarmListService.selectTShouhuanAlarmRailSolve(),tShouhuanAlarmListService.selectTShouhuanAlarmRail()));
        dto.setAlarmFall(checkPercent(tShouhuanAlarmListService.selectTShouhuanAlarmFallSolve(),tShouhuanAlarmListService.selectTShouhuanAlarmFall()));
        int statusSum = tShouhuanAlarmListService.selectTShouhuanAlarmStatus() + tShouhuanAlarmListService.selectTShouhuanAlarmStatusSolve();
        dto.setAlarmStauts(checkPercent(tShouhuanAlarmListService.selectTShouhuanAlarmStatus(),statusSum));
        int alarmAll = tShouhuanAlarmListService.selectTShouhuanAlarmSOSSolve() + tShouhuanAlarmListService.selectTShouhuanAlarmRailSolve() + tShouhuanAlarmListService.selectTShouhuanAlarmRailSolve() + tShouhuanAlarmListService.selectTShouhuanAlarmFallSolve() + tShouhuanAlarmListService.selectTShouhuanAlarmOtherSolve();
        dto.setAlarmAll(checkPercent(alarmAll,tShouhuanAlarmListService.selectTShouhuanAlarmListCount()));
        return success(dto);
    }

    /**
     * 总数
     */
    @GetMapping("/deviceView")
    public AjaxResult deviceView()
    {
        DeviceViewDto dto = new DeviceViewDto();
        dto.setDeviceCount(tShouhuanAlarmListService.selectTShouhuanAlarmStatus() + tShouhuanAlarmListService.selectTShouhuanAlarmStatusSolve());
        dto.setDeviceOnline(tShouhuanAlarmListService.selectTShouhuanAlarmStatus());
        dto.setUn_deviceOnline(tShouhuanAlarmListService.selectTShouhuanAlarmStatusSolve());
        dto.setAlarmCount(tShouhuanAlarmListService.selectTShouhuanAlarmListCount());
        return success(dto);
    }

    /**
     * 总数
     */
    @GetMapping("/deviceType")
    public AjaxResult deviceType()
    {
        Integer total = tDeviceListService.selectDeviceListCount();
        List<DeviceTypeDto> deviceListDto = tDeviceListService.selectDeviceListDto();
        for (DeviceTypeDto listDto : deviceListDto){
            listDto.setDeviceStr(checkDeviceType(listDto.getDevice_model()));
            listDto.setDevicePercent(checkPercent(listDto.getCount(),total));
        }
        AjaxResult ajax = new AjaxResult();
        ajax.put("list",deviceListDto);
        ajax.put("total", total);
        return AjaxResult.success("操作成功",ajax);
    }

    /**
     * 总数
     */
    @GetMapping("/userAgeAll")
    public AjaxResult userAgeAll()
    {
        Integer total = userProfileService.selectuserAgeCount();
        List<UserAgeDto> dtoList = userProfileService.selectuserAgeDto();
        AjaxResult ajax = new AjaxResult();
        ajax.put("list",dtoList);
        ajax.put("total", total);
        return AjaxResult.success("操作成功",ajax);
    }

    /**
     * 查询SOS预警处理列表
     */
    @GetMapping("/alarmList")
    public AjaxResult alarmList(DataAlarmListDto dataAlarmListDto)
    {
        List<DataAlarmListDto> list = tShouhuanAlarmListService.selectTShouhuanAlarmList(dataAlarmListDto);
        List<MessageAlarmListDto> messageAlarmListDtoList = new ArrayList<>();
        for (DataAlarmListDto dto : list){
            MessageAlarmListDto alarmListDto = new MessageAlarmListDto();
            alarmListDto.setName(dto.getName());
            alarmListDto.setAlarm_msg(tDeviceListService.checkMessage(dto.getAlarm_type()));
            alarmListDto.setTelno(dto.getTel_one());
            alarmListDto.setGps_long(dto.getGps_long());
            alarmListDto.setGps_lat(dto.getGps_lat());
            alarmListDto.setAddress(getCounterLocation(dto.getGps_long() + "," + dto.getGps_lat()));
            alarmListDto.setAlarm_type(dto.getAlarm_type());
            alarmListDto.setCreatetime(dto.getCreatetime());
            messageAlarmListDtoList.add(alarmListDto);
        }
        return success(messageAlarmListDtoList);
    }



    /**
     * 逆地理编码
     * @param location
     * @return
     */
    public String getCounterLocation(String location){

        Map<String, String> params = new HashMap<>();
        params.put("location", location);
        logger.info("获取逆地理编码,请求的参数为:{}", params);
        JSONObject map = locationUtils.getCounterLocation(params);
        LocationDto locationDto = com.alibaba.fastjson2.JSONObject.parseObject(String.valueOf(map), LocationDto.class);
        logger.info("获取逆地理编码,返回的请求结果为:{}", map);
        return locationDto.getFormatted_address();
    }

    /**
     * 计算百分比
     */
    public String checkPercent(int num1, int num2){
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float)num1/(float)num2*100);
        return result;
    }

    public String checkDeviceType(Integer device_model){
        switch (device_model){
            case 0:
                return "S8";
            case 1:
                return "X5";
            default:
                return "无效";
        }
    }
}
