 package com.unis.base.server.controller;

import java.util.List;

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.RestController;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.TyTfzyxxHisVO;
import com.unis.base.common.vo.TyTfzyxxVO;
import com.unis.base.server.service.TyTfzyxxService;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhengbodang
 * @version 1.0
 * @Title DAjxxsjyController
 * @ProjectName 通风作业信息 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "通风作业信息 ", tags = "通风作业信息 维护")
@Slf4j
public class VentilationOperationController {

	@Autowired
	private TyTfzyxxService tyTfzyxxService;

	/**
	 * @author zhengbodang
	 * @Description 获取通风作业列表
	 * @param
	 * @return 通风作业列表
	 */
	@ApiOperation(value = "通风作业列表", notes = "参数：")
	@PostMapping(value = "/ventilationOperationList")
    public ResponseWrapper getTyTfzyxxList(@RequestBody TyTfzyxxVO tyTfzyxxVO){
        try {
        	List<TyTfzyxxVO> tyTfzyxxVOs = tyTfzyxxService.getTyTfzyxxList(tyTfzyxxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyTfzyxxVOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author zhengbodang
	 * @Description 分页获取通风作业信息列表
	 * @param
	 * @return
	 */
	@ApiOperation(value = "分页获取通风作业信息列表", notes = "参数：")
	@PostMapping(value = "/ventilationOperationPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyTfzyxxPage(@RequestBody TyTfzyxxVO tyTfzyxxVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyTfzyxxVO> pageInfos = tyTfzyxxService.getTyTfzyxxPage(tyTfzyxxVO,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 zhengbodang
	 * @Description 新增通风作业信息
	 * @param 廒间信息实体 models
	 * @return 新增状态
	 */
	@PostMapping(value = "/ventilationOperation")
	@ApiOperation(value = "新增通风作业信息", notes = "参数：方案编号，方案名称")
    public ResponseWrapper insert(@RequestBody TyTfzyxxVO tyTfzyxxVO) {
		try{
			if(StringUtils.isEmpty(tyTfzyxxVO.getFamc())||StringUtils.isEmpty(tyTfzyxxVO.getFabh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyTfzyxxService.insert(tyTfzyxxVO);
			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);
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author zhengbodang
	 * @Description 更新通风作业信息
	 * @param
	 * @return 新增状态
	 */
	@PutMapping(value = "/ventilationOperation")
	@ApiOperation(value = "更新通风作业信息", notes = "参数：通风作业id")
    public ResponseWrapper update(@RequestBody TyTfzyxxVO tyTfzyxxVO) {
		try{
			if(StringUtils.isEmpty(tyTfzyxxVO.getTfzyid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyTfzyxxService.update(tyTfzyxxVO);
			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 {
	            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(),"服务错误，请联系管理员");
    }

	/**
	 * @author zhengbodang
	 * @Description 删除通风作业信息
	 * @param
	 * @return 状态
	 */
	@DeleteMapping(value="/ventilationOperation/{jsid}")
	@ApiOperation(value = "根据通风作业信息编号删除通风作业信息", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("jsid") String jsid) {
		try{
			int x = tyTfzyxxService.delete(jsid);
			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 {
				 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(),"服务错误，请联系管理员");
    }

	/**
	 * @author zhengbodang
	 * @Description 删除通风作业信息
	 * @param
	 * @return 状态
	 */
	@GetMapping(value="/ventilationOperation/{tfzyid}")
	@ApiOperation(value = "获取通风作业信息", notes = "参数：备案信息")
	public ResponseWrapper getTyTfzyxxByTfzyid(@PathVariable("tfzyid") String tfzyid) {
		try{
			TyTfzyxxVO tyTfzyxxVO = tyTfzyxxService.getTfzyxxByTfzyxxid(tfzyid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyTfzyxxVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }


	/**
	 * 通风作业信息-更改备案状态
	 * @param tyXzzyxxVO
	 * @return
	 */
	@PutMapping(value = "/ventilationOperationStatus")
	@ApiOperation(value = "通风作业信息-更改备案状态", notes = "参数：备案信息")
	public ResponseWrapper ventilationOperationStatus(@RequestBody TyTfzyxxHisVO tyTfzyxxHisVO) {
		try {
			if (tyTfzyxxHisVO.getSfba() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			Integer i = tyTfzyxxService.ventilationOperationStatus(tyTfzyxxHisVO);
			if (i > 0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
			} else if (i==-1) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "保存成功-(有已备案和已驳回才能添加记录)", null);
			} else if (i==-2) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "请输入驳回审批意见", null);
			} else {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "操作失败，请联系管理员");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
		}
	}
}
