package com.unis.base.server.controller;

import java.util.List;

import com.unis.base.common.vo.TyZjxxzbQueryVO;
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.TyZjxxzbVO;
import com.unis.base.server.service.TyZjxxzbService;
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 QualityMainController {

	@Autowired
	private TyZjxxzbService tyZjxxzbService;

	/**
	 * @author zhengbodang
	 * @Description 获取质检信息主表
	 * @param
	 * @return 质检信息主表
	 */
	@ApiOperation(value = "质检信息主表", notes = "参数：")
	@PostMapping(value = "/qualityMainList")
    public ResponseWrapper getZjxxzbList(@RequestBody TyZjxxzbQueryVO tyZjxxzbQueryVO){
        try {
        	List<TyZjxxzbVO> tyZjxxzbList = tyZjxxzbService.getTyZjxxzbList(tyZjxxzbQueryVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyZjxxzbList);
        } 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 = "/qualityMainPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyZjxxzbPage(@RequestBody TyZjxxzbQueryVO tyZjxxzbQueryVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyZjxxzbVO> pageInfos = new PageInfo<TyZjxxzbVO>(tyZjxxzbService.getTyZjxxzbPage(tyZjxxzbQueryVO,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 = "/qualityMain")
	@ApiOperation(value = "新增质检信息主表", notes = "参数：质检单编号，质检单名称")
    public ResponseWrapper insert(@RequestBody TyZjxxzbVO tyZjxxzbVO) {
		try{
			if(StringUtils.isEmpty(tyZjxxzbVO.getZjbgbh())||StringUtils.isEmpty(tyZjxxzbVO.getZjbgmc())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyZjxxzbService.insert(tyZjxxzbVO);
			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 = "/qualityMain")
	@ApiOperation(value = "更新质检信息主表", notes = "参数：质检信息编号")
    public ResponseWrapper update(@RequestBody TyZjxxzbVO tyZjxxzbVO) {
		try{
			if(StringUtils.isEmpty(tyZjxxzbVO.getZjxxid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyZjxxzbService.update(tyZjxxzbVO);
			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="/qualityMain/{zjxxid}")
	@ApiOperation(value = "根据信息编号删除质检信息主表", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("zjxxid") String zjxxid) {
		try{
			int x = tyZjxxzbService.delete(zjxxid);
			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="/qualityMain/{zjxxid}")
	@ApiOperation(value = "根据信息编号获取质检信息主表", notes = "参数：")
	public ResponseWrapper getZjxxzbByZjxxzbid(@PathVariable("zjxxid") String zjxxid) {
		try{
			TyZjxxzbVO tyZjxxzbVO = tyZjxxzbService.getZjxxzbByZjxxzbid(zjxxid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyZjxxzbVO);
        } 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 = "/batchQualityMain")
	@ApiOperation(value = "批量新增质检信息主表", notes = "参数：质检单编号，质检单名称")
    public ResponseWrapper batchInserts(@RequestBody List<TyZjxxzbVO> tyZjxxzbVO) {
		try{
			for (int t = 0; t < tyZjxxzbVO.size(); t++) {
				if(StringUtils.isEmpty(tyZjxxzbVO.get(t).getZjbgbh())||StringUtils.isEmpty(tyZjxxzbVO.get(t).getZjbgmc())) {
					return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
				}
			}
			int x = tyZjxxzbService.batchInserts(tyZjxxzbVO);
			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(),"服务错误，请联系管理员");
    }


	/**
	 * 根据信息编号获取质检信息主表
	 * @return
	 */
	@GetMapping(value="/qualityMain/getAllList")
	@ApiOperation(value = "根据信息编号获取质检信息主表", notes = "参数：")
	public ResponseWrapper getPageList(){
		try{
			PageInfo<TyZjxxzbVO> pageList = tyZjxxzbService.getPageList();
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
}
