package com.unis.base.server.controller;

import java.util.List;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.EquipmentTypeVO;
import com.unis.base.common.vo.EquipmentTypequeryVO;
import com.unis.base.common.vo.TyCrkxxVO;
import com.unis.base.common.vo.TyHtxxVO;
import com.unis.base.server.service.EquipmentTypeService;
import com.unis.base.server.service.TyCrkxxService;
import com.unis.base.server.service.TyHtxxService;
import com.unis.common.utils.ResponseWrapper;


@RestController
@Api(value = "设备类型", tags = "设备类型维护")
@Slf4j
public class EquipmentTypeController {

	@Autowired
	private EquipmentTypeService equipmentTypeService;

	/**
	 * 设备类型信息列表
	 * @param equipmentTypequeryVO
	 * @return
	 */
	@ApiOperation(value = "设备类型信息-查询列表", notes = "参数：设备类型，设备名称")
	@PostMapping(value = "/equipmentTypeList")
	public ResponseWrapper getEquipmentTypeList(@RequestBody EquipmentTypequeryVO equipmentTypequeryVO){
		try {
			List<EquipmentTypeVO> equipmentTypeList = equipmentTypeService.getEquipmentTypeList(equipmentTypequeryVO);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), equipmentTypeList);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * 设备类型信息列表
	 * @param equipmentTypequeryVO
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value = "设备类型信息-分页查询列表", notes = "参数：设备类型，设备名称")
	@PostMapping(value = "/equipmentTypePage/{pageNum}/{pageSize}")
    public ResponseWrapper getEquipmentTypePage(@RequestBody EquipmentTypequeryVO equipmentTypequeryVO,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        try {
        	//获取总条数
        	List<EquipmentTypeVO> equipmentTypeList = equipmentTypeService.getEquipmentTypeList(equipmentTypequeryVO);
        	
			PageInfo<EquipmentTypeVO> pageInfos = new PageInfo<EquipmentTypeVO>(equipmentTypeService.getEquipmentTypeListPage(equipmentTypequeryVO,pageNum,pageSize)) ;
			
			if(equipmentTypeList!=null) {
				
				pageInfos.setTotal(equipmentTypeList.size());
				
			}
			
            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(),"服务错误，请联系管理员");
    }
	
	
	/**
	 * 新增设备类型
	 * @param equipmentTypeVO
	 * @return
	 */
	@PostMapping(value = "/equipmentType")
	@ApiOperation(value = "新增设备类型", notes = "参数：设备类型")
    public ResponseWrapper insert(@RequestBody EquipmentTypeVO equipmentTypeVO) {
		try{
			int  i = 0;
			if(equipmentTypeVO !=null) {
				i = equipmentTypeService.insert(equipmentTypeVO);
			}
			if(i>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),i);
			} else if (i == -2 ) {
				return ResponseWrapper.error("设备类型及名称已存在");
			} else if (i == 0 ) {
	            return ResponseWrapper.error("设备类型为空");
			} else if (i == -1 ) {
	            return ResponseWrapper.error( "添加设备类型信息失败");
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
	
	
	/**
	 * 更新设备类型
	 * @param tyHtxxVO
	 * @return
	 */
	@PutMapping(value = "/equipmentType")
	@ApiOperation(value = "更新设备类型", notes = "参数：设备类型")
    public ResponseWrapper update(@RequestBody EquipmentTypeVO equipmentTypeVO) {
		try{
			int i=0;
			if(equipmentTypeVO == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			i = equipmentTypeService.update(equipmentTypeVO);
			if(i>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),i);
			} else{
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "更新设备类型失败", -1);
			} 
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
	
	/**
	 * 删除设备类型
	 */
	@DeleteMapping(value="/equipmentType/{sblxbh}")
	@ApiOperation(value = "根据合同信息id删除设备类型", notes = "参数：设备类型id")
	public ResponseWrapper delete(@PathVariable("sblxbh") String sblxbh) {
		try{
			int i = equipmentTypeService.delete(sblxbh);
			if(i>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),i);
			} else if (i==0) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败", null);
			} else {
				 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(),"服务错误，请联系管理员");
    }
	
	/**
	 * 获取设备类型
	 * @param sblxbh
	 * @return
	 */
	@GetMapping(value="/equipmentType/{sblxbh}")
	@ApiOperation(value = "根据设备类型编号获取设备类型信息", notes = "参数：")
	public ResponseWrapper getHtxxByHtxxid(@PathVariable("sblxbh") String sblxbh) {
		try{
			EquipmentTypeVO equipmentTypeVO = equipmentTypeService.getEquipmentTypeVOBysblxbh(sblxbh);
	        return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), equipmentTypeVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
}
