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.common.utils.ResponseWrapper;
import com.unis.base.common.vo.DHwxxsjyVO;
import com.unis.base.server.entity.DHwxxsjyDO;
import com.unis.base.server.service.DHwxxsjyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

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

	@Autowired
	private DHwxxsjyService dHwxxsjyService;

	/**
	 * @author huangxing
	 * @Description 获取仓房列表
	 * @param 仓房编号，仓房名称，货位编号，货位名称，货位状态
	 * @return 仓房信息列表
	 */
	@ApiOperation(value = "获取货位信息列表", notes = "参数：仓房编号，仓房名称，货位编号，货位名称，货位状态")
	@PostMapping(value = "/goodsallocationList")
    public ResponseWrapper getDHwxxsjyList(@RequestBody DHwxxsjyVO dHwxxsjyVO ){
        try {
        	List<DHwxxsjyVO> dHwxxsjyDOs = dHwxxsjyService.getDHwxxsjyList(dHwxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dHwxxsjyDOs);
        } 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 = "/goodsallocationPage/{pageNum}/{pageSize}")
    public ResponseWrapper getDHwxxsjyPage(@RequestBody DHwxxsjyVO dHwxxsjyVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<DHwxxsjyVO> pageInfos = new PageInfo<DHwxxsjyVO>(dHwxxsjyService.getDHwxxsjyPage(dHwxxsjyVO,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 货位信息实体 models
	 * @return 新增状态
	 */
	@PostMapping(value = "/goodsallocation")
	@ApiOperation(value = "新增货位信息", notes = "参数：货位名称，仓房编号")
    public ResponseWrapper insert(@RequestBody DHwxxsjyVO dHwxxsjyVO) {
		try{
//			if(StringUtils.isEmpty(dHwxxsjyVO.getHwmc())||StringUtils.isEmpty(dHwxxsjyVO.getCfbh())) {
			if(StringUtils.isEmpty(dHwxxsjyVO.getHwmc())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dHwxxsjyService.insert(dHwxxsjyVO);
			if(x>0) {
				dHwxxsjyService.depotPush(dHwxxsjyVO);
	            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 参数：货位信息实体 models
	 * @return 新增状态
	 */
	@PutMapping(value = "/goodsallocation")
	@ApiOperation(value = "更新货位信息", notes = "参数：货位编号")
    public ResponseWrapper update(@RequestBody DHwxxsjyVO dHwxxsjyVO) {
		try{
			if(StringUtils.isEmpty(dHwxxsjyVO.getHwbh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dHwxxsjyService.update(dHwxxsjyVO);
			if(x>0) {
				dHwxxsjyService.depotPush(dHwxxsjyVO);
	            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="/goodsallocation/{hwbh}")
	@ApiOperation(value = "根据货位编号删除货位信息", notes = "参数：货位编号")
	public ResponseWrapper delete(@PathVariable("hwbh") String hwbh) {
		try{
			int x = dHwxxsjyService.delete(hwbh);
			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="/goodsallocation/{hwbh}")
	@ApiOperation(value = "根据货位编号获取货位信息", notes = "参数：货位编号")
	public ResponseWrapper getDHwxxsjyByhwbh(@PathVariable("hwbh") String hwbh) {
		try{
			DHwxxsjyDO dHwxxsjyDO = dHwxxsjyService.getDHwxxsjyByhwbh(hwbh);
	        return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),dHwxxsjyDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
}
