package com.unis.base.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.unis.base.common.vo.TyLskcxxQueryVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.TyLskcxxVO;
import com.unis.base.server.service.TyLskcxxService;
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 DAjxxsjyController
 * @ProjectName 粮食库存信息 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "粮食库存信息 ", tags = "粮食库存信息 维护")
@Slf4j
public class FoodStocksController {

	@Autowired
	private TyLskcxxService tyLskcxxService;

	/**
	 * @author zhengbodang
	 * @Description 获取粮食库存列表
	 * @param
	 * @return 粮食库存列表
	 */
	@ApiOperation(value = "粮食库存列表", notes = "参数：")
	@PostMapping(value = "/foodStocksList")
    public ResponseWrapper getTyLskcxxList(@RequestBody TyLskcxxQueryVO  tyLskcxxQueryVO
										   ){
        try {
			List<TyLskcxxVO> tyLskcxxVOs = tyLskcxxService.getTyLskcxxList(tyLskcxxQueryVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyLskcxxVOs);
        } 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 = "/foodStocksPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTyLskcxxPage(@RequestBody TyLskcxxQueryVO  tyLskcxxQueryVO,
										   @PathVariable("pageNum")Integer pageNum,
										   @PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TyLskcxxVO> pageInfos = new PageInfo<TyLskcxxVO>(tyLskcxxService.getTyLskcxxPage(tyLskcxxQueryVO,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  tyLskcxxVO
	 * @return 新增状态
	 */
	@PostMapping(value = "/foodStocks")
	@ApiOperation(value = "新增粮食库存信息", notes = "参数：货位编号，廒间编号")
    public ResponseWrapper insert(@RequestBody TyLskcxxVO tyLskcxxVO) {
		try{
			if(StringUtils.isEmpty(tyLskcxxVO.getHwbm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyLskcxxService.insert(tyLskcxxVO);
			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 = "/foodStocks")
	@ApiOperation(value = "更新粮食库存信息", notes = "参数：库存粮食id")
    public ResponseWrapper update(@RequestBody TyLskcxxVO tyLskcxxVO) {
		try{
			if(StringUtils.isEmpty(tyLskcxxVO.getKcxxid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyLskcxxService.update(tyLskcxxVO);
			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(),"服务错误，请联系管理员");
    }

	@PostMapping(value = "/foodStocks/updateByEntitySelective")
	@ApiOperation(value = "更新粮食库存信息", notes = "参数：库存粮食id")
	public ResponseWrapper updateByEntitySelective(@RequestBody TyLskcxxVO tyLskcxxVO) {
		try{
			if(StringUtils.isEmpty(tyLskcxxVO.getKcxxid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tyLskcxxService.updateByEntitySelective(tyLskcxxVO);
			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="/foodStocks/{kcxxid}")
	@ApiOperation(value = "根据粮食库存信息编号删除粮食库存信息", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("kcxxid") String kcxxid) {
		try{
			int x = tyLskcxxService.delete(kcxxid);
			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="/foodStocks/{kcxxid}")
	@ApiOperation(value = "获取粮食库存信息", notes = "参数：")
	public ResponseWrapper getTyLskcxxByTfzyid(@PathVariable("kcxxid") String kcxxid) {
		try{
			TyLskcxxVO tyLskcxxVO = tyLskcxxService.getLskcxxByLskcxxid(kcxxid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyLskcxxVO);
        } 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 状态
	 */
	@PostMapping(value="/foodStocks/lastest")
	@ApiOperation(value = "根据品种,仓号,公司,截止时间,获取唯一粮食库存信息", notes = "参数：")
	public ResponseWrapper selectLastestByEntity(@RequestBody TyLskcxxQueryVO tyLskcxxQueryVO) {
		try{
			TyLskcxxVO tyLskcxxVO = tyLskcxxService.selectLastestByEntity(tyLskcxxQueryVO);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyLskcxxVO);
		} 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 状态
	 */
	@PostMapping(value="/foodStock/selectById")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "kcxxid", value = "id", dataType = "String", paramType = "header"),
			@ApiImplicitParam(name = "sscfbh", value = "仓号编码", dataType = "String", paramType = "header"),
			@ApiImplicitParam(name = "sscfbhOut", value = "市库的仓房编号", dataType = "String", paramType = "header")
	})
	@ApiOperation(value = "根据id,仓号获取唯一粮食库存信息", notes = "参数：")
	public ResponseWrapper selectById(@RequestBody TyLskcxxVO tyLskcxxVO) {
		try{
			return tyLskcxxService.selectById(tyLskcxxVO);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

}
