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.base.common.vo.TySltpxxVO;
import com.unis.base.server.service.TySltpxxService;
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 FoodPicturesController {

	@Autowired
	private TySltpxxService tySltpxxService;

	/**
	 * @author zhengbodang
	 * @Description 获取涉粮图片信息列表
	 * @param
	 * @return
	 */
	@ApiOperation(value = "涉粮图片信息列表", notes = "参数：")
	@PostMapping(value = "/foodPicturesList")
    public ResponseWrapper getTySltpxxList(@RequestBody TySltpxxVO tySltpxxVO){
        try {
        	List<TySltpxxVO> tySltpxxVOs = tySltpxxService.getTySltpxxList(tySltpxxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tySltpxxVOs);
        } 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 = "/foodPicturesPage/{pageNum}/{pageSize}")
    public ResponseWrapper getTySltpxxPage(@RequestBody TySltpxxVO tySltpxxVO ,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try{
			PageInfo<TySltpxxVO> pageInfos = new PageInfo<TySltpxxVO>(tySltpxxService.getTySltpxxPage(tySltpxxVO,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 新增涉粮图片信息
	 * @return 新增状态
	 */
	@PostMapping(value = "/foodPictures")
	@ApiOperation(value = "新增涉粮图片信息", notes = "参数：所属库点代码，所属企业代码")
    public ResponseWrapper insert(@RequestBody TySltpxxVO tySltpxxVO) {
		try{
			if(StringUtils.isEmpty(tySltpxxVO.getSskddm())||StringUtils.isEmpty(tySltpxxVO.getSsqydm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tySltpxxService.insert(tySltpxxVO);
			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
	 */
	@PutMapping(value = "/foodPictures")
	@ApiOperation(value = "更新涉粮图片信息", notes = "参数：图片id")
    public ResponseWrapper update(@RequestBody TySltpxxVO tySltpxxVO) {
		try{
			if(StringUtils.isEmpty(tySltpxxVO.getTpid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = tySltpxxService.update(tySltpxxVO);
			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="/foodPictures/{tpid}")
	@ApiOperation(value = "删除涉粮图片信息", notes = "参数：")
	public ResponseWrapper delete(@PathVariable("tpid") String tpid) {
		try{
			int x = tySltpxxService.delete(tpid);
			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
	 * @param
	 * @return 状态
	 */
	@GetMapping(value="/foodPictures/{tpid}")
	@ApiOperation(value = "号获取涉粮图片信息", notes = "参数：")
	public ResponseWrapper getDAjxxsjyByajbh(@PathVariable("tpid") String tpid) {
		try{
			TySltpxxVO tySltpxxVO = tySltpxxService.getSltpxxBySltpxxid(tpid);
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tySltpxxVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
}
