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.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.unis.common.utils.ResponseWrapper;
import com.unis.base.common.vo.DCfztsjyVO;
import com.unis.base.server.entity.DCfztsjyDO;
import com.unis.base.server.service.DCfztsjyService;

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

/**
 * @author huangxing
 * @version 1.0
 * @Title DCfztsjyController
 * @ProjectName 仓房状态信息 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "仓房状态信息", tags = "仓房状态信息维护")
@Slf4j
public class WarehouseStatusController {

	@Autowired
	private DCfztsjyService dCfztsjyService;

	/**
	 * @author huangxing
	 * @Description 获取仓房状态信息列表
	 * @return 仓房状态信息列表
	 */
	@ApiOperation(value = "获取仓房状态信息列表", notes = "参数：仓房编号，仓房名称")
	@PostMapping(value = "/warehouseStatusList")
    public ResponseWrapper getDCfztsjyList(@RequestBody DCfztsjyVO DCfztsjyVO){
        try {
        	List<DCfztsjyVO> DCfztsjyDOs = dCfztsjyService.getDCfztsjyList(DCfztsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), DCfztsjyDOs);
        } 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 根据仓房编码获取仓房状态信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "根据仓房编码获取仓房状态信息", notes = "参数：仓房编号")
	@GetMapping(value = "/warehouseStatus/{cfbh}")
	public ResponseWrapper getDCfztsjyInfoBycfbh(@PathVariable("cfbh")String cfbh) {
		try {
			DCfztsjyDO dKdxxsjyDO = dCfztsjyService.getDCfztsjyInfoBycfbh(cfbh);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDO);
        } 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 根据库点编码获取库点信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "更新仓房状态信息", notes = "参数：仓房编号")
	@PutMapping(value = "/warehouseStatus")
	public ResponseWrapper update(@RequestBody DCfztsjyVO dCfztsjyVO) {
		try{
			if(StringUtils.isEmpty(dCfztsjyVO.getCfbh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dCfztsjyService.update(dCfztsjyVO);
			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);
			}
        } 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 = "/warehouseStatus")
	@ApiOperation(value = "新增仓房信息", notes = "参数：仓房名称，库点代码")
    public ResponseWrapper insert(@RequestBody DCfztsjyVO dCfztsjyVO) {
		try{
			if(StringUtils.isEmpty(dCfztsjyVO.getCfmc())||StringUtils.isEmpty(dCfztsjyVO.getKddm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dCfztsjyService.insert(dCfztsjyVO);
			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(),"服务错误，请联系管理员");
    }
}
