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.DCfxxsjyVO;
import com.unis.detectionnum.common.vo.MenuVO;
import com.unis.detectionnum.server.service.DCfxxsjyService;
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 DCfxxsjyController
 * @ProjectName 仓房信息 Controller
 * @Description  TODO
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "仓房信息", tags = "仓房信息维护")
@Slf4j
public class WarehouseController {
	
	@Autowired
	private DCfxxsjyService dCfxxsjyService;

	@ApiOperation(value = "仓房信息-带状态更新", notes = "参数：仓房编号")
	@PutMapping(value = "/warehouseUpdate")
	public ResponseWrapper warehouseUpdate(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
		try{
			if(StringUtils.isEmpty(dCfxxsjyVO.getCfbh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dCfxxsjyService.warehouseUpdate(dCfxxsjyVO);
			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 获取仓房列表
	 * @return 仓房信息列表
	 */
	@ApiOperation(value = "获取仓房信息列表", notes = "参数：仓房编号，仓房名称，库点编号，库点名称，仓房类型，建筑类型，仓房状态")
	@RequestMapping(value = "/warehouseAll", method = RequestMethod.GET)
    public ResponseWrapper getDCfxxxsjyList(String kddm){
        try {
			DCfxxsjyVO dCfxxsjyVO = new DCfxxsjyVO();
			dCfxxsjyVO.setKddm(kddm);
        	List<DCfxxsjyVO> dCfxxsjyDOs = dCfxxsjyService.getDCfxxxsjyList(dCfxxsjyVO);
			ArrayList<HashMap<String, String>> objects = Lists.newArrayList();
			if (dCfxxsjyDOs.size()>0){
				for (DCfxxsjyVO cf:dCfxxsjyDOs) {
					HashMap<String, String> map = Maps.newHashMap();
					map.put("cfmc",cf.getCfmc());
					map.put("cfbh",cf.getCfbh());
					objects.add(map);
				}
			}
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), objects);
        } 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 企业信息列表
	 */
	@ApiOperation(value = "分页获取仓房信息列表", notes = "参数：仓房编号，仓房名称，库点编号，库点名称，仓房类型，建筑类型，仓房状态")
	@RequestMapping(value = "/warehousePage", method = RequestMethod.GET)
	public ResponseWrapper warehousePage(String kddm,String cfbh, String userCode, Integer pageNum,Integer pageSize) {
		try {
			if(StringUtils.isEmpty(kddm)) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			DCfxxsjyVO dCfxxsjyVO = new DCfxxsjyVO();
			dCfxxsjyVO.setKddm(kddm);
			dCfxxsjyVO.setCfbh(cfbh);
			PageInfo<DCfxxsjyVO> pageInfos = new PageInfo<DCfxxsjyVO>(dCfxxsjyService.getDCfxxxsjyPage(dCfxxsjyVO,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 huangxing
	 * @Description 根据仓房编码获取仓房信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "根据仓房编码获取仓房信息", notes = "参数：仓房编号")
	@RequestMapping(value = "/warehouse/getBycfbh", method = RequestMethod.GET)
	public ResponseWrapper getBycfbh(String cfbh) {
		try {
			if(StringUtils.isEmpty(cfbh)) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			DCfxxsjyVO dCfxxsjyVO = dCfxxsjyService.getDCfxxxsjyInfoBycfbh(cfbh);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyVO);
        } 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 = "/warehouse/update", method = RequestMethod.POST)
	public ResponseWrapper update(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
		try{
			if(StringUtils.isEmpty(dCfxxsjyVO.getCfbh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dCfxxsjyService.update(dCfxxsjyVO);
			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 WangZL
	 * @Description 更新仓房信息
	 * @return 库点信息
	 */

	
	/**
	 * @author huangxing
	 * @Description 删除企业信息
	 * @param 参数：社会行政代码
	 * @return 状态
	 */
	@RequestMapping(value = "/warehouse/delete", method = RequestMethod.POST)
	@ApiOperation(value = "根据仓房编号删除仓房信息", notes = "参数：仓房编号")
	public ResponseWrapper delete(String cfbh) {
		try{
			int x = dCfxxsjyService.delete(cfbh);
			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 huangxing
	 * @Description 新增仓房信息
	 * @param 仓房信息实体 models
	 * @return 新增状态
	 */
	@RequestMapping(value = "/warehouse/insert", method = RequestMethod.POST)
	@ApiOperation(value = "新增仓房信息", notes = "参数：仓房名称，苦点代码")
    public ResponseWrapper insert(@RequestBody DCfxxsjyVO dAjxxsjyVO) {
		try{
			if(/*StringUtils.isEmpty(dAjxxsjyVO.getCfbh())||*/StringUtils.isEmpty(dAjxxsjyVO.getKddm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dCfxxsjyService.insert(dAjxxsjyVO);
			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(),"服务错误，请联系管理员");
    }

    /* ==========================================================end=========================================================================== */
	/**
	 * @author WangZL
	 * @Description 根据库点代码删除仓房信息列表
	 * @param 库点代码
	 * @return 状态
	 */
	@DeleteMapping(value="/warehouseList/{kddm}")
	@ApiOperation(value = "根据库点代码删除仓房信息列表", notes = "参数：库点代码")
	public ResponseWrapper deleteList(@PathVariable("kddm")String kddm) {
		try{
			int x = dCfxxsjyService.deleteList(kddm);
			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 WangZL
	 * @Description 根据库点代码获取仓房列表
	 * @param 库点代码
	 * @return 仓房信息列表
	 */
	@ApiOperation(value = "仓房信息-根据库点代码获取仓房列表", notes = "参数：库点代码")
	@PostMapping(value = "/warehouseListByKddm/{kddm}")
	public ResponseWrapper getWarehouseListByKddm(@PathVariable("kddm")String kddm){
		try {
			if (kddm == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			List<DCfxxsjyVO> dCfxxsjyVOList = dCfxxsjyService.getWarehouseListByKddm(kddm);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dCfxxsjyVOList);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * 人员信息-备案主表状态
	 * @param dCfxxsjyVO
	 * @return
	 */
	@PostMapping(value = "/warehouseFiling")
	@ApiOperation(value = "仓房信息-备案主表状态", notes = "参数：仓房编号，是否备案")
	public ResponseWrapper updateFiling(@RequestBody DCfxxsjyVO dCfxxsjyVO) {
		try {
			if (StringUtils.isEmpty(dCfxxsjyVO.getCfbh()) || dCfxxsjyVO.getSfba() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int i = dCfxxsjyService.updateFiling(dCfxxsjyVO);
			if (i > 0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
			} else {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "操作失败，请联系管理员");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
		}
	}
	
	/**
	 * 菜单栏  直属库-库点-仓房
	 * @param userCode
	 * @return
	 */
	@ApiOperation(value = "菜单栏  直属库-库点-仓房", notes = "参数：库点名称")
	@RequestMapping(value = "/menuList/{userCode}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseWrapper getMenuList(@PathVariable("userCode") String userCode) {
		try {

			List<MenuVO> menus =  dCfxxsjyService.getMenuList(userCode);
			//如果说一个用户对应一个仓房 需要将用户下cfbh去关联
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), menus);

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