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.RestController;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.TyCrkxxPageVO;
import com.unis.base.common.vo.TyCrkxxVO;
import com.unis.base.server.service.TyCrkxxService;
import com.unis.common.utils.ResponseWrapper;

/**
 * @author zhengbodang
 * @version 1.0
 * @Title TyCrkxxController
 * @ProjectName 出入库结算信息 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "出入库信息 ", tags = "出入库信息维护")
@Slf4j
public class ExitDepotController {

	@Autowired
	private TyCrkxxService tyCrkxxService;

	/**
	 * @author zhengbodang
	 * @Description 获取出入库结算列表
	 * @param
	 * @return 出入库结算列表
	 */
	@ApiOperation(value = "出入库结算列表", notes = "参数：")
	@PostMapping(value = "/exitDepotList")
    public ResponseWrapper getTyCrkjsxxList(@RequestBody TyCrkxxVO tyCrkxxVO){
        try {
        	List<TyCrkxxVO> TyCrkxxVOs = tyCrkxxService.getTyCrkxxList(tyCrkxxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), TyCrkxxVOs);
        } 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 = "/exitDepotPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyCrkjsxxPage(@RequestBody TyCrkxxVO tyCrkxxVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyCrkxxVO> pageInfos = tyCrkxxService.getTyCrkxxPage(tyCrkxxVO,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 = "/exitDepot")
	@ApiOperation(value = "新增出入库信息", notes = "参数：出入库单号，出入库类型")
    public ResponseWrapper insert(@RequestBody TyCrkxxVO tyCrkxxVO) {
		try{
			if(StringUtils.isEmpty(tyCrkxxVO.getCrkdh())||StringUtils.isEmpty(tyCrkxxVO.getCrklx())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyCrkxxService.insert(tyCrkxxVO);
			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 = "/exitDepot")
	@ApiOperation(value = "更新出入库结算信息", notes = "参数：出入库信息id")
    public ResponseWrapper update(@RequestBody TyCrkxxVO tyCrkxxVO) {
		try{
			if(StringUtils.isEmpty(tyCrkxxVO.getCrkid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyCrkxxService.update(tyCrkxxVO);
			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="/exitDepot/{jsid}")
	@ApiOperation(value = "根据出入库结算信息编号删除出入库结算信息", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("jsid") String crkid) {
		try{
			int x = tyCrkxxService.delete(crkid);
			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="/exitDepot/{crkid}")
	@ApiOperation(value = "根据出入库信息编号获取出入库信息", notes = "参数：")
	public ResponseWrapper getDAjxxsjyByajbh(@PathVariable("crkid") String crkid) {
		try{
			TyCrkxxVO tyCrkxxVO = tyCrkxxService.getTyCrkxxByCrkid(crkid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyCrkxxVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }


	/**
	 * @author lidiaoquan
	 * @Description 分页获取出入库信息列表-含合同信息
	 * @param
	 * @return
	 */
	@ApiOperation(value = "分页获取出入库信息列表-含合同信息", notes = "参数：")
	@PostMapping(value = "/exitDepotJoinPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyCrkjsxxJoinPage(@RequestBody TyCrkxxPageVO tyCrkxxVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyCrkxxPageVO> pageInfos = tyCrkxxService.getTyCrkxxPageExtend(tyCrkxxVO, 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(),"服务错误，请联系管理员");
    }
}
