package com.unis.detectionnum.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.common.utils.ResponseWrapper;
import com.unis.detectionnum.common.vo.DeviceCameraParamVO;
import com.unis.detectionnum.common.vo.DeviceInfoQueryVO;
import com.unis.detectionnum.common.vo.DeviceInfoVO;
import com.unis.detectionnum.common.vo.DeviceParamVO;
import com.unis.detectionnum.common.vo.DeviceProbeParamVO;
import com.unis.detectionnum.server.service.DeviceParamService;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description 设备参数controller
 * @Package: com.unis.detectionnum.server.controller
 * @Author: LiuFeng
 * @Date: Created in 2019/8/14
 */

@RestController
@Slf4j
@Api(value = "参数管理-硬件参数管理", tags = "参数管理-硬件参数管理")
public class DeviceParamController {

    @Autowired
    private DeviceParamService deviceParamService;
    
    @ApiOperation(value = "读取设备参数接口")
    @PostMapping(value = "/device/param/read")
    public ResponseWrapper readDeviceParam(HttpServletRequest request,
    		@ModelAttribute DeviceInfoQueryVO deviceInfoQueryVO){
    	
    	try {
    		int result  = deviceParamService.readDeviceParam(deviceInfoQueryVO);
    		if(result > 0) {
    			return ResponseWrapper.success(HttpStatus.OK.value(), "设备参数读取成功",null);
    		}

		} catch (Exception e) {
			log.info("DeviceParamController.readDeviceParam",e);
		}
    	return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),"设备参数写入失败",null);
    }
    
    @ApiOperation(value = "设置设备参数接口")
    @RequestMapping(value = "/device/param/set",method=RequestMethod.POST)
    @ResponseBody
    public ResponseWrapper writeDeviceParam(HttpServletRequest request,
    		@ModelAttribute DeviceParamVO DeviceParamVO,
    		@ModelAttribute DeviceInfoQueryVO deviceInfoQueryVO){
    	
    	try {
    		deviceParamService.writeDeviceParam(DeviceParamVO,deviceInfoQueryVO);
    		
    		return ResponseWrapper.success(HttpStatus.OK.value(), "设备参数写入成功",null);
    		
		} catch (Exception e) {
			log.info("DeviceParamController.writeDeviceParam",e);
			
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
    
    /**
	 * 根据条件分页查询硬件设备信息
	 * @param taskQueryVO
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value = "参数管理->硬件参数管理->根据条件【分页查询】",notes = "参数：")
	@PostMapping(value = "/paramManage/devicePageList/{pageNum}/{pageSize}")
	public ResponseWrapper getDevicePageList(
			@RequestBody DeviceInfoQueryVO deviceInfoQueryVO,
			@PathVariable(value = "pageNum") Integer pageNum,
			@PathVariable(value = "pageSize") Integer pageSize){
		try {
			if (deviceInfoQueryVO.getSbName() != null && deviceInfoQueryVO.getSbName().length() != 0){
				String SbName = deviceInfoQueryVO.getSbName();
				deviceInfoQueryVO.setSbName("%"+SbName+"%");
			}
			PageInfo<DeviceInfoVO> pageInfos = deviceParamService.getDevicePageList(deviceInfoQueryVO,pageNum,pageSize);
			return ResponseWrapper.success(HttpStatus.OK.value(), "获取信息列表成功", pageInfos);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),"获取信息列表失败",null);
		}
	}
	
	/**
	 * 根据设备code、设备类型查询硬件信息
	 * @param sbCode
	 * @param sblx
	 * @return
	 */
	@ApiOperation(value = "参数管理->硬件参数管理->根据设备code、设备类型【查询】",notes = "参数：设备code、设备类型")
	@GetMapping(value = "/paramManage/device/{sbCode}/{sblx}")
	public ResponseWrapper getDeviceParam(@PathVariable(value = "sbCode") String sbCode,
			@PathVariable(value = "sblx") Integer sblx){
		try {
			if(sblx == 0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), "获取信息成功", deviceParamService.getDeviceProbeParam(sbCode));
			}else if(sblx == 1) {
				return ResponseWrapper.success(HttpStatus.OK.value(), "获取信息成功", deviceParamService.getDeviceCameraParam(sbCode));
			}

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),"获取信息失败",null);
	}
	 
    @ApiOperation(value = "参数管理->硬件参数管理->激光探头【新增】")
//    @PostMapping(value = "/paramManage/device/probe")
	@RequestMapping(value = "/paramManage/device/probeSave", method = RequestMethod.POST)
    public ResponseWrapper probeSave(@RequestBody DeviceProbeParamVO deviceProbeVO){
    	
    	try {
    		deviceParamService.saveProbeParam(deviceProbeVO);
    		
    		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 = "参数管理->硬件参数管理->激光探头【修改】")
//    @PutMapping(value = "/paramManage/device/probeUpdate")
	@RequestMapping(value = "/paramManage/device/probeUpdate", method = RequestMethod.POST)
    public ResponseWrapper probeUpdate(@RequestBody DeviceProbeParamVO deviceProbeVO){
    	
    	try {
    		deviceParamService.updateProbeParam(deviceProbeVO);
    		
    		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(),"服务错误，请联系管理员");
    }
    
    /**
	 * 根据设备code、设备类型删除相关硬件参数信息
	 * @param sbCode
	 * @param sblx
	 * @return
	 */
	@ApiOperation(value = "参数管理->硬件参数管理->根据设备code、设备类型【删除】",notes = "参数：设备code、设备类型")
	@DeleteMapping(value = "/paramManage/device/{sbCode}/{sblx}")
	public ResponseWrapper deleteDeviceParam(@PathVariable(value = "sbCode") String sbCode,
			@PathVariable(value = "sblx") Integer sblx){
		try {
			if(sblx == 0) {
				deviceParamService.deleteProbeParam(sbCode);
				return ResponseWrapper.success(HttpStatus.OK.value(), "删除成功", null);
			}else if(sblx == 1) {
				deviceParamService.deleteCameraParam(sbCode);
				return ResponseWrapper.success(HttpStatus.OK.value(), "获取信息成功", null);
			}

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),"获取信息失败",null);
	}
	
	@ApiOperation(value = "参数管理->硬件参数管理->摄像头【新增】")
    @PostMapping(value = "/paramManage/device/camera")
    public ResponseWrapper saveCameraParam(@RequestBody DeviceCameraParamVO deviceCameraVO){
    	
    	try {
    		deviceParamService.saveCameraParam(deviceCameraVO);
    		
    		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 = "参数管理->硬件参数管理->摄像头【修改】")
    @PutMapping(value = "/paramManage/device/camera")
    public ResponseWrapper updateCameraParam(@RequestBody DeviceCameraParamVO deviceCameraVO){
    	
    	try {
    		deviceParamService.updateCameraParam(deviceCameraVO);
    		
    		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 = "参数管理->硬件参数管理->新增时获取设备最新【编号】")
    @GetMapping(value = "/paramManage/device/number/{cfbh}")
    public ResponseWrapper getDeviceNo(@PathVariable("cfbh")String cfbh){
    	
    	try {
    		
			String deviceNo = deviceParamService.getDeviceNo(cfbh);
			return ResponseWrapper.success(HttpStatus.OK.value(), "获取成功", deviceNo);
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
	
}
