package com.uas.eis.serviceImpl;

import com.uas.eis.core.config.SpObserver;
import com.uas.eis.dao.BaseDao;
import com.uas.eis.dto.DataCenter;
import com.uas.eis.entity.ErrorMessage;
import com.uas.eis.exception.ApiSystemException;
import com.uas.eis.sdk.entity.ApiResult;
import com.uas.eis.service.MESDataService;
import com.uas.eis.utils.BaseUtil;
import com.uas.eis.utils.Constant;
import com.uas.eis.utils.DateUtil;
import com.uas.eis.utils.SqlUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MESDataServiceImpl implements MESDataService {

	@Autowired
	private BaseDao baseDao;

	@Value("${spring.datasource.username}")
	private String username;

	@Override
	public List<Map<Object,Object>> snStepPass(String accessKey, String requestId, String data) {
		String AE_MASTER = checkAccessKey(accessKey, requestId);
		List<Map<Object, Object>> maps = BaseUtil.parseGridStoreToMaps(data);
		SpObserver.putSp(AE_MASTER);
		List<Map<Object, Object>> relist = new ArrayList<>();
		for (Map<Object, Object> map : maps) {
			Map<Object, Object> remap = new HashMap<>();
			String remark = "";
			int cn = baseDao.getJdbcTemplate().queryForObject("select count(1) from TEMP_STEPPASSED where sp_id=? and nvl(t_status,'W')='W' ",Integer.class,map.get("SP_ID"));
			remap.put("id",map.get("SP_ID"));
			if(cn>0){
				map.put("T_STATUS","D");
				map.put("T_REMAKR","重复");
				remark = "重复";
			}else{
				map.put("T_STATUS","W");
			}
			map.put("T_KEY",accessKey);
			map.put("T_INDATE", Timestamp.valueOf(DateUtil.currentDateString(Constant.YMD_HMS)));
			try {
				baseDao.execute(SqlUtil.getInsertSqlByMap(map,"TEMP_STEPPASSED"));
				remap.put("result","OK");
				remap.put("remark",remark);
			}catch (Exception e){
				remap.put("result","NG");
				remap.put("remark",e.getMessage());
				e.printStackTrace();
			}
			relist.add(remap);
		}
		//baseDao.execute(SqlUtil.getInsertSqlbyGridStore(maps, "TEMP_STEPPASSED"));
		//baseDao.execute("update temp_steppassed set t_status='D',T_REMAKR='重复' where t_id in (select t_id from (select temp_steppassed.*,row_number()over(partition by sp_id order by t_id desc) cn from temp_steppassed where nvl(t_status,'W')='W' ) where cn>1)");
		SpObserver.putSp(username);
		return relist;
	}

	@Override
	public List<Map<Object,Object>> snInfo(String accessKey, String requestId, String data) {
		String AE_MASTER = checkAccessKey(accessKey, requestId);
		List<Map<Object, Object>> maps = BaseUtil.parseGridStoreToMaps(data);
		SpObserver.putSp(AE_MASTER);
		List<Map<Object, Object>> relist = new ArrayList<>();
		for (Map<Object, Object> map : maps) {
			Map<Object, Object> remap = new HashMap<>();
			String remark = "";
			int cn = baseDao.getJdbcTemplate().queryForObject("select count(1) from TEMP_SNINFO where SI_ID=? and nvl(t_status,'W')='W' ",Integer.class,map.get("SI_ID"));
			remap.put("id",map.get("SI_ID"));
			if(cn>0){
				map.put("T_STATUS","D");
				map.put("T_REMAKR","重复");
				remark = "重复";
			}else{
				map.put("T_STATUS","W");
			}
			map.put("T_KEY",accessKey);
			map.put("T_INDATE", Timestamp.valueOf(DateUtil.currentDateString(Constant.YMD_HMS)));
			try {
				baseDao.execute(SqlUtil.getInsertSqlByMap(map,"TEMP_SNINFO"));
				remap.put("result","OK");
				remap.put("remark",remark);
			}catch (Exception e){
				remap.put("result","NG");
				remap.put("remark",e.getMessage());
				e.printStackTrace();
			}
			relist.add(remap);
		}
		SpObserver.putSp(username);
		return relist;
	}

	@Override
	public List<Map<Object,Object>> makeSnRelation(String accessKey, String requestId, String data) {
		String AE_MASTER = checkAccessKey(accessKey, requestId);
		List<Map<Object, Object>> maps = BaseUtil.parseGridStoreToMaps(data);
		SpObserver.putSp(AE_MASTER);
		List<Map<Object, Object>> relist = new ArrayList<>();
		for (Map<Object, Object> map : maps) {
			Map<Object, Object> remap = new HashMap<>();
			String remark = "";
			int cn = baseDao.getJdbcTemplate().queryForObject("select count(1) from TEMP_MAKESNRELATION where ID=? and nvl(t_status,'W')='W' ",Integer.class,map.get("ID"));
			remap.put("id",map.get("ID"));
			if(cn>0){
				map.put("T_STATUS","D");
				map.put("T_REMAKR","重复");
				remark = "重复";
			}else{
				map.put("T_STATUS","W");
			}
			map.put("T_KEY",accessKey);
			map.put("T_INDATE", Timestamp.valueOf(DateUtil.currentDateString(Constant.YMD_HMS)));
			try {
				baseDao.execute(SqlUtil.getInsertSqlByMap(map,"TEMP_MAKESNRELATION"));
				remap.put("result","OK");
				remap.put("remark",remark);
			}catch (Exception e){
				remap.put("result","NG");
				remap.put("remark",e.getMessage());
				e.printStackTrace();
			}
			relist.add(remap);
		}
		SpObserver.putSp(username);
		return relist;
	}

	@Override
	public List<Map<Object,Object>> MAKEBAD(String accessKey, String requestId, String data) {
		String AE_MASTER = checkAccessKey(accessKey, requestId);
		List<Map<Object, Object>> maps = BaseUtil.parseGridStoreToMaps(data);
		SpObserver.putSp(AE_MASTER);
		List<Map<Object, Object>> relist = new ArrayList<>();
		for (Map<Object, Object> map : maps) {
			Map<Object, Object> remap = new HashMap<>();
			String remark = "";
			int cn = baseDao.getJdbcTemplate().queryForObject("select count(1) from TEMP_MAKEBAD where MB_CODE=? and nvl(t_status,'W')='W' ",Integer.class,map.get("MB_CODE"));
			remap.put("id",map.get("MB_CODE"));
			if(cn>0){
				map.put("T_STATUS","D");
				map.put("T_REMAKR","重复");
				remark = "重复";
			}else{
				map.put("T_STATUS","W");
			}
			map.put("T_KEY",accessKey);
			map.put("T_INDATE", Timestamp.valueOf(DateUtil.currentDateString(Constant.YMD_HMS)));
			try {
				baseDao.execute(SqlUtil.getInsertSqlByMap(map,"TEMP_MAKEBAD"));
				remap.put("result","OK");
				remap.put("remark",remark);
			}catch (Exception e){
				remap.put("result","NG");
				remap.put("remark",e.getMessage());
				e.printStackTrace();
			}
			relist.add(remap);
		}
		SpObserver.putSp(username);
		return relist;
	}

	@Override
	public List<Map<Object,Object>> makeBadReason(String accessKey, String requestId, String data) {
		String AE_MASTER = checkAccessKey(accessKey, requestId);
		List<Map<Object, Object>> maps = BaseUtil.parseGridStoreToMaps(data);
		SpObserver.putSp(AE_MASTER);
		List<Map<Object, Object>> relist = new ArrayList<>();
		for (Map<Object, Object> map : maps) {
			Map<Object, Object> remap = new HashMap<>();
			String remark = "";
			int cn = baseDao.getJdbcTemplate().queryForObject("select count(1) from TEMP_MAKEBADREASON where MBR_ID=? and nvl(t_status,'W')='W' ",Integer.class,map.get("MBR_ID"));
			remap.put("id",map.get("MBR_ID"));
			if(cn>0){
				map.put("T_STATUS","D");
				map.put("T_REMAKR","重复");
				remark = "重复";
			}else{
				map.put("T_STATUS","W");
			}
			map.put("T_KEY",accessKey);
			map.put("T_INDATE", Timestamp.valueOf(DateUtil.currentDateString(Constant.YMD_HMS)));
			try {
				baseDao.execute(SqlUtil.getInsertSqlByMap(map,"TEMP_MAKEBADREASON"));
				remap.put("result","OK");
				remap.put("remark",remark);
			}catch (Exception e){
				remap.put("result","NG");
				remap.put("remark",e.getMessage());
				e.printStackTrace();
			}
			relist.add(remap);
		}
		SpObserver.putSp(username);
		return relist;
	}

	@Override
	public List<Map<Object,Object>> packageDetail(String accessKey, String requestId, String data) {
		String AE_MASTER = checkAccessKey(accessKey, requestId);
		List<Map<Object, Object>> maps = BaseUtil.parseGridStoreToMaps(data);
		SpObserver.putSp(AE_MASTER);
		List<Map<Object, Object>> relist = new ArrayList<>();
		for (Map<Object, Object> map : maps) {
			Map<Object, Object> remap = new HashMap<>();
			String remark = "";
			int cn = baseDao.getJdbcTemplate().queryForObject("select count(1) from TEMP_PACKAGEDETAIL where PD_ID=? and nvl(t_status,'W')='W' ",Integer.class,map.get("MBR_ID"));
			remap.put("id",map.get("PD_ID"));
			if(cn>0){
				map.put("T_STATUS","D");
				map.put("T_REMAKR","重复");
				remark = "重复";
			}else{
				map.put("T_STATUS","W");
			}
			map.put("T_KEY",accessKey);
			map.put("T_INDATE", Timestamp.valueOf(DateUtil.currentDateString(Constant.YMD_HMS)));
			try {
				baseDao.execute(SqlUtil.getInsertSqlByMap(map,"TEMP_PACKAGEDETAIL"));
				remap.put("result","OK");
				remap.put("remark",remark);
			}catch (Exception e){
				remap.put("result","NG");
				remap.put("remark",e.getMessage());
				e.printStackTrace();
			}
			relist.add(remap);
		}
		SpObserver.putSp(username);
		return relist;
	}

	private String checkAccessKey(String accessKey,String requestId){
		Object accessSecret_O = baseDao.getFieldDataByCondition("APIEMPLOYEE", "AE_SECRET", "AE_KEY='" + accessKey + "'");
		Object AE_MASTER = baseDao.getFieldDataByCondition("APIEMPLOYEE", "AE_MASTER", "AE_KEY='" + accessKey + "'");
		String accessSecret = accessSecret_O == null ? "" : accessSecret_O.toString();
		// 检查KEY是否合理
		if (StringUtils.isEmpty(accessKey) || StringUtils.isEmpty(accessSecret) || AE_MASTER== null || "".equals(AE_MASTER.toString())) {
			ApiResult apiResult = new ApiResult();
			apiResult.setCode(ErrorMessage.ACCESSKEY_ILLEGAL.getCode());
			apiResult.setMessage(ErrorMessage.ACCESSKEY_ILLEGAL.getMessage());
			apiResult.setRequestId(requestId);
			throw new ApiSystemException(apiResult);
		}
		return AE_MASTER.toString();
	}

}
