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.TyZjxxmxbVO;
import com.unis.base.server.service.TyZjxxmxbService;
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 QualityInfoController {

	@Autowired
	private TyZjxxmxbService tyZjxxmxbService;

	/**
	 * @author zhengbodang
	 * @Description 获取质检列表
	 * @param
	 * @return 质检列表
	 */
	@ApiOperation(value = "质检列表", notes = "参数：")
	@PostMapping(value = "/qualityInfoList")
    public ResponseWrapper getTyZjxxmxbList(@RequestBody TyZjxxmxbVO tyZjxxmxbVO){
        try {
        	List<TyZjxxmxbVO> tyZjxxmxbVOs = tyZjxxmxbService.getTyZjxxmxbList(tyZjxxmxbVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyZjxxmxbVOs);
        } 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 = "/qualityInfoPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyZjxxmxbPage(@RequestBody TyZjxxmxbVO tyZjxxmxbVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyZjxxmxbVO> pageInfos = new PageInfo<TyZjxxmxbVO>(tyZjxxmxbService.getTyZjxxmxbPage(tyZjxxmxbVO,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 = "/qualityInfo")
	@ApiOperation(value = "新增质检信息", notes = "参数：质检信息id，检验单号")
    public ResponseWrapper insert(@RequestBody TyZjxxmxbVO tyZjxxmxbVO) {
		try{
			if(StringUtils.isEmpty(tyZjxxmxbVO.getZjxxid())||StringUtils.isEmpty(tyZjxxmxbVO.getJydh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyZjxxmxbService.insert(tyZjxxmxbVO);
			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 = "/qualityInfo")
	@ApiOperation(value = "更新质检信息", notes = "参数：顺序号")
    public ResponseWrapper update(@RequestBody TyZjxxmxbVO tyZjxxmxbVO) {
		try{
			int x = tyZjxxmxbService.update(tyZjxxmxbVO);
			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="/qualityInfo/{zjxxid}")
	@ApiOperation(value = "根据质检信息编号删除质检信息", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("zjxxid") String zjxxid) {
		try{
			int x = tyZjxxmxbService.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="/qualityInfo/{zjxxid}")
	@ApiOperation(value = "获取质检信息", notes = "参数：")
	public ResponseWrapper getTyZjxxmxbByTfzyid(@PathVariable("zjxxid") String zjxxid) {
		try{
			TyZjxxmxbVO tyZjxxmxbVO = tyZjxxmxbService.getZjxxmxbByZjxxmxbid(zjxxid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyZjxxmxbVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }


	/**
	 * 用zjxxid获取所有列表
	 * @param zjxxid
	 * @return
	 */
	@GetMapping(value="/qualityInfoByzjxxid/{zjxxid}")
	@ApiOperation(value = "获取质检信息", notes = "参数：")
	public ResponseWrapper  getAllZjxxmx(@PathVariable("zjxxid") String zjxxid){
		try{
			List<TyZjxxmxbVO> tyZjxxmxbVOList = tyZjxxmxbService.getTyZjxxmxbVOList(zjxxid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyZjxxmxbVOList);
		}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 = "/batchQualityInfo")
	@ApiOperation(value = "批量新增质检明细信息", notes = "参数：质检信息id，检验单号")
    public ResponseWrapper batchInserts(@RequestBody List<TyZjxxmxbVO> tyZjxxmxbVO) {
		try{
			for (int t = 0; t < tyZjxxmxbVO.size(); t++) {
				if(StringUtils.isEmpty(tyZjxxmxbVO.get(t).getZjxxid())||StringUtils.isEmpty(tyZjxxmxbVO.get(t).getJydh())) {
					return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
				}
			}
			int x = tyZjxxmxbService.batchInserts(tyZjxxmxbVO);
			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(),"服务错误，请联系管理员");
    }
}
