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.service.impl.MESServiceImpl;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPackingKey;
import com.cim.idm.wmspackage.materialpacking.management.info.SetEventInfo;
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.*;

/**
 * 货位控制器
 * @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 )  throws Exception {

		StorageSpec storageSpec = JSON.toJavaObject(in, StorageSpec.class);
		String erplocation = storageSpec.getErpLocation();
		//String erpLocation = storageSpec.getErpLocation();
		String areaName = "";
//		String commitDate = storageSpec.getCommitDate();
		//判断条码是否存在且状态是在库
		String sql = "SELECT m.STORAGENAME ,m.DESCRIPTION || m.STORAGENAME DESCRIPTION  FROM STORAGESPEC m WHERE m.ERPLOCATION = :ERPLOCATION ORDER BY STORAGENAME ";
		/*String sql = " SELECT\n" +
				"\tm.STORAGENAME,\n" +
				"\tm.STORAGENAME || ' ' || m.DESCRIPTION DESCRIPTION \n" +
				"FROM\n" +
				"\tSTORAGESPEC m \n" +
				"WHERE\n" +
				"\tm.SITENAME =:SITENAME\n" +
				"\tAND m.AREANAME =:AREANAME\n" +
				"ORDER BY\n" +
				"\tSTORAGENAME ";*/
		/*
		String sql = " SELECT * FROM ( " +
				" SELECT\n" +
				"\tm.STORAGENAME,\n" +
				"\tm.STORAGENAME || ' ' || m.DESCRIPTION DESCRIPTION \n" +
				"FROM\n" +
				"\tSTORAGESPEC m \n" +
				"WHERE\n" +
				"\tm.SITENAME =:SITENAME\n" +
				"\tAND m.AREANAME =:AREANAME\n" +
				"ORDER BY\n" +
				"\tSTORAGENAME" +
				" ) WHERE ROWNUM <= 50 ";
		*/
		/*
		switch (erpLocation) {
			case "1010":
			case "1020":
				areaName = "SH";
				break;
			case "1030":
				areaName = "TC";
				break;
			case "1050":
				areaName = "CQ";
				break;
			case "1060":
				areaName = "DG";
				break;
			case "3500":
				areaName = "YN";
				break;
			default :
				break;
		}
		*/
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("ERPLOCATION",erplocation);
		/*
		bindMap.put("SITENAME","SDK");
		bindMap.put("AREANAME",areaName);
		*/
		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);
	}
}
