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.TyCrkxxVO;
import com.unis.base.common.vo.TyHtxxVO;
import com.unis.base.server.service.TyCrkxxService;
import com.unis.base.server.service.TyHtxxService;
import com.unis.common.utils.ResponseWrapper;

/**
 * @author zhengbodang
 * @version 1.0
 * @Title TyHtxxController
 * @ProjectName 合同信息 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "合同信息 ", tags = "合同信息维护")
@Slf4j
public class ContractController {

	@Autowired
	private TyHtxxService hyHtxxService;


	@ApiOperation(value = "合同信息列表", notes = "参数：")
	@PostMapping(value = "/contractList")
    public ResponseWrapper getTyHtxxList(@RequestBody TyHtxxVO tyHtxxVO){
        try {
        	List<TyHtxxVO> tyHtxxVOVOs = hyHtxxService.getTyHtxxList(tyHtxxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyHtxxVOVOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }


	@ApiOperation(value = "分页获取合同信息列表", notes = "参数：")
	@PostMapping(value = "/contractPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyHtxxPage(@RequestBody TyHtxxVO tyHtxxVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyHtxxVO> pageInfos = new PageInfo<TyHtxxVO>(hyHtxxService.getTyHtxxPage(tyHtxxVO,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(),"服务错误，请联系管理员");
    }


	@PostMapping(value = "/contract")
	@ApiOperation(value = "新增合同信息", notes = "参数：合同编号，合同名称")
    public ResponseWrapper insert(@RequestBody TyHtxxVO tyHtxxVO) {
		try{
			if(StringUtils.isEmpty(tyHtxxVO.getHtbh())||StringUtils.isEmpty(tyHtxxVO.getHtmc())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = hyHtxxService.insert(tyHtxxVO);
			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(),"服务错误，请联系管理员");
    }


	@PutMapping(value = "/contract")
	@ApiOperation(value = "更新合同信息", notes = "参数：合同信息id")
    public ResponseWrapper update(@RequestBody TyHtxxVO tyHtxxVO) {
		try{
			if(StringUtils.isEmpty(tyHtxxVO.getHtxxid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = hyHtxxService.update(tyHtxxVO);
			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(),"服务错误，请联系管理员");
    }


	@DeleteMapping(value="/contract/{htxxid}")
	@ApiOperation(value = "根据合同信息id删除合同信息", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("htxxid") String htxxid) {
		try{
			int x = hyHtxxService.delete(htxxid);
			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(),"服务错误，请联系管理员");
    }


	@GetMapping(value="/contract/{htxxid}")
	@ApiOperation(value = "根据合同编号获取合同信息", notes = "参数：")
	public ResponseWrapper getHtxxByHtxxid(@PathVariable("htxxid") String htxxid) {
		try{
			TyHtxxVO tyHtxxVO = hyHtxxService.getHtxxByHtxxid(htxxid);
	        return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyHtxxVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
}
