/**   
 * @Title: AlarmParamController.java 
 * @Package com.unis.detectionnum.server.controller 
 * @Description: 参数管理-告警参数信息维护
 * @author LiuFeng  
 * @date 2019年10月16日 下午4:55:23 
 * @version V1.0   
 */
package com.unis.detectionnum.server.controller;

import java.util.List;

import com.unis.detectionnum.common.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unis.common.utils.ResponseWrapper;
import com.unis.detectionnum.server.service.DeviceAlarmService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/** 
 * @ClassName: AlarmParamController 
 * @Description: 报警记录管理 
 * @author LiuFeng 
 * @date 2019年10月16日 下午4:55:23  
 */
@RestController
@Slf4j
@Api(value = "报警记录管理", tags = "报警记录管理")
public class DeviceAlarmController {

	@Autowired
	private DeviceAlarmService deviceAlarmService;
	
	@ApiOperation(value = "报警记录管理->直属库首页【库点统计】")
	@RequestMapping(value = "/alarmManage/statistic/storehouse/{userCode}", method = RequestMethod.GET)
	@ResponseBody
    public ResponseWrapper storehouseStatistic(@PathVariable("userCode") String userCode){
    	
    	try {
   		
        	return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), deviceAlarmService.storehouseStatistic(userCode));
    		 		
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

    @ApiOperation(value = "报警记录管理->触发报警", notes = "参数：仓房编号")
    @PostMapping(value = "/alarmManage/record/triggerCallAlarm/{cfbh}")
    public ResponseWrapper triggerCallAlarm(@PathVariable("cfbh")String cfbh){
        try {
            deviceAlarmService.triggerCallAlarm(cfbh);

            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");

    }

    @ApiOperation(value = "报警记录管理->触发风扇", notes = "参数：仓房编号")
    @PostMapping(value = "/alarmManage/record/startFan/{cfbh}")
    public ResponseWrapper startFan(@PathVariable("cfbh")String cfbh){
        try {
            deviceAlarmService.triggerCallAlarm(cfbh);

            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "风扇启动成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");

    }


	@ApiOperation(value = "报警记录管理->报警记录条件【分页查询】")
//    @PostMapping(value = "/alarmManage/record/{pageNum}/{pageSize}")
	@RequestMapping(value = "/alarmManage/recordPage", method = RequestMethod.POST)
    public ResponseWrapper alarmRecordPageList(Integer pageNum,Integer pageSize,
    		@RequestBody DeviceAlarmQueryVO deviceAlarmQueryVO){
    	
    	try {
			if (deviceAlarmQueryVO.getCfmc() != null && deviceAlarmQueryVO.getCfmc().length() != 0){
				String Cfmc = deviceAlarmQueryVO.getCfmc();
				deviceAlarmQueryVO.setCfmc("%"+Cfmc+"%");
			}
			if (deviceAlarmQueryVO.getZskmc() != null && deviceAlarmQueryVO.getZskmc().length() != 0){
				String Zskmc = deviceAlarmQueryVO.getZskmc();
				deviceAlarmQueryVO.setZskmc("%"+Zskmc+"%");
			}
    		PageHelper.startPage(pageNum, pageSize);
    		List<DeviceAlarmRecodVO> list = deviceAlarmService.alarmRecordList(deviceAlarmQueryVO);
    		PageInfo<DeviceAlarmRecodVO> recordPageList = new PageInfo<>(list);
        	return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), recordPageList);	
    		 		
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	@ApiOperation(value = "报警记录管理->报警记录【新增】")
    @PostMapping(value = "/alarmManage/record")
    public ResponseWrapper saveAlarmRecord(@RequestBody DeviceAlarmVO deviceAlarmVO){
    	
    	try {
   		
    		deviceAlarmService.saveDeviceAlarm(deviceAlarmVO);
        	return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);	
    		 		
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
	
}
