package cn.hmsoft.frame.service;

import java.util.Properties;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.frame.data.dao.FrameResDao;
import cn.hmsoft.frame.data.model.FrameOptrRes;
import cn.hmsoft.frame.data.model.FrameRes;
import cn.hmsoft.frame.data.model.FrameRoleRes;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.helper.StringHelper;

@Service
public class FrameResService extends FrameService {

	@Autowired
	private FrameResDao daoRes;

	
	public void addRes(FrameRes res) {
		// FrameAssertUtil.adminOptr(optr);
		FrameAssertUtil.isNotEmpty(res.getRes_name(), "资源名称不允许为空");
		FrameAssertUtil.isNotNull(res.getRes_pid(), "级资源不允许为空");
		FrameRes tr = this.daoRes.find(FrameRes.class, "res_id", res.getRes_id());
		FrameAssertUtil.isNull(tr, "资源编号 " + res.getRes_id() + " 已经存在，请重新输入");
		// this.authOptrRole(optr, FrameResId.Res_Add, res.getRes_id() + "," +
		// res.getRes_name());

		if (res.getRes_pid() > 0) {
			tr = this.daoRes.find(FrameRes.class, "res_id", res.getRes_pid());
			FrameAssertUtil.isNotNull(tr, "上级资源 " + res.getRes_pid() + " 不存在，请重新输入");
			res.setRes_level(tr.getRes_level() + 1);
		} else
			res.setRes_level(1);
		this.daoRes.insert(res);
	}

	public void editRes(FrameRes res) {
		FrameAssertUtil.isNotEmpty(res.getRes_name(), "资源名称不允许为空");
		FrameAssertUtil.isNotNull(res.getRes_pid(), "上级资源不允许为空");
		FrameAssertUtil.isNotEqual(res.getRes_id(), res.getRes_pid(), "上级资源不能为自己");
		if (res.getRes_pid() > 0) {
			FrameRes tr = this.daoRes.find(FrameRes.class, "res_id", res.getRes_pid());
			FrameAssertUtil.isNotNull(tr, "上级资源 " + res.getRes_pid() + " 不存在，请重新输入");
			FrameAssertUtil.isNotEqual(res.getRes_id(), tr.getRes_pid(), "上级资源不能的父资源不能为当前资源");
			
			res.setRes_level(tr.getRes_level() + 1);			
		} else
			res.setRes_level(1);
		this.daoRes.update(res);
	}

	public void deleteRes(int res_id) {
		this.daoRes.delete(FrameRes.class, "res_id", res_id);
		this.daoRes.delete(FrameRoleRes.class, "res_id", res_id);
		this.daoRes.delete(FrameOptrRes.class, "res_id", res_id);
	}

	public void syncRes(Properties framePro, Properties sysPro) {
		//框架菜单
		try {
			if(framePro != null) {
				Set<Object> keys = framePro.keySet();
				for(Object key : keys) {
					FrameRes res = daoRes.find("res_url", key);
					String result = (String) framePro.get(key);
					if(StringHelper.isEmpty(result)) {
						throw new BusinessException("frame-res.properties中的key为:" +key +"的value值不能为空！" );
					}
					String[] value = result.split(",");
					if(value.length !=2 ) 
						throw new BusinessException("frame-res.properties中的key为:" +key +"的value值需要以指定资源ID和资源名称，并且以英文逗号分割！");
					if(res == null) {
						res = new FrameRes();
						res.setRes_id(Integer.parseInt(value[0]));
						res.setRes_name(value[1]);
						res.setRes_url(String.valueOf(key));
						res.setRes_pid(1000);
						res.setRes_level(3);
						res.setRes_type("Page");
						res.setRes_css("icon-people");
						res.setRes_status(FrameStatus.Active.toString());
						res.setRes_order(Integer.parseInt(value[0]));
						daoRes.insert(res);
					}
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}

		//业务菜单
		try {
			if(sysPro != null) {
				Set<Object> keys = sysPro.keySet();
				for(Object key : keys) {
					FrameRes res = daoRes.find("res_url", key);
					String result = (String) sysPro.get(key);
					if(StringHelper.isEmpty(result)) {
						throw new BusinessException("sys-res.properties中的key为:" +key +"的value值不能为空！" );
					}
					String[] value = result.split(",");
					if(value.length !=2 ) 
						throw new BusinessException("sys-res.properties中的key为:" +key +"的value值需要以指定资源ID和资源名称，并且以英文逗号分割！");
					if(res == null) {
						if(daoRes.find(value[0]) != null ) {
							throw new BusinessException("菜单名称：" + value[1] +"指定的ID和现在数据库中的数据重复，请更换ID");
						} else {
							res = new FrameRes();
							res.setRes_id(Integer.parseInt(value[0]));
							res.setRes_name(value[1]);
							res.setRes_url(String.valueOf(key));
							res.setRes_pid(1000);
							res.setRes_level(3);
							res.setRes_type("Page");
							res.setRes_css("icon-people");
							res.setRes_status(FrameStatus.Active.toString());
							res.setRes_order(Integer.parseInt(value[0]));
							daoRes.insert(res);
						}
						
					}
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}

}
