package com.hnshituo.core.auth.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.vo.ResultModel;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.mapper.FunctionMapper;
import com.hnshituo.core.auth.service.FunctionService;
import com.hnshituo.core.auth.service.HomeService;
import com.hnshituo.core.auth.vo.Function;

/**
 * @author pq
 * 
 * TODO: 级联删除
 *
 */
@RemoteService(path="core/auth/function")
public class FunctionServiceImpl extends BaseServiceImpl<Function, Integer> implements FunctionService {

	@RemoteService(path="tree")
	public List<Function> findTreeAll() {
		List<Function> all = this.getMapper(FunctionMapper.class).findWithCondition(null, null, "ID", 0, 0);
		return findByPid(all, null);
	}

	/**
	 * 获取不含按钮的界面
	 * 用于activiti配置界面与流程关联
	 * */
	@RemoteService(path="activitiMenu")
	public List<Function> findActCmbTreeDb() throws Exception {
		//TODO 权限控制
		List<Function> all = this.getMapper(FunctionMapper.class).findWithCondition(null, " VISIBLE = 0 AND TYPE = 0 ", " SEQ ", 0, 0);
		return findByPid(all, 1);
	}

	@RemoteService(path="tree4Prv")
	public List<Function> findTreeByRoleId(Integer roleId) {
		if (roleId==null || roleId==0) return findTreeAll();
		List<Function> all = this.getMapper(FunctionMapper.class).findTree4Prv(roleId);
		return findByPid(all, null);
	}

	/**
	 * 递归菜单数据，输出树形数据对象
	 * @param all
	 * @param pid
	 * @return
	 */
	private List<Function> findByPid(List<Function> all, Integer pid) {
		List<Function> list = null;
		for (Function func : all) {
			if (pid == null && func.getPid() == null) {
				if (list == null) list = new ArrayList<Function>();
				list.add(func);
			} else if (pid != null && pid.equals(func.getPid())) {
				if (list == null) list = new ArrayList<Function>();
				list.add(func);
			}
		}
		if (list != null) {
			for (Function func : list) {
				func.setChildren(findByPid(all, func.getId()));
			}
		}
		return list;
	}

	public List<Function> findFunctions(Integer pid) {
		Function func = new Function();
		func.setPid(pid);
		func.setType(1);
		func.setVisible(0);
		return this.getMapper(FunctionMapper.class).findWithCondition(func, null, "SEQ", 0, 0);
	}

	/*public List<Function> findFunctions() {
		Function func = new Function();
		func.setType(1);
		return this.getMapper(FunctionMapper.class).find(func, 0, 0);
	}*/

	public List<Function> findByPid(Integer pid) {
		Function func = new Function();
		func.setPid(pid);
		return this.getMapper(FunctionMapper.class).find(func, 0, 0);
	}

	/**
	 * 新增
	 * */
	public ResultModel doAdd(Function t) throws Exception {
		ResultModel rm=new ResultModel();
		t.setCreateTime(new Date());
		t.setCreateMan((String)getSession().getAttribute(HomeService.SESSION_USER_NAME));
		//oracle无自动递增
		int count=this.getMapper(FunctionMapper.class).findMaxId();
		t.setId(count+1);
		super.insert(t);
		rm.setState("200");
		rm.setData(t);
		return rm;
	}

	/**
	 * 删除
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDelete(Integer id) throws Exception {
		ResultModel rm=new ResultModel();
		try{
			List<Function> list = this.findByPid(id);
			for (Function function : list) {
				delete(function.getId());
			}
			super.delete(id);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			throw e;
		}
		return rm;
	}

	public Function modify(Function t) throws Exception {
		t.setUpdateTime(new Date());
		t.setUpdateMan((String)getSession().getAttribute(HomeService.SESSION_USER_NAME));
		super.update(t);
		return t;
	}

	@Transactional(value="tm", rollbackFor=Exception.class)
	public void dragMenu(int id, int pid, int[] ids) {
		Function function = new Function();
		function.setId(id);
		function.setPid(pid);
		this.getMapper(FunctionMapper.class).update(function);
		reordering(ids);
	}

	public void reordering(int[] ids) {
		for (int i = 0; i < ids.length; i++) {
			//int id = ids[i];
			Function function = new Function();
			function.setId(ids[i]);
			function.setSeq(i + 1);
			this.getMapper(FunctionMapper.class).update(function);
		}
	}

	public boolean hasFunction(String url) {
		Function function = new Function();
		function.setUrl(url);
		int count = this.getMapper(FunctionMapper.class).count(function);
		if (count > 0) return true;
		return false;
	}

	@Override
	public boolean isOpenDataPrg(String url) throws Exception {
		Function function = new Function();
		function.setUrl(url);
		function.setDataPrg(1);
		int count = this.getMapper(FunctionMapper.class).count(function);
		if (count > 0) return true;
		return false;
	}

	public String getDataPrgField(String url) throws Exception {
		Function function = new Function();
		function.setUrl(url);
		function.setDataPrg(1);
		List<Function> funcs = this.getMapper(FunctionMapper.class).find(function, 0, 0);
		if (funcs != null) {
			for (Function func : funcs) {
				if (StringUtils.isNotEmpty(func.getDataPrgField())) {
					return func.getDataPrgField();
				}
			}
		}
		return null;
	}

}
