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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.GranaryVO;
import com.unis.base.server.entity.DAjxxsjyDO;
import com.unis.base.server.service.GranaryService;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


/**
 * @author huangxing
 * @version 1.0
 * @Title DAjxxsjyController
 * @ProjectName 廒间信息 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "廒间信息", tags = "廒间信息维护")
@Slf4j
public class GranaryController {

	@Autowired
	private GranaryService granaryService;

	/**
	 * @author huangxing
	 * @Description 获取廒间列表
	 * @param 仓房编号，仓房名称，廒间编号，廒间名称，廒间状态
	 * @return 廒间信息列表
	 */
	@ApiOperation(value = "获取廒间信息列表", notes = "参数：仓房编号，仓房名称，廒间编号，廒间名称，廒间状态")
	@PostMapping(value = "/granaryList")
    public ResponseWrapper getGranaryList(@RequestBody GranaryVO granaryVO ){
        try {
        	List<GranaryVO> dAjxxsjyDOs = granaryService.getGranaryList(granaryVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dAjxxsjyDOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author huangxing
	 * @Description 获取廒间信息列表
	 * @param 仓房编号，仓房名称，廒间编号，廒间名称，廒间状态
	 * @return 廒间信息列表
	 */
	@ApiOperation(value = "分页获取廒间信息列表", notes = "参数：仓房编号，仓房名称，廒间编号，廒间名称，廒间状态")
	@PostMapping(value = "/granaryPage/{pageNum}/{pageSize}")
    public ResponseWrapper getGranaryPage(@RequestBody GranaryVO granaryVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<GranaryVO> pageInfos = new PageInfo<GranaryVO>(granaryService.getGranaryPage(granaryVO,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 huangxing
	 * @Description 新增廒间信息
	 * @param 廒间信息对象 granaryVO
	 * @return 新增状态
	 */
	@PostMapping(value = "/granary")
	@ApiOperation(value = "新增廒间信息", notes = "参数：仓房编号，廒间名称")
    public ResponseWrapper insert(@RequestBody GranaryVO granaryVO) {
		try{
			if(StringUtils.isEmpty(granaryVO.getCfbh())||StringUtils.isEmpty(granaryVO.getAjmc())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = granaryService.insert(granaryVO);
			if(x>0) {
				granaryService.depotPush(granaryVO);
	            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 huangxing
	 * @Description 更新廒间信息
	 * @param 参数：廒间信息对象 granaryVO
	 * @return 新增状态
	 */
	@PutMapping(value = "/granary")
	@ApiOperation(value = "更新廒间信息", notes = "参数：廒间编号")
    public ResponseWrapper update(@RequestBody GranaryVO granaryVO) {
		try{
			if(StringUtils.isEmpty(granaryVO.getAjbh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = granaryService.update(granaryVO);
			if(x>0) {
				granaryService.depotPush(granaryVO);
	            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 huangxing
	 * @Description 删除廒间信息
	 * @param 参数：廒间编号
	 * @return 状态
	 */
	@DeleteMapping(value="/granary/{ajbh}")
	@ApiOperation(value = "根据廒间编号删除廒间信息", notes = "参数：廒间编号")
	public ResponseWrapper delete(@PathVariable("ajbh") String ajbh) {
		try{
			int x = granaryService.delete(ajbh);
			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 huangxing
	 * @Description 获取廒间信息
	 * @param 参数：廒间编号
	 * @return 廒间信息
	 */
	@GetMapping(value="/granary/{ajbh}")
	@ApiOperation(value = "根据廒间编号获取廒间信息", notes = "参数：廒间编号")
	public ResponseWrapper getGranaryByajbh(@PathVariable("ajbh") String ajbh) {
		try{
			GranaryVO granaryVO = granaryService.getGranaryByajbh(ajbh);
	        return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),granaryVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
}
