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.TyCrkjsxxVO;
import com.unis.base.common.vo.TyJhxxmxbVO;
import com.unis.base.server.service.TyCrkjsxxService;
import com.unis.base.server.service.TyJhxxmxbService;
import com.unis.common.utils.ResponseWrapper;

/**
 * @author zhengbodang
 * @version 1.0
 * @Title TyJhxxmxbController
 * @ProjectName 计划信息明细 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "计划信息明细 ", tags = "计划信息明细维护")
@Slf4j
public class PlanInfoController {

	@Autowired
	private TyJhxxmxbService tyJhxxmxbService;

	/**
	 * @author zhengbodang
	 * @Description 获取计划信息明细列表
	 * @param
	 * @return 计划信息明细列表
	 */
	@ApiOperation(value = "计划信息明细列表", notes = "参数：")
	@PostMapping(value = "/planInfoList")
    public ResponseWrapper getTyCrkjsxxList(@RequestBody TyJhxxmxbVO tyJhxxmxbVO){
        try {
        	List<TyJhxxmxbVO> tyJhxxmxbVOs = tyJhxxmxbService.getTyJhxxmxbList(tyJhxxmxbVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyJhxxmxbVOs);
        } 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 = "/planInfoPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyJhxxmxbPage(@RequestBody TyJhxxmxbVO tyJhxxmxbVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyJhxxmxbVO> pageInfos = new PageInfo<TyJhxxmxbVO>(tyJhxxmxbService.getTyJhxxmxbPage(tyJhxxmxbVO,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 = "/planInfo")
	@ApiOperation(value = "新增计划信息明细信息", notes = "参数：计划信息di")
    public ResponseWrapper insert(@RequestBody TyJhxxmxbVO tyJhxxmxbVO) {
		try{
			if(StringUtils.isEmpty(tyJhxxmxbVO.getJhxxid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyJhxxmxbService.insert(tyJhxxmxbVO);
			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 = "/planInfo")
	@ApiOperation(value = "更新计划信息明细信息", notes = "参数：顺序号")
    public ResponseWrapper update(@RequestBody TyJhxxmxbVO tyJhxxmxbVO) {
		try{
			int x = tyJhxxmxbService.update(tyJhxxmxbVO);
			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="/planInfo/{jhxxid}")
	@ApiOperation(value = "根据计划信息明细信息编号删除出入库结算信息", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("jhxxid") String jhxxid) {
		try{
			int x = tyJhxxmxbService.delete(jhxxid);
			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="/planInfo/{jhxxid}")
	@ApiOperation(value = "根据计划信息明细信息编号获取计划信息", notes = "参数：")
	public ResponseWrapper getDAjxxsjyByajbh(@PathVariable("jhxxid") String jhxxid) {
		try{
			TyJhxxmxbVO tyJhxxmxbVO = tyJhxxmxbService.getJhxxmxbByJhxxmxbid(jhxxid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyJhxxmxbVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
}
