package com.unis.detectionnum.server.controller;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.unis.common.utils.ResponseWrapper;
import com.unis.detectionnum.common.vo.DKdxxsjyVO;
import com.unis.detectionnum.server.entity.DKdxxsjyDO;
import com.unis.detectionnum.server.service.DKdxxsjyService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author huangxing
 * @version 1.0
 * @Title DKdxxsjyController
 * @ProjectName 库点信息 Controller
 * @Description  TODO
 * @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 = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@RequestMapping(value = "/sinkpointAll/{userCode}", method = RequestMethod.GET)
    public ResponseWrapper getDKdxxsjyList(@PathVariable("userCode") String userCode){
        try {
        	List<DKdxxsjyDO> child = dKdxxsjyService.getDKdxxsjyAll(userCode);
			ArrayList<HashMap<String, String>> objects = Lists.newArrayList();
			if (child.size()>0){
				for (DKdxxsjyDO r:child) {
					HashMap<String, String> map = Maps.newHashMap();
					map.put("kddm",r.getKddm());
					map.put("kdmc",r.getKdmc());
					objects.add(map);
				}
			}
			return ResponseWrapper.success( objects );
        } 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 分页获取库点列表
	 * @return 库点信息列表
	 */
	@ApiOperation(value = "分页获取库点信息列表", notes = "参数：库点名称，粮库类别，企业编号，企业名称，库点企业性质")
	@RequestMapping(value = "/sinkpointPage", method = RequestMethod.GET)
	public ResponseWrapper getDKdxxsjyPage(String userCode, Integer pageNum,Integer pageSize){
		try {
			DKdxxsjyVO dKdxxsjyVO= new DKdxxsjyVO();
			if(userCode.equals("U0000777")){
				userCode = "";
			}
			dKdxxsjyVO.setUserCode(userCode);
			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/getBykddm")
	public ResponseWrapper getDKdxxsjyInfoBykddm(String kddm){
		try {
			DKdxxsjyVO dKdxxsjyVO = dKdxxsjyService.getDKdxxsjyInfoBykddm(kddm);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyVO);
		} 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 新增状态
	 */
	@RequestMapping(value = "/sinkpoint/insert", method = RequestMethod.POST)
	@ApiOperation(value = "新增库点信息", notes = "参数：库点名称，统一社会信用代码")
    public ResponseWrapper insert(@RequestBody DKdxxsjyVO dKdxxsjyVO) {
		try{
			if (StringUtils.isEmpty(dKdxxsjyVO.getTyshxydm())){
				dKdxxsjyVO.setTyshxydm("100000001");
			}
			if(StringUtils.isEmpty(dKdxxsjyVO.getKdmc())) {
				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 新增状态
	 */
	@RequestMapping(value = "/sinkpoint/update", method = RequestMethod.POST)
	@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 状态
	 */
	@RequestMapping(value = "/sinkpoint/delete", method = RequestMethod.POST)
	@ApiOperation(value = "根据库点编号删除库点信息", notes = "参数：库点编号")
	public ResponseWrapper delete(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(),"服务错误，请联系管理员");
    }
}
