package com.unis.base.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.EquipmentRecordsQueryVO;
import com.unis.base.common.vo.EquipmentRecordsVO;
import com.unis.base.server.service.EquipmentRecordsService;
import com.unis.common.utils.ResponseWrapper;
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 java.util.List;

/**
 * @author WangZL
 * @version 1.0
 * @Title EquipmentRecordsController
 * @ProjectName 设备记录 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "设备记录", tags = "设备记录")
@Slf4j
public class EquipmentRecordsController {

	@Autowired
	private EquipmentRecordsService equipmentRecordsService;

	/**
	 * @author WangZL
	 * @Description 获取设备列表
	 * @param 设备编号，设备名称，库点编号，设备类型，设备状态, 机构ID
	 * @return 设备记录列表
	 */
	@ApiOperation(value = "设备记录-查询列表", notes = "参数：设备编号，设备名称，库点编号，库点名称，设备类型，设备状态, 机构ID")
	@PostMapping(value = "/equipmentRecordsList")
    public ResponseWrapper getEquipmentRecordsList(@RequestBody EquipmentRecordsQueryVO equipmentRecordsQueryVO){
        try {
        	List<EquipmentRecordsVO> equipmentRecordsList = equipmentRecordsService.getEquipmentRecordsList(equipmentRecordsQueryVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), equipmentRecordsList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author WangZL
	 * @Description 获取设备记录列表
	 * @param 设备编号，设备名称，库点编号，设备类型，设备状态, 机构ID
	 * @return 设备记录列表
	 */
	@ApiOperation(value = "设备记录-分页查询列表", notes = "参数：设备编号，设备名称，库点编号，设备类型，设备状态, 机构ID")
	@PostMapping(value = "/equipmentRecordsPage/{pageNum}/{pageSize}")
	public ResponseWrapper getEquipmentRecordsPage(@RequestBody EquipmentRecordsQueryVO equipmentRecordsQueryVO,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try {
			PageInfo<EquipmentRecordsVO> pageInfos = new PageInfo<EquipmentRecordsVO>(equipmentRecordsService.getEquipmentRecordsPage(equipmentRecordsQueryVO,pageNum,pageSize)) ;
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author WangZL
	 * @Description 根据设备编码获取设备记录
	 * @return
	 */
	@ApiOperation(value = "设备记录-查询详情", notes = "参数：设备记录ID")
	@GetMapping(value = "/equipmentRecords/{sbbajlId}")
	public ResponseWrapper getEquipmentRecordsInfoById(@PathVariable("sbbajlId")Integer sbbajlId) {
		try {
			EquipmentRecordsVO equipmentRecordsVO = equipmentRecordsService.getEquipmentRecordsInfoById(sbbajlId);
			if(equipmentRecordsVO == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "获取失败，该设备记录不存在", null);
			}
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), equipmentRecordsVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}


	/**
	 * @author WangZL
	 * @Description 新增设备记录
	 * @param 设备信息，备案状态, 审批意见
	 * @return
	 */
	@PostMapping(value = "/equipmentRecords")
	@ApiOperation(value = "设备记录-根据备案状态新增", notes = "参数：设备信息，备案状态, 审批意见")
    public ResponseWrapper insert(@RequestBody EquipmentRecordsVO equipmentRecordsVO) {
		try{
			if(equipmentRecordsVO.getSfba() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = equipmentRecordsService.insert(equipmentRecordsVO);
			if(x>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加设备记录失败,该设备记录已存在", null);
			} else if (x==-1) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "只有已备案和已驳回才能添加记录", null);
			} else if (x==-2) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "请输入驳回审批意见", null);
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

}
