package com.unis.base.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.SampleQueryVO;
import com.unis.base.common.vo.SampleVO;
import com.unis.base.server.service.SampleService;
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.lang3.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 SampleController
 * @ProjectName 样品信息 SampleController
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "样品信息 ", tags = "样品信息维护")
@Slf4j
public class SampleController {

	@Autowired
	private SampleService sampleService;

	/**
	 * @author WangZL
	 * @Description 获取样品列表
	 * @param 样品ID，样品编号，样品单编号，规格型号，来样方式，粮食等级编码，粮食等级名称，粮食性质编码，粮食性质名称，样品状态标识，样品所属库点编码，样品所属库点名称，检测开始时间，检测结束时间
	 * @return 样品列表
	 */
	@ApiOperation(value = "样品列表", notes = "参数：样品ID，样品编号，样品单编号，规格型号，来样方式，粮食等级编码，粮食等级名称，粮食性质编码，粮食性质名称，样品状态标识，样品所属库点编码，样品所属库点名称，检测开始时间，检测结束时间")
	@PostMapping(value = "/sampleList")
    public ResponseWrapper getSampleList(@RequestBody SampleQueryVO sampleVO){
        try {
        	List<SampleVO> sampleVOs = sampleService.getSampleList(sampleVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), sampleVOs);
        } 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 = "/samplePage/{pageNum}/{pageSize}")
    public ResponseWrapper getSamplePage(@RequestBody SampleQueryVO sampleVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<SampleVO> pageInfos = new PageInfo<SampleVO>(sampleService.getSamplePage(sampleVO,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 新增样品信息
	 * @param 样品信息实体 models
	 * @return 新增状态
	 */
	@PostMapping(value = "/sample")
	@ApiOperation(value = "新增样品信息", notes = "参数：样品编号，样品单编号")
    public ResponseWrapper insert(@RequestBody SampleVO sampleVO) {
		try{
			if(StringUtils.isEmpty(sampleVO.getYpbh())||StringUtils.isEmpty(sampleVO.getQydbh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			SampleVO sampleVOs = sampleService.insert(sampleVO);
			if(sampleVOs == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "新增样品失败，请重新输入", null);
			}
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), sampleVOs);
        } 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 样品信息对象 model
	 * @return 新增状态
	 */
	@PutMapping(value = "/sample")
	@ApiOperation(value = "更新样品信息", notes = "参数：样品id")
    public ResponseWrapper update(@RequestBody SampleVO sampleVO) {
		try{
			if(StringUtils.isEmpty(sampleVO.getYpid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = sampleService.update(sampleVO);
			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 WangZL
	 * @Description 根据样品ID删除样品信息
	 * @param 样品ID
	 * @return 状态
	 */
	@DeleteMapping(value="/sample/{ypid}")
	@ApiOperation(value = "根据样品ID删除样品信息", notes = "参数：样品ID")
	public ResponseWrapper delete(@PathVariable("ypid") String ypid) {
		try{
			int x = sampleService.delete(ypid);
			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(), "删除失败,请样品ID是否正确", null);
			}
        } 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 根据样品ID获取样品信息
	 * @param 样品ID
	 * @return 状态
	 */
	@GetMapping(value="/sample/{ypid}")
	@ApiOperation(value = "根据样品ID获取样品信息", notes = "参数：样品ID")
	public ResponseWrapper getSampleInfoByypid(@PathVariable("ypid") String ypid) {
		try{
			SampleVO sampleVO = sampleService.getSampleInfoByypid(ypid);
			if(sampleVO == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "获取失败，样品不存在", null);
			}
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), sampleVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }


	/**
	 * 根据样品单ID获取样品信息
	 * @param ypdbh
	 * @return
	 */
	@GetMapping(value="/sampleByydbh/{ypdbh}")
	@ApiOperation(value = "根据样品单ID获取样品信息", notes = "参数：样品单ID")
	public ResponseWrapper getSampleInfoByypdbh(@PathVariable("ypdbh") String ypdbh) {
		try{
			SampleVO sampleVO = sampleService.getSampleInfoByypdid(ypdbh);
			if(sampleVO == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "获取失败，样品不存在", null);
			}
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), sampleVO);
		}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 新增状态
	 */
	@PostMapping(value = "/batchSample")
	@ApiOperation(value = "批量新增样品信息", notes = "参数：样品编号，样品单编号")
    public ResponseWrapper batchInserts(@RequestBody List<SampleVO> sampleVO) {
		try{
			for(int t=0;t<sampleVO.size();t++) {
				if(StringUtils.isEmpty(sampleVO.get(t).getYpbh())||StringUtils.isEmpty(sampleVO.get(t).getQydbh())) {
					return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
				}
			}

			int sampleVOs = sampleService.batchInserts(sampleVO);
			if(sampleVOs == 0) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "新增样品失败，请重新输入", null);
			}
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), sampleVOs);

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

}
