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.TyLqmxxxVO;
import com.unis.base.server.service.TyLqmxxxService;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

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

	@Autowired
	private TyLqmxxxService tyLqmxxxService;

	/**
	 * @author zhengbodang
	 * @Description 获取粮情列表
	 * @param
	 * @return 粮情列表
	 */
	@ApiOperation(value = "粮情列表", notes = "参数：")
	@PostMapping(value = "/grainSituationDetailList")
    public ResponseWrapper getTyLqmxxxList(@RequestBody TyLqmxxxVO tyLqmxxxVO){
        try {
        	List<TyLqmxxxVO> tyLqmxxxVOs = tyLqmxxxService.getTyLqmxxxList(tyLqmxxxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyLqmxxxVOs);
        } 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 = "/grainSituationDetailPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyLqmxxxPage(@RequestBody TyLqmxxxVO tyLqmxxxVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyLqmxxxVO> pageInfos = new PageInfo<TyLqmxxxVO>(tyLqmxxxService.getTyLqmxxxPage(tyLqmxxxVO,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 = "/grainSituationDetail")
	@ApiOperation(value = "新增粮情信息", notes = "参数：粮情id")
    public ResponseWrapper insert(@RequestBody TyLqmxxxVO tyLqmxxxVO) {
		try{
			if(StringUtils.isEmpty(tyLqmxxxVO.getLqid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyLqmxxxService.insert(tyLqmxxxVO);
			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 = "/grainSituationDetail")
	@ApiOperation(value = "更新粮情信息", notes = "参数：货位编号，仓房编号")
    public ResponseWrapper update(@RequestBody TyLqmxxxVO tyLqmxxxVO) {
		try{
			if(StringUtils.isEmpty(tyLqmxxxVO.getCfbh())||StringUtils.isEmpty(tyLqmxxxVO.getHwbm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyLqmxxxService.update(tyLqmxxxVO);
			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="/grainSituationDetail/{lqid}")
	@ApiOperation(value = "根据粮情信息编号删除粮情信息", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("lqid") String lqid) {
		try{
			int x = tyLqmxxxService.delete(lqid);
			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="/grainSituationDetail/{lqid}")
	@ApiOperation(value = "获取粮情信息", notes = "参数：")
	public ResponseWrapper getTyLqmxxxByLqid(@PathVariable("lqid") String lqid) {
		try{
			TyLqmxxxVO tyLqmxxxVO = tyLqmxxxService.getLqmxxxByLqmxxxid(lqid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyLqmxxxVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

}
