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.TyLhtzdVO;
import com.unis.base.server.service.TyLhtzdService;
import com.unis.common.utils.ResponseWrapper;

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

	@Autowired
	private TyLhtzdService tyLhtzdService;

	/**
	 * @author zhengbodang
	 * @Description 获取出入库通知单列表
	 * @param
	 * @return 出入库结算列表
	 */
	@ApiOperation(value = "出入库结算列表", notes = "参数：")
	@PostMapping(value = "/warehouseNoticeList")
    public ResponseWrapper getTyCrkjsxxList(@RequestBody TyLhtzdVO tyLhtzdVO){
        try {
        	List<TyLhtzdVO> tyLhtzdVOs = tyLhtzdService.getTyLhtzdList(tyLhtzdVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyLhtzdVOs);
        } 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 = "/warehouseNoticePage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyCrkjsxxPage(@RequestBody TyLhtzdVO tyLhtzdVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyLhtzdVO> pageInfos = new PageInfo<TyLhtzdVO>(tyLhtzdService.getTyLhtzdPage(tyLhtzdVO,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 = "/warehouseNotice")
	@ApiOperation(value = "新增出入库通知单信息", notes = "参数：通知单类型，通知单单号")
    public ResponseWrapper insert(@RequestBody TyLhtzdVO tyLhtzdVO) {
		try{
			if(StringUtils.isEmpty(tyLhtzdVO.getTzdlx())||StringUtils.isEmpty(tyLhtzdVO.getTzdh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyLhtzdService.insert(tyLhtzdVO);
			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 = "/warehouseNotice")
	@ApiOperation(value = "更新出入库通知单信息", notes = "参数：通知单id")
    public ResponseWrapper update(@RequestBody TyLhtzdVO tyLhtzdVO) {
		try{
			if(StringUtils.isEmpty(tyLhtzdVO.getTzdid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyLhtzdService.update(tyLhtzdVO);
			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="/warehouseNotice/{tzdid}")
	@ApiOperation(value = "根据出入库结算信息编号删除出入库结算信息", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("tzdid") String tzdid) {
		try{
			int x = tyLhtzdService.delete(tzdid);
			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="/warehouseNotice/{tzdid}")
	@ApiOperation(value = "根据结算信息编号获取出入库结算信息", notes = "参数：")
	public ResponseWrapper getDAjxxsjyByajbh(@PathVariable("tzdid") String tzdid) {
		try{
			TyLhtzdVO tyLhtzdVO = tyLhtzdService.getLhtzdByLhtzdid(tzdid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyLhtzdVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
}
