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.RestController;

import com.github.pagehelper.PageInfo;
import com.unis.common.utils.ResponseWrapper;
import com.unis.base.common.vo.DKdxxsjyVO;
import com.unis.base.server.entity.DKdxxsjyDO;
import com.unis.base.server.service.DKdxxsjyService;

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

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

	@Autowired
	private DKdxxsjyService dKdxxsjyService;

	/**
	 * @author huangxing
	 * @Description 获取库点列表
	 * @return 库点信息列表
	 */
	@ApiOperation(value = "获取库点信息列表", notes = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@PostMapping(value = "/sinkpointList")
    public ResponseWrapper getDKdxxsjyList(@RequestBody DKdxxsjyVO dKdxxsjyVO){
        try {
        	List<DKdxxsjyVO> dKdxxsjyDOs = dKdxxsjyService.getDKdxxsjyList(dKdxxsjyVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	@ApiOperation(value = "获取库点信息列表-储备粮-发货单位", notes = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@PostMapping(value = "/sinkpointListJQ")
	public ResponseWrapper sinkpointListJQ(@RequestBody DKdxxsjyVO dKdxxsjyVO){
		try {
			List<DKdxxsjyVO> dKdxxsjyDOs = dKdxxsjyService.sinkpointListJQ(dKdxxsjyVO);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDOs);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	@ApiOperation(value = "获取库点信息列表-储备粮-瘦货单位-LDQ", notes = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@PostMapping(value = "/sinkpointListLDQ")
	public ResponseWrapper sinkpointListLDQ(@RequestBody DKdxxsjyVO dKdxxsjyVO){
		try {
			List<DKdxxsjyVO> dKdxxsjyDOs = dKdxxsjyService.sinkpointListLDQ(dKdxxsjyVO);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDOs);
		} 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 = "获取库点信息列表,带组织机构id", notes = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@PostMapping(value = "/selectKdxx")
	public ResponseWrapper selectKdxx(@RequestBody DKdxxsjyVO dKdxxsjyVO){
		try {
			List<DKdxxsjyVO> dKdxxsjyDOs = dKdxxsjyService.selectKdxx(dKdxxsjyVO);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDOs);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	@ApiOperation(value = "获取库点信息列表,带组织机构id", notes = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@PostMapping(value = "/selectKdxxLgd")
	public ResponseWrapper selectKdxxLgd(@RequestBody DKdxxsjyVO dKdxxsjyVO){
		try {
			List<DKdxxsjyVO> dKdxxsjyDOs = dKdxxsjyService.selectKdxxLgd(dKdxxsjyVO);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDOs);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}
	@ApiOperation(value = "获取库点信息列表,带组织机构id(区储)", notes = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@PostMapping(value = "/selectKdxxLgdForQc")
	public ResponseWrapper selectKdxxLgdForQc(@RequestBody DKdxxsjyVO dKdxxsjyVO){
		try {
			List<DKdxxsjyVO> dKdxxsjyDOs = dKdxxsjyService.selectKdxxLgdForQc(dKdxxsjyVO);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDOs);
		} 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 = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@PostMapping(value = "/sinkpointTree")
	public ResponseWrapper getDKdxxsjyTree(@RequestBody DKdxxsjyVO dKdxxsjyVO){
		try {
			List<DKdxxsjyVO> dKdxxsjyDOs = dKdxxsjyService.getDKdxxsjyTree(dKdxxsjyVO);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDOs);
		} 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 = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@PostMapping(value = "/sinkpointPage/{pageNum}/{pageSize}")
    public ResponseWrapper getDKdxxsjyPage(@RequestBody DKdxxsjyVO dKdxxsjyVO,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        try {
        	PageInfo<DKdxxsjyVO> dKdxxsjyDOs = new PageInfo<DKdxxsjyVO>(dKdxxsjyService.getDKdxxsjyPage(dKdxxsjyVO,pageNum,pageSize));
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDOs);
        } 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 = "/sinkpoint/{kddm}")
    public ResponseWrapper getDKdxxsjyInfoBykddm(@PathVariable("kddm") String kddm){
        try {
        	DKdxxsjyDO dKdxxsjyDO = dKdxxsjyService.getDKdxxsjyInfoBykddm(kddm);
            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 WangZL
	 * @Description 根据统一社会信用代码获取库点信息列表
	 * @return 统一社会信用代码
	 */
	@ApiOperation(value = "获取库点信息列表", notes = "参数：统一社会信用代码")
	@GetMapping(value = "/sinkpointList/{tyshxydm}")
	public ResponseWrapper getDKdxxsjyInfoBytyshxydm(@PathVariable("tyshxydm") String tyshxydm){
		try {
			List<DKdxxsjyVO> dKdxxsjyVOList = dKdxxsjyService.getDKdxxsjyInfoBytyshxydm(tyshxydm);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyVOList);
		} 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 状态
	 */
	@DeleteMapping(value="/sinkpoint/{tyshxydm}")
	@ApiOperation(value = "根据统一社会信用代码删除库点信息", notes = "参数：统一社会信用代码")
	public ResponseWrapper deleteList(@PathVariable("tyshxydm") String tyshxydm) {
		try{
			int x = dKdxxsjyService.deleteList(tyshxydm);
			if(x>0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), 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 参数：库点信息实体 models
	 * @return 新增状态
	 */
	@PostMapping(value = "/sinkpoint")
	@ApiOperation(value = "新增库点信息", notes = "参数：库点名称，统一社会信用代码")
    public ResponseWrapper insert(@RequestBody DKdxxsjyVO dKdxxsjyVO) {
		try{
			if(StringUtils.isEmpty(dKdxxsjyVO.getKdmc())||StringUtils.isEmpty(dKdxxsjyVO.getTyshxydm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dKdxxsjyService.insert(dKdxxsjyVO);
			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 huangxing
	 * @Description 修改库点信息
	 * @param 参数：库点信息实体 models
	 * @return 新增状态
	 */
	@PutMapping(value = "/sinkpoint")
	@ApiOperation(value = "更新库点信息", notes = "参数：库点代码")
    public ResponseWrapper update(@RequestBody DKdxxsjyVO dKdxxsjyVO) {
		try{
			if(StringUtils.isEmpty(dKdxxsjyVO.getKddm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dKdxxsjyService.update(dKdxxsjyVO);
			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 参数：苦点编号
	 * @return 状态
	 */
	@DeleteMapping(value="/sinkpoint/{kddm}")
	@ApiOperation(value = "根据库点编号删除库点信息", notes = "参数：库点编号")
	public ResponseWrapper delete(@PathVariable("kddm") String kddm) {
		try{
			int x = dKdxxsjyService.delete(kddm);
			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(),"服务错误，请联系管理员");
    }
}
