package com.unis.base.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.SinkpointRecordsQueryVO;
import com.unis.base.common.vo.SinkpointRecordsVO;
import com.unis.base.server.service.SinkpointRecordsService;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author WangZL
 * @version 1.0
 * @Title SinkpointRecordsController
 * @ProjectName 库点记录 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "库点记录", tags = "库点记录")
@Slf4j
public class SinkpointRecordsController {

	@Autowired
	private SinkpointRecordsService sinkpointRecordsService;

	/**
	 * @author WangZL
	 * @Description 获取设备列表
	 * @param 涉粮库点备案记录ID，统一社会信用代码，备案时间（年度），是否备案
	 * @return 库点记录列表
	 */
	@ApiOperation(value = "库点记录-查询列表", notes = "参数：涉粮库点备案记录ID，统一社会信用代码，备案时间（年度），是否备案")
	@PostMapping(value = "/sinkpointRecordsList")
    public ResponseWrapper getSinkpointRecordsList(@RequestBody SinkpointRecordsQueryVO sinkpointRecordsQueryVO){
        try {
        	List<SinkpointRecordsVO> SinkpointRecordsList = sinkpointRecordsService.getSinkpointRecordsList(sinkpointRecordsQueryVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), SinkpointRecordsList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author WangZL
	 * @Description 获取库点记录列表
	 * @param 涉粮库点备案记录ID，统一社会信用代码，备案时间（年度），是否备案
	 * @return 库点记录列表
	 */
	@ApiOperation(value = "库点记录-分页查询列表", notes = "参数：涉粮库点备案记录ID，统一社会信用代码，备案时间（年度），是否备案")
	@PostMapping(value = "/sinkpointRecordsPage/{pageNum}/{pageSize}")
	public ResponseWrapper getSinkpointRecordsPage(@RequestBody SinkpointRecordsQueryVO sinkpointRecordsQueryVO,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try {
			PageInfo<SinkpointRecordsVO> pageInfos = new PageInfo<SinkpointRecordsVO>(sinkpointRecordsService.getSinkpointRecordsPage(sinkpointRecordsQueryVO,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 WangZL
	 * @Description 根据设备编码获取库点记录
	 * @return
	 */
	@ApiOperation(value = "库点记录-查询详情", notes = "参数：库点记录ID")
	@GetMapping(value = "/sinkpointRecords/{kdbabh}")
	public ResponseWrapper getSinkpointRecordsInfoById(@PathVariable("kdbabh")String kdbabh) {
		try {
			SinkpointRecordsVO SinkpointRecordsVO = sinkpointRecordsService.getSinkpointRecordsInfoById(kdbabh);
			if(SinkpointRecordsVO == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "获取失败，该库点记录不存在", null);
			}
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), SinkpointRecordsVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}


	/**
	 * @author WangZL
	 * @Description 新增库点记录
	 * @param 设备信息，备案状态, 审批意见
	 * @return
	 */
	@PostMapping(value = "/sinkpointRecords")
	@ApiOperation(value = "库点记录-根据备案状态新增", notes = "参数：设备信息，备案状态, 审批意见")
    public ResponseWrapper insert(@RequestBody SinkpointRecordsVO SinkpointRecordsVO) {
		try{
			if(SinkpointRecordsVO.getSfba() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			String kdbabh = sinkpointRecordsService.insert(SinkpointRecordsVO);
			if (kdbabh == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加失败", null);
			}
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),kdbabh);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author WangZL
	 * @Description 修改库点信息
	 * @param 库点记录信息 models
	 * @return
	 */
	@PutMapping(value = "/sinkpointRecords")
	@ApiOperation(value = "库点记录-更新", notes = "参数：库点记录信息")
	public ResponseWrapper update(@RequestBody SinkpointRecordsVO sinkpointRecordsVO) {
		try{
			int x = sinkpointRecordsService.update(sinkpointRecordsVO);
			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(),"服务错误，请联系管理员");
	}

}
