package com.foreverwin.mesnac.common.service.impl;

import com.foreverwin.mesnac.common.mapper.CustomReportConfigMapper;
import com.foreverwin.mesnac.common.model.*;
import com.foreverwin.mesnac.common.service.CustomReportConfigInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**  
 * 
 * @Title:  CustomReportConfigImpl.java   
 * @Package com.knsdev.service.impl   
 * @Description: 自定义报表配置 Interface 实现    
 * @author: Andy    
 * @date:   2020年10月14日 上午9:57:38   
 * @version V1.0 
 *
 * @History 操作记录
 * 2020年10月14日 上午9:57:38 Andy add
 *
 */
@Service
public class CustomReportConfigImpl implements CustomReportConfigInterface {

	@Autowired
	private CustomReportConfigMapper customReportConfigMapper;


	@Override
	public CustomReportConfig Query(String reportid) throws Exception {
		//2020-11-6 andy, 因所有的 site共用配置，所以此处site直接给*
		String site="*";
	 	CustomReportConfig config=new CustomReportConfig();
	 	String mainHandle="CustomReportBO:"+site+","+reportid;

	 	Map<String, Object> param = new HashMap<String, Object>();
	 	param.put("SITE", site);
	 	param.put("REPORT_ID", reportid);
		        
		 //自定义报表_主要模块 数据
		List<CustomReportMainmodel> map = customReportConfigMapper.selectMain( site,reportid );
		 
		 if(map.size()<1)
		 {
			 throw new RuntimeException("查询无资料");
		 } else
		 {
			//获取 Z_CUSTOM_REPORT_MAINMODEL 表中相关字段
			 config.setHandle(map.get(0).getHandle()==null?"":map.get(0).getHandle().toString());
			 config.setSite(site);
			 config.setReportID(reportid);
			 config.setReportTitle(map.get(0).getReportTitle()==null?"":map.get(0).getReportTitle().toString().trim());
			 config.setReportDESC(map.get(0).getReportDesc()==null?"":map.get(0).getReportDesc().toString().trim());
			 config.setConditionSQLConfig(map.get(0).getConditionSqlConfig()==null?"":map.get(0).getConditionSqlConfig().toString().trim());
			 config.setDisplayType(map.get(0).getDisplayType()==null?"":map.get(0).getDisplayType().toString().trim());
			 config.setTimeRefresh(map.get(0).getTimeRefresh()==null?"0":map.get(0).getTimeRefresh().toString());
			 config.setBackgroundColor(map.get(0).getBackgroundColor()==null?"":map.get(0).getBackgroundColor().toString().trim());
			 config.setFontColor(map.get(0).getFontColor()==null?"":map.get(0).getFontColor().toString().trim());
			 config.setPageSize(map.get(0).getPageSize()==null?"0":map.get(0).getPageSize().toString());
			 config.setUser(map.get(0).getModifyUser()==null?"":map.get(0).getModifyUser().toString().trim());//map.get(0).get("USER")
		 }
		 
		 //自定义报表_检索条件配置 数据
		 
		 List<CustomReportSearchConditionConfig> conditionConfigList=new ArrayList<CustomReportSearchConditionConfig>();

		 List<CustomReportSearchConditionConfig> searchConditionMap = customReportConfigMapper.selectSearchConditionConfig( mainHandle );
		 
		 for(int i=0;i<searchConditionMap.size();i++)
		 {
			 CustomReportSearchConditionConfig searchConditionConfig=new CustomReportSearchConditionConfig();
			 searchConditionConfig.setSeq(searchConditionMap.get(i).getSeq()==null?0:searchConditionMap.get(i).getSeq());
			 searchConditionConfig.setConditionName(searchConditionMap.get(i).getConditionName()==null?"":searchConditionMap.get(i).getConditionName().toString());
			 searchConditionConfig.setConditionField(searchConditionMap.get(i).getConditionField()==null?"":searchConditionMap.get(i).getConditionField().toString());
			 searchConditionConfig.setFieldType(searchConditionMap.get(i).getFieldType()==null?"":searchConditionMap.get(i).getFieldType().toString());
			 searchConditionConfig.setDailogCheckboxSQL(searchConditionMap.get(i).getDailogCheckboxSQL()==null?"":searchConditionMap.get(i).getDailogCheckboxSQL().toString());
			 searchConditionConfig.setRequiredType(searchConditionMap.get(i).getRequiredType()==null?"":searchConditionMap.get(i).getRequiredType().toString());
			 searchConditionConfig.setRowID(searchConditionMap.get(i).getRowID()==null?1:searchConditionMap.get(i).getRowID());
			 searchConditionConfig.setColumnID(searchConditionMap.get(i).getColumnID()==null?1:searchConditionMap.get(i).getColumnID());
			 searchConditionConfig.setColumnWidthPercent(searchConditionMap.get(i).getColumnWidthPercent()==null?1:searchConditionMap.get(i).getColumnWidthPercent());
			 conditionConfigList.add(searchConditionConfig);
		 }
		 
		 config.setSearchConditionConfigList(conditionConfigList);
		 
		//自定义报表_检索条件动态SQL配置 
		 List<CustomReportSearchConditionDynSQLConfig> dynSQLConfigList=new ArrayList<CustomReportSearchConditionDynSQLConfig>();

         List<CustomReportSearchConditionDynSQLConfig> dynSqlConfigMap = customReportConfigMapper.selectDynSqlConfig( mainHandle );
		 
		 for(int i=0;i<dynSqlConfigMap.size();i++)
		 {
			 CustomReportSearchConditionDynSQLConfig dynSQLConfig=new CustomReportSearchConditionDynSQLConfig();
			 dynSQLConfig.setSeq(dynSqlConfigMap.get(i).getSeq()==null?"0":dynSqlConfigMap.get(i).getSeq().toString());
			 dynSQLConfig.setDynConditionReplaceField(dynSqlConfigMap.get(i).getDynConditionReplaceField()==null?"":dynSqlConfigMap.get(i).getDynConditionReplaceField().toString());
			 dynSQLConfig.setDynamicConditionSQL(dynSqlConfigMap.get(i).getDynamicConditionSQL()==null?"":dynSqlConfigMap.get(i).getDynamicConditionSQL().toString());
			 dynSQLConfig.setConditionField(dynSqlConfigMap.get(i).getConditionField()==null?"":dynSqlConfigMap.get(i).getConditionField().toString());
			 dynSQLConfigList.add(dynSQLConfig);
		 }
		 
		 config.setSearchConditionDynSQLConfigList(dynSQLConfigList);
		 
		 //自定义报表_检索结果列配置
		 List<CustomReportSearchResultColumnConfig> resultColumnConfigList=new ArrayList<CustomReportSearchResultColumnConfig>();

		 List<CustomReportSearchResultColumnConfig> resultColumnMap = customReportConfigMapper.selectResultColumnConfig( mainHandle );
		 
		 for(int i=0;i<resultColumnMap.size();i++)
		 {
			 CustomReportSearchResultColumnConfig resultColumnConfig=new CustomReportSearchResultColumnConfig();
			 resultColumnConfig.setSeq(resultColumnMap.get(i).getSeq()==null?"0":resultColumnMap.get(i).getSeq().toString());
			 resultColumnConfig.setColumnField(resultColumnMap.get(i).getColumnField()==null?"":resultColumnMap.get(i).getColumnField().toString());
			 resultColumnConfig.setColumnName(resultColumnMap.get(i).getColumnName()==null?"":resultColumnMap.get(i).getColumnName().toString().trim());
			 resultColumnConfig.setColumnWidthPercent(resultColumnMap.get(i).getColumnWidthPercent()==null?"1":resultColumnMap.get(i).getColumnWidthPercent().toString().trim());
			 resultColumnConfigList.add(resultColumnConfig);
		 }
		 
		 config.setSearchResultColumnList(resultColumnConfigList);

		 return config;
	}


	/* (non-Javadoc)
	 * @see com.knsdev.service.CustomReportConfigInterface#Save(com.knsdev.service.domain.CustomReportConfig)
	 */
	@Override
	public void Save(CustomReportConfig config) throws Exception {
		//2020-11-6 andy, 因所有的 site共用配置，所以此处site直接给*
		//String site=config.getSite().trim();
		String site="*";
		
		String reportID=config.getReportID().trim();
		
		if(site.length()<1 || reportID.length()<1)
		{
			//70501.simple=站点、报表作业编号均不能为空
			 //throw Exceptions.convert(new BasicBOBeanException(70501,new Data()));
			throw new RuntimeException("站点、报表作业编号均不能为空");
		}
		
		//1.删除 资料
		Delete(site, reportID);

		LocalDateTime dateTime = LocalDateTime.now();
		String user=config.getUser();
		String customReportBO="CustomReportBO:"+site+","+reportID;
		
		//2.保存资料至   Z_CUSTOM_REPORT_SEARCHCONDITION_CONFIG
		List<CustomReportSearchConditionConfig> configList=config.getSearchConditionConfigList();
		
		if(configList!=null && configList.size() > 0) {
			CustomReportSearchConditionConfig customReportSearchConditionConfig = new CustomReportSearchConditionConfig();
			customReportSearchConditionConfig.setSite( site );
			customReportSearchConditionConfig.setCustomReportMainModelBo( customReportBO );
			customReportSearchConditionConfig.setModifyDateTime( new Date() );
			customReportSearchConditionConfig.setModifyUser( user );
			
			for(int i=0;i<configList.size();i++)
			{
				customReportSearchConditionConfig.setHandle( "CustomReportSearchConditonBO:"+UUID.randomUUID().toString() );
				customReportSearchConditionConfig.setSeq( configList.get(i).getSeq()==null?0:configList.get(i).getSeq() );
				customReportSearchConditionConfig.setConditionName( configList.get(i).getConditionName()==null?"":configList.get(i).getConditionName().trim() );
				customReportSearchConditionConfig.setConditionField( configList.get(i).getConditionField()==null?"":configList.get(i).getConditionField().trim() );
				customReportSearchConditionConfig.setFieldType( configList.get(i).getFieldType()==null?"":configList.get(i).getFieldType().trim() );
				customReportSearchConditionConfig.setDailogCheckboxSQL( configList.get(i).getDailogCheckboxSQL()==null?"":configList.get(i).getDailogCheckboxSQL().trim() );
				customReportSearchConditionConfig.setRequiredType( configList.get(i).getRequiredType()==null?"":configList.get(i).getRequiredType().trim() );
				customReportSearchConditionConfig.setRowID( configList.get(i).getRowID()==null?1:configList.get(i).getRowID() );
				customReportSearchConditionConfig.setColumnID( configList.get(i).getColumnID()==null?1:configList.get(i).getColumnID() );
				customReportSearchConditionConfig.setColumnWidthPercent( configList.get(i).getColumnWidthPercent()==null?1:configList.get(i).getColumnWidthPercent() );

				customReportConfigMapper.insertSearchConditionConfig( customReportSearchConditionConfig );
			}

		}
		

		//3.保存资料至 Z_CUSTOM_REPORT_SEARCHCONDITION_DYNSQL_CONFIG
		List<CustomReportSearchConditionDynSQLConfig> searchConditionDynSQLConfigList=config.getSearchConditionDynSQLConfigList();
		if(searchConditionDynSQLConfigList!=null && searchConditionDynSQLConfigList.size() > 0) {
			CustomReportSearchConditionDynSQLConfig customReportSearchConditionDynSQLConfig = new CustomReportSearchConditionDynSQLConfig();
			customReportSearchConditionDynSQLConfig.setSite( site );
			customReportSearchConditionDynSQLConfig.setCustomReportMainModelBo( customReportBO );
			customReportSearchConditionDynSQLConfig.setModifyDateTime( dateTime );
			customReportSearchConditionDynSQLConfig.setModifyUser( user );
			
			for(int i=0;i<searchConditionDynSQLConfigList.size();i++)
			{
				customReportSearchConditionDynSQLConfig.setHandle( "CustomReportSearchConditonDynSQLBO:"+UUID.randomUUID().toString() );
				customReportSearchConditionDynSQLConfig.setSeq( searchConditionDynSQLConfigList.get(i).getSeq()==null?"0":searchConditionDynSQLConfigList.get(i).getSeq().trim() );
				customReportSearchConditionDynSQLConfig.setDynConditionReplaceField( searchConditionDynSQLConfigList.get(i).getDynConditionReplaceField()==null?"":searchConditionDynSQLConfigList.get(i).getDynConditionReplaceField().trim() );
				customReportSearchConditionDynSQLConfig.setDynamicConditionSQL( searchConditionDynSQLConfigList.get(i).getDynamicConditionSQL()==null?"":searchConditionDynSQLConfigList.get(i).getDynamicConditionSQL().trim() );
				customReportSearchConditionDynSQLConfig.setConditionField( searchConditionDynSQLConfigList.get(i).getConditionField()==null?"":searchConditionDynSQLConfigList.get(i).getConditionField().trim() );

				customReportConfigMapper.insertDynSqlConfig( customReportSearchConditionDynSQLConfig );
			}

		}
		
		//4.保存资料至 Z_CUSTOM_REPORT_SEARCHRESULT_COLUMN_CONFIG
		List<CustomReportSearchResultColumnConfig> searchResultColumnList=config.getSearchResultColumnList();
		if(searchResultColumnList!=null && searchResultColumnList.size() > 0) {
			CustomReportSearchResultColumnConfig customReportSearchResultColumnConfig = new CustomReportSearchResultColumnConfig();
			customReportSearchResultColumnConfig.setSite( site );
			customReportSearchResultColumnConfig.setCustomReportMainModelBo( customReportBO );
			customReportSearchResultColumnConfig.setModifyDateTime( dateTime );
			customReportSearchResultColumnConfig.setModifyUser( user );

			
			for(int i=0;i<searchResultColumnList.size();i++)
			{
				customReportSearchResultColumnConfig.setHandle( "CustomReportSearchResultColumnBO"+UUID.randomUUID().toString() );
				customReportSearchResultColumnConfig.setSeq( searchResultColumnList.get(i).getSeq()==null?"0":searchResultColumnList.get(i).getSeq().trim() );
				customReportSearchResultColumnConfig.setColumnField( searchResultColumnList.get(i).getColumnField()==null?"":searchResultColumnList.get(i).getColumnField().trim());
				customReportSearchResultColumnConfig.setColumnName( searchResultColumnList.get(i).getColumnName()==null?"":searchResultColumnList.get(i).getColumnName().trim() );
				customReportSearchResultColumnConfig.setColumnWidthPercent( searchResultColumnList.get(i).getColumnWidthPercent()==null?"1":searchResultColumnList.get(i).getColumnWidthPercent().trim() );
				customReportConfigMapper.insertResultColumnConfig( customReportSearchResultColumnConfig );
			}
			
		}
		
		//5.保存资料至  Z_CUSTOM_REPORT_MAINMODEL
		CustomReportMainmodel customReportMainmodel = new CustomReportMainmodel();

		customReportMainmodel.setHandle( customReportBO );
		customReportMainmodel.setSite( site );
		customReportMainmodel.setReportId( reportID );
		customReportMainmodel.setReportTitle( config.getReportTitle().trim() );
		customReportMainmodel.setReportDesc( config.getReportDESC().trim() );
		customReportMainmodel.setConditionSqlConfig( config.getConditionSQLConfig().trim() );
		customReportMainmodel.setDisplayType( config.getDisplayType().trim() );
		customReportMainmodel.setTimeRefresh( config.getTimeRefresh().trim().length()<1?0:Integer.parseInt( config.getTimeRefresh().trim()) );
		customReportMainmodel.setBackgroundColor( config.getBackgroundColor().trim() );
		customReportMainmodel.setFontColor( config.getFontColor().trim() );
		customReportMainmodel.setPageSize( config.getPageSize().trim().length()<1?0:Integer.parseInt( config.getPageSize().trim() ) );
		customReportMainmodel.setModifyDateTime( dateTime );
		customReportMainmodel.setModifyUser( user );

		customReportConfigMapper.insertInsertMainmodel( customReportMainmodel );
	}


	/* (non-Javadoc)
	 * @see com.knsdev.service.CustomReportConfigInterface#Delete(java.lang.String, java.lang.String)
	 */
	@Override
	public void Delete(String site, String reportid) throws Exception {
		site="*";
		String handle="CustomReportBO:"+site+","+reportid;
		
		//删除 Z_CUSTOM_REPORT_SEARCHCONDITION_CONFIG 数据
		customReportConfigMapper.deleteSearchConditionConfig(handle);
		
		//删除 Z_CUSTOM_REPORT_SEARCHCONDITION_DYNSQL_CONFIG 数据
		customReportConfigMapper.deleteDynSqlConfig( handle );
		
		//删除 Z_CUSTOM_REPORT_SEARCHRESULT_COLUMN_CONFIG 数据
		customReportConfigMapper.deleteResultColumnConfig( handle );
		
		//删除 Z_CUSTOM_REPORT_MAINMODEL 数据
		customReportConfigMapper.deleteMainmodel( handle );
	}


	/* (non-Javadoc)
	 * @see com.knsdev.service.CustomReportConfigInterface#GetReportIDList(java.lang.String)
	 */
	@Override
	public List<CustomReport> GetReportIDList(String site) throws Exception {
		List<CustomReport> customReportList=null;
	 	site="*";
	 	Map<String, Object> param = new HashMap<String, Object>();
	 	param.put("SITE", site);

	 	//List<Map<String,Object>> map=logicService.executeQuery(site, REPORTID_DIALOG_SQL, param);
	 	List<CustomReportMainmodel> map = customReportConfigMapper.selectReportIdDialog( site );

	 	if(map.size()>0)
	 	{
			 customReportList=new ArrayList<CustomReport>();
			 CustomReport cr=null;

			 for(int i=0;i<map.size();i++)
			 {
				 cr=new CustomReport();
				 cr.setReportID(map.get(i).getReportId().trim());
				 cr.setReportDESC(map.get(i).getReportTitle()==null?"":map.get(i).getReportTitle().trim());
				 customReportList.add(cr);
			 }
		}else
		{
			throw new RuntimeException("没有报表作业编号信息");
		}

		return customReportList;
	}
}
