package com.hotent.portal.persistence.manager.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.hotent.base.model.CommonResult;
import com.hotent.base.util.UniqueIdUtil;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.portal.model.SysLayoutTools;
import com.hotent.portal.persistence.dao.SysIndexToolsDao;
import com.hotent.portal.persistence.dao.SysLayoutToolsDao;
import com.hotent.portal.persistence.manager.SysLayoutToolsManager;


/**
 * 布局工具设置 处理实现类
 * 
 * @company 广州宏天软件股份有限公司
 * @author maoww
 * @email maoww@jee-soft.cn
 * @date 2018年6月11日
 */
@Service("sysLayoutToolsManager")
public class SysLayoutToolsManagerImpl extends BaseManagerImpl<SysLayoutToolsDao, SysLayoutTools> implements SysLayoutToolsManager{

	@Resource
	SysLayoutToolsManager sysLayoutToolsManager;

	@Override
	public SysLayoutTools getByLayoutID(String layoutId, String toolsType) {
		Map<String, Object> params = new HashMap<>();
		params.put("layoutId", layoutId);
		params.put("toolsType", toolsType);
		return baseMapper.getByLayoutID(params);
	}

    @Override
    public CommonResult<String> moveTool(String layoutId, String toolsType, String toolId, boolean isMove) {
		try{
			SysLayoutTools sysLayoutTools = getByLayoutID(layoutId, toolsType);
			String[] toolsArray = sysLayoutTools.getToolsIds().split(",");
			for (int i=0; i < toolsArray.length; i++){
				if(toolsArray[i].equals(toolId)){
					if(isMove){
						// 向前移动
						String pre = toolsArray[i-1];
						toolsArray[i-1] = toolsArray[i];
						toolsArray[i] = pre;
					}else{
						// 向后移动
						String later = toolsArray[i+1];
						toolsArray[i+1] = toolsArray[i];
						toolsArray[i] = later;
					}
					sysLayoutTools.setToolsIds(StringUtils.join(toolsArray, ","));
					update(sysLayoutTools);
					break;
				}
			}
			return new CommonResult<>(true, "移动成功");
		}catch(Exception e){
			return new CommonResult<>(false, "移动失败", e.getMessage());
		}
    }

    @Override
    public CommonResult<String> saveSysLayoutTools(SysLayoutTools sysLayoutTools) {
		String resultMsg=null;
		try {
			SysLayoutTools layoutTools = sysLayoutToolsManager.getByLayoutID(sysLayoutTools.getLayoutId(), sysLayoutTools.getToolsType());
			if(layoutTools == null){
				sysLayoutTools.setId(UniqueIdUtil.getSuid());
				sysLayoutToolsManager.create(sysLayoutTools);
				resultMsg="添加布局工具设置成功";
			}else{
				layoutTools.setToolsIds(sysLayoutTools.getToolsIds());
				sysLayoutToolsManager.update(layoutTools);
				resultMsg="更新布局工具设置成功";
			}
			return new CommonResult<>(true, resultMsg);
		} catch (Exception e) {
			resultMsg="对布局工具设置操作失败";
			return new CommonResult<>(false, resultMsg);
		}
    }

	@Override
	public CommonResult<String> deleteTool(String layoutId, String toolsType, String toolId) {
		try{
			SysLayoutTools sysLayoutTools = sysLayoutToolsManager.getByLayoutID(layoutId, toolsType);
			String[] toolsArray = sysLayoutTools.getToolsIds().split(",");
			toolsArray = (String[]) ArrayUtils.removeElement(toolsArray, toolId);
			sysLayoutTools.setToolsIds(StringUtils.join(toolsArray, ","));
			sysLayoutToolsManager.update(sysLayoutTools);
			return new CommonResult<>(true, "删除成功");
		}catch(Exception e){
			return new CommonResult<>(false, "删除失败", e.getMessage());
		}
	}

//	@Override
//	public List<SysIndexTools> queryTools(String layoutId, String tools) {
//		List<SysIndexTools> sysIndexToolsList = new ArrayList<SysIndexTools>();
//		List<String> authorizeIdsByUserMap = bpmdefUserManager.getAuthorizeIdsByUserMap(SysIndexTools.INDEX_TOOLS);
//		SysLayoutTools sysLayoutTools = sysLayoutToolsDao.getByLayoutID(layoutId, tools);
//		if(sysLayoutTools != null){
//		String[] toolsArray = sysLayoutTools.getToolsIds().split(",");
//			for(String toolId : toolsArray){
//				if(authorizeIdsByUserMap.contains(toolId)){
//					SysIndexTools sysIndexTools = sysIndexToolsDao.get(toolId);
//					sysIndexToolsList.add(sysIndexTools);
//				}
//			}
//		}
//		return sysIndexToolsList;
//	}
}
