package com.fastbee.data.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fastbee.common.core.controller.BaseController;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.iot.domain.DeviceReportInfo;
import com.fastbee.iot.service.IDeviceReportInfoService;
import com.fastbee.common.utils.poi.ExcelUtil;
import com.fastbee.common.core.page.TableDataInfo;

/**
 * 设备上电审核前上报的基础信息Controller
 *
 * @author kerwincui
 * &#064;date  2024-12-05
 */
@RestController
@RequestMapping("/iot/device/report/info")
@Api(tags = "设备上电审核前上报的基础信息")
public class DeviceReportInfoController extends BaseController
{
    @Autowired
    private IDeviceReportInfoService deviceReportInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

/**
 * 查询设备上电审核前上报的基础信息列表
 */
//    @PreAuthorize("@ss.hasPermi('iot:info:list')")
    @GetMapping("/list")
    @ApiOperation("查询设备上电审核前上报的基础信息列表")
    public TableDataInfo list(DeviceReportInfo deviceReportInfo)
    {
        startPage();
        List<DeviceReportInfo> list = deviceReportInfoService.selectDeviceReportInfoList(deviceReportInfo);
        System.err.println("查询条件："+deviceReportInfo.getOnLine());
        System.err.println("设备总长度："+list.size());
        //查询设备在线状态
        list.forEach(d->{
            if(d.getType()==1){
                Boolean hassed = stringRedisTemplate.hasKey("neixiang_device_online_status:" + 147 + ":" + d.getSerialNumber());
                if(hassed){
                    d.setOnLine(1);
                }else{
                    d.setOnLine(0);
                }
            } else if ( d.getType()==2) {
                //展连流量计设备在线状态判断
                d.setType(0);
            }
        });
        // //处理在线/离线筛选
        // if(deviceReportInfo.getOnLine()!=null){
        //     //根据online状态的值过滤集合
        //     list= list.stream().filter(d -> Objects.equals(d.getOnLine(), deviceReportInfo.getOnLine()))
        //             .collect(Collectors.toList());
        // }
        System.err.println("过滤后总长度："+list.size());
        return getDataTable(list);
    }
    //统计是设备相关信息
    @GetMapping("/list/count")
    @ApiOperation("统计是设备相关信息")
    public AjaxResult listCount(DeviceReportInfo deviceReportInfo)
        {
            List<DeviceReportInfo> list = deviceReportInfoService.selectDeviceReportInfoList(deviceReportInfo);
            AtomicInteger onLineTotal= new AtomicInteger();
            list.forEach(d->{
                if(d.getType()==1){
                    if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("neixiang_device_online_status:" + 147 + ":" + d.getSerialNumber())))
                    {
                        onLineTotal.getAndIncrement();
                    }
                } else if ( d.getType()==2) {
                    if(d.getOnLine()==1){
                        onLineTotal.getAndIncrement();
                    }
                }
            });
            Map<String,Object> resp=new HashMap<>();
            resp.put("allTotal",list.size());
            resp.put("onLineTotal",onLineTotal.get());
            return AjaxResult.success((resp));
        }

    /**
     * 导出设备上电审核前上报的基础信息列表
     */
    @ApiOperation("导出设备上电审核前上报的基础信息列表")
    // @PreAuthorize("@ss.hasPermi('iot:info:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceReportInfo deviceReportInfo)
    {
        List<DeviceReportInfo> list = deviceReportInfoService.selectDeviceReportInfoList(deviceReportInfo);
        ExcelUtil<DeviceReportInfo> util = new ExcelUtil<DeviceReportInfo>(DeviceReportInfo.class);
        util.exportExcel(response, list, "设备上电审核前上报的基础信息数据");
    }

    /**
     * 获取设备上电审核前上报的基础信息详细信息
     */
    // @PreAuthorize("@ss.hasPermi('iot:info:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取设备上电审核前上报的基础信息详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deviceReportInfoService.selectDeviceReportInfoById(id));
    }

    /**
     * 新增设备上电审核前上报的基础信息
     */
    // @PreAuthorize("@ss.hasPermi('iot:info:add')")
    @PostMapping
    @ApiOperation("新增设备上电审核前上报的基础信息")
    public AjaxResult add(@RequestBody DeviceReportInfo deviceReportInfo)
    {
        return toAjax(deviceReportInfoService.insertDeviceReportInfo(deviceReportInfo));
    }

    /**
     * 修改设备上电审核前上报的基础信息
     */
    // @PreAuthorize("@ss.hasPermi('iot:info:edit')")
    @PutMapping
    @ApiOperation("修改设备上电审核前上报的基础信息")
    public AjaxResult edit(@RequestBody DeviceReportInfo deviceReportInfo)
    {
        return toAjax(deviceReportInfoService.updateDeviceReportInfo(deviceReportInfo));
    }

    /**
     * 删除设备上电审核前上报的基础信息
     */
    // @PreAuthorize("@ss.hasPermi('iot:info:remove')")
    @DeleteMapping("/{ids}")
    @ApiOperation("删除设备上电审核前上报的基础信息")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceReportInfoService.deleteDeviceReportInfoByIds(ids));
    }

    /**
     * 批量更新状态为已打印
     */
    // @PreAuthorize("@ss.hasPermi('iot:info:edit')")
    @PutMapping("/updateStatus")
    @ApiOperation("批量更新状态")
    public AjaxResult updateStatus(@RequestBody List<DeviceReportInfo> deviceReportInfos){
        return toAjax(deviceReportInfoService.updateDeviceReportStatus(deviceReportInfos));
    }
}
