package com.huimv.eartag.controller;

import com.huimv.eartag.service.IDeviceManageService;
import com.huimv.eartag.utils.DateUtil;
import com.huimv.eartag.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@CrossOrigin
@RestController
@RequestMapping(value = "/device")
@Slf4j
public class DeviceManageController {
//    @Autowired
//    private ICacheService cacheService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IDeviceManageService deviceService;

    /**
     * @Method      : getNowAllStatus
     * @Description : 读取最新设备和耳标总状态
     * @Params      : []
     * @Return      : void
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/5
     * @Time        : 17:03
     */
    @RequestMapping(value = "/getNowAllStatus",method = RequestMethod.GET)
    public Result getNowAllStatus(@RequestParam(value = "farmId",required = true) String farmId) throws ParseException {
        log.info("<getNowAllStatus>输入参数 farmId>>"+farmId);
        //
        return deviceService.getNowAllStatus(farmId);
    }

    /**
     * @Method      : listManyDaysAllStatus
     * @Description : 显示N天的总状态记录
     * @Params      : [farmId, days]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/6
     * @Time        : 22:18
     */
    @RequestMapping(value = "/listManyDaysAllStatus",method = RequestMethod.GET)
    public Result listManyDaysAllStatus(@RequestParam(value = "farmId",required = true) String farmId,@RequestParam(value = "days",required = true) Integer days) throws ParseException {
        log.info("<listManyDaysAllStatus>输入参数 farmId>>"+farmId);
        log.info("<listManyDaysAllStatus>输入参数 days>>"+days);
        if(days == null){
            //默认7天数据
            days = 7;
        }
        //
        return deviceService.listManyDaysAllStatus(farmId,days);
    }

    /**
     * @Method      : listOnlineDevice
     * @Description : 显示当天在线设备记录
     * @Params      : [farmId]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/7
     * @Time        : 11:09
     */
    @RequestMapping(value = "/listOnlineDevice",method = RequestMethod.GET)
    public Result listOnlineDevice(@RequestParam(value = "farmId",required = true) String farmId,
                                   @RequestParam(value = "date",required = true) String date) throws ParseException {
        log.info("<listOnlineDevice>输入参数 farmId>>"+farmId);
        log.info("<listOnlineDevice>输入参数 date>>"+date);
        //
        return deviceService.listOnlineDevice(farmId,date);
    }

    /**
     * @Method      : listOnlineDeviceByPager
     * @Description : 分页显示N天在线记录
     * @Params      : [farmId, deviceCode, alias, addDate, pageNo, pageSize]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/7
     * @Time        : 20:22
     */
    @RequestMapping(value = "/listOnlineDeviceByPager",method = RequestMethod.GET)
    public Result listOnlineDeviceByPager(@RequestParam(value = "farmId",required = true) String farmId,
                                          @RequestParam(value = "deviceCode",required = false) String deviceCode,
                                          @RequestParam(value = "alias",required = false) String alias,
                                          @RequestParam(value = "addDate",required = false) String addDate,
                                          @RequestParam(value = "pageNo",required = true) Integer pageNo,
                                          @RequestParam(value = "pageSize",required = true) Integer pageSize) throws ParseException {
        log.info("<listOnlineDevice>输入参数 farmId>>"+farmId);
        log.info("<listOnlineDevice>输入参数 deviceCode>>"+deviceCode);
        log.info("<listOnlineDevice>输入参数 alias>>"+alias);
        log.info("<listOnlineDevice>输入参数 addDate>>"+addDate);
        log.info("<listOnlineDevice>输入参数 pageNo>>"+pageNo);
        log.info("<listOnlineDevice>输入参数 pageSize>>"+pageSize);
        //
        return deviceService.listOnlineDevice(farmId,deviceCode,alias,addDate,pageNo,pageSize);
    }

    /**
     * @Method      : getDeviceRegisterByDeviceCode
     * @Description : 通过设备编码查询设备注册信息
     * @Params      : [deviceCode]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/7
     * @Time        : 14:01
     */
    @RequestMapping(value = "/getDeviceRegisterByDeviceCode",method = RequestMethod.GET)
    public Result getDeviceRegisterByDeviceCode(@RequestParam(value = "deviceCode",required = true) String deviceCode) throws ParseException {
        log.info("<getDeviceRegisterByDeviceCode>输入参数 deviceCode>>"+deviceCode);
        //
        return deviceService.getDeviceRegisterByDeviceCode(deviceCode);
    }

    /**
     * @Method      : listDeviceRegisterInPager
     * @Description : 列表展示设备注册（分页）
     * @Params      : [farmId, deviceCode, alias, startDate, endDate, activeStatus, liveStatus, pageNo, pageSize]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/9
     * @Time        : 22:34
     */
    @RequestMapping(value = "/listDeviceRegisterInPager",method = RequestMethod.GET)
    public Result listDeviceRegisterInPager(@RequestParam(value = "farmId",required = true) String farmId,
                                            @RequestParam(value = "deviceCode",required = false) String deviceCode,
                                            @RequestParam(value = "alias",required = false) String alias,
                                            @RequestParam(value = "startDate",required = false) String startDate,
                                            @RequestParam(value = "endDate",required = false) String endDate,
                                            @RequestParam(value = "activeStatus",required = false) Integer activeStatus,
                                            @RequestParam(value = "liveStatus",required = false) Integer liveStatus,
                                            @RequestParam(value = "pageNo",required = true) Integer pageNo,
                                            @RequestParam(value = "pageSize",required = true) Integer pageSize) throws ParseException {
        log.info("<listDeviceRegisterInPager>输入参数 farmId>>"+farmId);
        log.info("<listDeviceRegisterInPager>输入参数 deviceCode>>"+deviceCode);
        log.info("<listDeviceRegisterInPager>输入参数 alias>>"+alias);
        log.info("<listDeviceRegisterInPager>输入参数 startDate>>"+startDate);
        log.info("<listDeviceRegisterInPager>输入参数 endDate>>"+endDate);
        log.info("<listDeviceRegisterInPager>输入参数 activeStatus>>"+activeStatus);
        log.info("<listDeviceRegisterInPager>输入参数 liveStatus>>"+liveStatus);
        log.info("<listDeviceRegisterInPager>输入参数 pageNo>>"+pageNo);
        log.info("<listDeviceRegisterInPager>输入参数 pageSize>>"+pageSize);
        //
        return deviceService.listDeviceRegisterInPager(farmId,deviceCode,alias,startDate,endDate,activeStatus,liveStatus,pageNo,pageSize);
    }

    /**
     * @Method      : listManyDaysOnlineByDeviceCode
     * @Description : F1接口-查询单个设备N天在线图表
     * @Params      : [deviceCode]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/7
     * @Time        : 14:07
     */
    @RequestMapping(value = "/listDeviceOnlineCountByDeviceCode",method = RequestMethod.GET)
    public Result listDeviceOnlineCountByDeviceCode(@RequestParam(value = "deviceCode",required = true) String deviceCode,
                                                    @RequestParam(value = "pastDays",required = false) Integer pastDays) throws ParseException {
        log.info("<listManyDaysOnlineByDeviceCode>输入参数 deviceCode>>"+deviceCode);
        log.info("<listManyDaysOnlineByDeviceCode>输入参数 pastDays>>"+pastDays);
        if(pastDays == null){
            pastDays = 7;
        }
        //查询单个设备N天在线记录
        return deviceService.listDeviceOnlineCountByDeviceCode(deviceCode,pastDays);
    }

    /**
     * @Method      : listDeviceOnlineCountByDeviceCodeInPager
     * @Description : F2接口-查询单个设备在线记录
     * @Params      : [deviceCode, startDate, endDate, pageNo, pageSize]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/18
     * @Time        : 14:31
     */
    @RequestMapping(value = "/listDeviceOnlineCountByDeviceCodeInPager",method = RequestMethod.GET)
    public Result listDeviceOnlineCountByDeviceCodeInPager(@RequestParam(value = "deviceCode",required = true) String deviceCode,
                                                           @RequestParam(value = "startDate",required = false) String startDate,
                                                           @RequestParam(value = "endDate",required = false) String endDate,
                                                           @RequestParam(value = "pageNo",required = true) Integer pageNo,
                                                           @RequestParam(value = "pageSize",required = true) Integer pageSize ) throws ParseException {
        log.info("<listManyDaysOnlineByDeviceCode>输入参数 deviceCode>>"+deviceCode);
        log.info("<listManyDaysOnlineByDeviceCode>输入参数 startDate>>"+startDate);
        log.info("<listManyDaysOnlineByDeviceCode>输入参数 endDate>>"+endDate);
        log.info("<listManyDaysOnlineByDeviceCode>输入参数 pageNo>>"+pageNo);
        log.info("<listManyDaysOnlineByDeviceCode>输入参数 pageSize>>"+pageSize);
        DateUtil dateUtil = new DateUtil();
        if(startDate == null || startDate.trim().length() == 0){
            startDate = dateUtil.getTodayDateText();
        }
        //endDate
        if(endDate == null || endDate.trim().length() == 0){
            endDate = dateUtil.getTodayDateText();
        }
        //查询单个设备N天在线记录(,endDate)
        return deviceService.listDeviceOnlineCountByDeviceCodeInPager(deviceCode,startDate,endDate,pageNo,pageSize);
    }

    /**
     * @Method      : listDeviceOnlineStatusByDeviceCode
     * @Description : 显示设备在线状态
     * @Params      : [deviceCode, pastDays]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/7
     * @Time        : 15:44
     */
    @RequestMapping(value = "/listDeviceOnlineStatusByDeviceCode",method = RequestMethod.GET)
    public Result listDeviceOnlineStatusByDeviceCode(@RequestParam(value = "deviceCode",required = true) String deviceCode,@RequestParam(value = "pastDays",required = true) Integer pastDays) throws ParseException {
        log.info("<listManyDaysOnlineByDeviceCode>输入参数 deviceCode>>"+deviceCode);
        log.info("<listManyDaysOnlineByDeviceCode>输入参数 pastDays>>"+pastDays);
        if(pastDays == null){
            pastDays = 7;
        }
        //查询单个设备N天在线记录
        return deviceService.listDeviceOnlineStatusByDeviceCode(deviceCode,pastDays);
    }

    /**
     * @Method      : listEartagOnlineCountByDeviceCode
     * @Description : 显示设备关联耳标统计
     * @Params      : [deviceCode]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/7
     * @Time        : 18:11
     */
    @RequestMapping(value = "/listDeviceEartagOnlineCountByDeviceCode",method = RequestMethod.GET)
    public Result listDeviceEartagOnlineCountByDeviceCode(@RequestParam(value = "deviceCode",required = true) String deviceCode) throws ParseException {
        log.info("<listEartagOnlineCountByDeviceCode>输入参数 deviceCode>>"+deviceCode);
        //查询设备关联的在线耳标统计
        return deviceService.listDeviceEartagOnlineCountByDeviceCode(deviceCode);
    }

    /**
     * @Method      : listDeviceEartagOnlineCountByDeviceCodeInPager
     * @Description : 显示设备关联耳标统计（带分页）
     * @Params      : [deviceCode, earmark, addDate, pageNo, pageSize]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/9
     * @Time        : 20:06
     */
    @RequestMapping(value = "/listDeviceEartagOnlineCountByDeviceCodeInPager",method = RequestMethod.GET)
    public Result listDeviceEartagOnlineCountByDeviceCodeInPager(@RequestParam(value = "deviceCode",required = true) String deviceCode,
                                                                 @RequestParam(value = "earmark",required = false) String earmark,
                                                                 @RequestParam(value = "addDate",required = false) String addDate,
                                                                 @RequestParam(value = "pageNo",required = true) Integer pageNo,
                                                                 @RequestParam(value = "pageSize",required = true) Integer pageSize) throws ParseException {
        log.info("<listDeviceEartagOnlineCountByDeviceCodeInPager>输入参数 deviceCode>>"+deviceCode);
        log.info("<listDeviceEartagOnlineCountByDeviceCodeInPager>输入参数 earmark>>"+earmark);
        log.info("<listDeviceEartagOnlineCountByDeviceCodeInPager>输入参数 addDate>>"+addDate);
        log.info("<listDeviceEartagOnlineCountByDeviceCodeInPager>输入参数 pageNo>>"+pageNo);
        log.info("<listDeviceEartagOnlineCountByDeviceCodeInPager>输入参数 pageSize>>"+pageSize);
        //
        if(addDate == null){
            addDate  =  new DateUtil().getTodayDateText();
        }
        //查询设备关联的在线耳标统计
        return deviceService.listDeviceEartagOnlineCountByDeviceCodeInPager(deviceCode,earmark,addDate,pageNo,pageSize);
    }

    /**
     * @Method      : listDeviceEartagOnlineStatusByDeviceCodeAndDate
     * @Description : 查询和设备相关的所有耳标状态
     * @Params      : [deviceCode, earmark, addDate]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/29
     * @Time        : 10:55
     */
    @RequestMapping(value = "/listDeviceEartagOnlineStatusByDeviceCodeAndDate",method = RequestMethod.GET)
    public Result listDeviceEartagOnlineStatusByDeviceCodeAndDate(@RequestParam(value = "deviceCode",required = true) String deviceCode,
                                                                  @RequestParam(value = "earmark",required = false) String earmark,
                                                                  @RequestParam(value = "addDate",required = false) String addDate) throws ParseException {
        log.info("<listDeviceEartagOnlineStatusByDeviceCodeAndDate>输入参数 deviceCode>>"+deviceCode);
        log.info("<listDeviceEartagOnlineStatusByDeviceCodeAndDate>输入参数 earmark>>"+earmark);
        log.info("<listDeviceEartagOnlineStatusByDeviceCodeAndDate>输入参数 addDate>>"+addDate);
        //
        if(addDate == null || addDate.trim().length() == 0){
            addDate  =  new DateUtil().getTodayDateText();
        }
        //查询设备关联的在线耳标统计
        return deviceService.listDeviceEartagOnlineStatusByDeviceCodeAndDate(deviceCode,earmark,addDate);
    }

    /**
     * @Method      : listDeviceEnvtempByDeviceCode
     * @Description : 显示设备温度流水
     * @Params      : [deviceCode]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/8
     * @Time        : 21:11
     */
    @RequestMapping(value = "/listDeviceEnvtempByDeviceCode",method = RequestMethod.GET)
    public Result listDeviceEnvtempByDeviceCode(@RequestParam(value = "deviceCode",required = true) String deviceCode) throws ParseException {
        log.info("<listDeviceEnvtempByDeviceCode>输入参数 deviceCode>>"+deviceCode);
        //查询设备环境温度
        return deviceService.listDeviceEnvtempByDeviceCode(deviceCode);
    }

    /**
     * @Method      : listDeviceEnvtempByDeviceCodeInPager
     * @Description : 显示设备温度流水
     * @Params      : [deviceCode]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/8
     * @Time        : 21:11
     */
    @RequestMapping(value = "/listDeviceEnvtempByDeviceCodeInPager",method = RequestMethod.GET)
    public Result listDeviceEnvtempByDeviceCodeInPager(@RequestParam(value = "deviceCode",required = true) String deviceCode,
                                                       @RequestParam(value = "addDate",required = false) String addDate,
                                                       @RequestParam(value = "pageNo",required = true) Integer pageNo,
                                                       @RequestParam(value = "pageSize",required = true) Integer pageSize) throws ParseException {
        log.info("<listDeviceEnvtempByDeviceCodeInPager>输入参数 deviceCode>>"+deviceCode);
        log.info("<listDeviceEnvtempByDeviceCodeInPager>输入参数 addDate>>"+addDate);
        log.info("<listDeviceEnvtempByDeviceCodeInPager>输入参数 pageNo>>"+pageNo);
        log.info("<listDeviceEnvtempByDeviceCodeInPager>输入参数 pageSize>>"+pageSize);
        //
        if(addDate == null || addDate.trim().length() == 0){
            addDate  =  new DateUtil().getTodayDateText();
        }
        //查询设备环境温度
        return deviceService.listDeviceEnvtempByDeviceCodeInPager(deviceCode,addDate,pageNo,pageSize);
    }

    /**
     * @Method      : listHeartbeatByDeviceCodeAndDateInPager
     * @Description : 显示设备心跳数据（带分页）
     * @Params      : [deviceCode]
     * @Return      : com.huimv.eartag2.common.utils.Result
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/3/9
     * @Time        : 20:35
     */
    @RequestMapping(value = "/listHeartbeatByDeviceCodeAndDateInPager",method = RequestMethod.GET)
    public Result listHeartbeatByDeviceCodeAndDateInPager(@RequestParam(value = "deviceCode",required = true) String deviceCode,
                                                          @RequestParam(value = "addDate",required = false) String addDate,
                                                          @RequestParam(value = "pageNo",required = true) Integer pageNo,
                                                          @RequestParam(value = "pageSize",required = true) Integer pageSize) throws ParseException {
        log.info("<listHeartbeatByDeviceCodeAndDateInPager>输入参数 deviceCode>>"+deviceCode);
        log.info("<listHeartbeatByDeviceCodeAndDateInPager>输入参数 addDate>>"+addDate);
        log.info("<listHeartbeatByDeviceCodeAndDateInPager>输入参数 pageNo>>"+pageNo);
        log.info("<listHeartbeatByDeviceCodeAndDateInPager>输入参数 pageSize>>"+pageSize);
        //
        if(addDate == null || addDate.trim().length() == 0){
            addDate  =  new DateUtil().getTodayDateText();
        }
        //显示设备心跳
        return deviceService.listHeartbeatByDeviceCodeAndDateInPager(deviceCode,addDate,pageNo,pageSize);
    }
}
