package com.cim.idm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cim.idm.exception.GlobalException;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.model.BSERPlocation;
import com.cim.idm.model.CheckPlan1;
import com.cim.idm.model.StorageSpec;
import com.cim.idm.response.BaseResponse;
import com.cim.idm.response.RespGenerator;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.wmspackage.storage.StorageServiceProxy;
import com.cim.idm.wmspackage.storage.management.data.StorageSpecKey;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 货位控制器
 * @author ZXYGY17
 *
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/api/storage")
public class StorageController {


	@RequestMapping(value = "/getORG", method = RequestMethod.POST)
	public AjaxResult getORG(@RequestBody JSONObject in ){

		StorageSpec storageSpec = JSON.toJavaObject(in, StorageSpec.class);
		String storageName = storageSpec.getStorageName();
		//判断条码是否存在且状态是在库
		String sql = "SELECT T.ORGNO ERPFACTORY,T.ORGNAME DESCRIPTION FROM ORG T WHERE T.ORGNO IN ('101','102','103','105','106')  ORDER BY T.ORGNO";
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
//		bindMap.put("STORAGENAME",storageName); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到组织信息！");
		}
//		if(list.size() > 1) {
//			throw new GlobalException("货位不止一个,请联系IT处理！");
//		}
		return AjaxResult.me().setResultObj(list);

	}
	
	/**
	 * 获取货位信息
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getLocationName", method = RequestMethod.POST)
	public AjaxResult getLocationName(@RequestBody JSONObject in ){

		StorageSpec storageSpec = JSON.toJavaObject(in, StorageSpec.class);
		String storageName = storageSpec.getStorageName();
		//判断条码是否存在且状态是在库
		String sql = "SELECT m.STORAGENAME ,m.ERPFACTORY ,m.ERPLOCATION  FROM STORAGESPEC m WHERE m.STORAGENAME = :STORAGENAME";
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("STORAGENAME",storageName); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的货位记录！");
		}
		if(list.size() > 1) {
			throw new GlobalException("货位不止一个,请联系IT处理！");
		}
		return AjaxResult.me().setResultObj(list);

	}
	
	
	/**
	 * 通过仓库获取货位信息
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getLocationNameForERPLocation", method = RequestMethod.POST)
	public AjaxResult getLocationName2(@RequestBody JSONObject in ){

		StorageSpec storageSpec = JSON.toJavaObject(in, StorageSpec.class);
		String erplocation = storageSpec.getErpLocation();
//		String commitDate = storageSpec.getCommitDate();
		//判断条码是否存在且状态是在库
		String sql = "SELECT m.STORAGENAME ,m.DESCRIPTION || m.STORAGENAME DESCRIPTION  FROM STORAGESPEC m WHERE m.ERPLOCATION = :ERPLOCATION ORDER BY STORAGENAME ";
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("ERPLOCATION",erplocation); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到该仓库下的库位");
		}
		return AjaxResult.me().setResultObj(list);
	}
	/**
	 * 获取仓库信息
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getERPLocation", method = RequestMethod.POST)
	public AjaxResult getERPLocationName(@RequestBody JSONObject in ){

		BSERPlocation erplocation = JSON.toJavaObject(in, BSERPlocation.class);
		String erpfactory = erplocation.getERPFACTORYNAME();
		//判断条码是否存在且状态是在库
		String sql = "SELECT ERPLOCATIONNAME, DESCRIPTION ||' ' || ERPLOCATIONNAME AS  DESCRIPTION ,USE_LOCATION, DEPARTNAME   FROM BS_ERPLOCATION be  WHERE ERPFACTORYNAME =:ERPFACTORYNAME AND NCFLAG = 'Y' ORDER BY ERPFACTORYNAME ASC";
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("ERPFACTORYNAME",erpfactory); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到该组织下的仓库");
		}
		return AjaxResult.me().setResultObj(list);

	}
	
	/**
	 * 获取组织下仓库
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getERPLocationName", method = RequestMethod.POST)
	public AjaxResult getERPLocation(@RequestBody JSONObject in ){

		BSERPlocation erplocation = JSON.toJavaObject(in, BSERPlocation.class);
		String erpfactory = erplocation.getERPLOCATIONNAME();
		String sql = "	SELECT be.ERPLOCATIONNAME value,be.DESCRIPTION label FROM BS_ERPLOCATION be WHERE be.ERPFACTORYNAME = :ERPFACTORYNAME AND NCFLAG = 'Y' ";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("ERPFACTORYNAME",erpfactory); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if(Objects.isNull(list) || list.isEmpty()) {
			return AjaxResult.me().setSuccess(false).setMessage("未找组织下的仓库").setResultObj(null);
		}
		return AjaxResult.me().setResultObj(list);
	}
	
	/**
	 * 获取仓库下货位
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getLocationNameAndERPLocation", method = RequestMethod.POST)
	public AjaxResult getLocationName3(@RequestBody JSONObject in ){

		CheckPlan1 checkplan = JSON.toJavaObject(in, CheckPlan1.class);
		String erplocation = checkplan.getERPLOCATION();
		String sql = "SELECT s.DESCRIPTION,s.STORAGENAME FROM STORAGESPEC s WHERE s.ERPLOCATION = :ERPLOCATION";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("ERPLOCATION",erplocation); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			return AjaxResult.me().setSuccess(false).setMessage("未找到该仓库下的库位").setResultObj(null);
		}
		return AjaxResult.me().setResultObj(list);
	}
	
	/**
	 * 通过组别获取组织列表
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getERPFactoryByGroup", method = RequestMethod.POST)
	public AjaxResult getERPFactoryByGroup(@RequestBody JSONObject in ){

		StorageSpec storageSpec = JSON.toJavaObject(in, StorageSpec.class);
		String locationgroup = storageSpec.getLOCATIONGROUP();
		//判断条码是否存在且状态是在库
		String sql = "SELECT m.STORAGENAME ,m.ERPFACTORY ,m.ERPLOCATION  FROM STORAGESPEC m WHERE m.STORAGENAME = :STORAGENAME";
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("STORAGENAME",locationgroup); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的货位记录！");
		}
		if(list.size() > 1) {
			throw new GlobalException("货位不止一个,请联系IT处理！");
		}
		return AjaxResult.me().setResultObj(list);
	}
	
	/**
	 * 获取库区
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getArea", method = RequestMethod.POST)
	public AjaxResult getArea(@RequestBody JSONObject in ){
		//判断条码是否存在且状态是在库
		String sql = "SELECT a.AREANAME AS \"value\" ,a.DESCRIPTION AS \"label\" FROM AREA a ";
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		return AjaxResult.me().setResultObj(list);
	}
	
	/**
	 * 获取库区2
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getArea2", method = RequestMethod.POST)
	public AjaxResult getArea2(@RequestBody JSONObject in ){
		//判断条码是否存在且状态是在库
		String sql = "SELECT a.AREANAME AS \"value\" ,a.DESCRIPTION AS \"text\" FROM AREA a ";
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		return AjaxResult.me().setResultObj(list);
	}

	@RequestMapping(value = "/addStorage", method = RequestMethod.POST)
	public BaseResponse<Object> addStorage(@RequestBody Map<String, Object> param) {
		// 获取参数
		String storageName = (String) param.get("STORAGENAME");
		String description = (String) param.get("DESCRIPTION");
		String siteName = (String) param.get("SITENAME");
		String erpFactory = (String) param.get("ERPFACTORY");
		// 异常
		StorageSpecKey storageSpecKey = new StorageSpecKey();
		storageSpecKey.setStorageName(storageName);
		storageSpecKey.setSiteName(siteName);
		com.cim.idm.wmspackage.storage.management.data.StorageSpec storageSpec = null;
		try {
			storageSpec = StorageServiceProxy.getStorageSpecService().selectByKey(storageSpecKey);
			if (Objects.nonNull(storageSpec)) {
				return RespGenerator.returnError("已存在该货位！");
			}
		} catch (Exception ignored) {

		}
		storageSpec = new com.cim.idm.wmspackage.storage.management.data.StorageSpec();
		// 构建数据
		storageSpec.setKey(storageSpecKey);
		storageSpec.setDescription(description);
		storageSpec.setErpFactory(erpFactory);
		// 插入数据
		StorageServiceProxy.getStorageSpecService().insert(storageSpec);
		return RespGenerator.returnOK(param);
	}

	@RequestMapping(value = "/editStorage", method = RequestMethod.POST)
	public BaseResponse<Object> editStorage(@RequestBody Map<String, Object> param) {
		// 获取参数
		String storageName = (String) param.get("STORAGENAME");
		String siteName = (String) param.get("SITENAME");
		String description = (String) param.get("DESCRIPTION");
		// 获取数据
		StorageSpecKey storageSpecKey = new StorageSpecKey();
		storageSpecKey.setStorageName(storageName);
		storageSpecKey.setSiteName(siteName);
		com.cim.idm.wmspackage.storage.management.data.StorageSpec storageSpec = null;
		try {
			storageSpec = StorageServiceProxy.getStorageSpecService().selectByKey(storageSpecKey);
		} catch (Exception e) {
			return RespGenerator.returnError("未找到该货位！");
		}
		// 构建数据
		storageSpec.setDescription(description);
		// 更新数据
		StorageServiceProxy.getStorageSpecService().update(storageSpec);
		return RespGenerator.returnOK(param);

	}

	@RequestMapping(value = "/delStorage", method = RequestMethod.POST)
	public BaseResponse<Map<String, Object>> delStorage(@RequestBody Map<String, Object> param) {
		// 获取参数
		String storageName = (String) param.get("STORAGENAME");
		String siteName = (String) param.get("SITENAME");
		// 组建数据
		StorageSpecKey storageSpec = new StorageSpecKey();
		storageSpec.setStorageName(storageName);
		storageSpec.setSiteName(siteName);
		// 删除数据
		StorageServiceProxy.getStorageSpecService().delete(storageSpec);
		return RespGenerator.returnOK(param);
	}
}
