package com.iamberry.wechat.service.admin;

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

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

import com.iamberry.app.tool.des.MD5;
import com.iamberry.app.tool.log.RatFWLogger;
import com.iamberry.wechat.core.entity.admin.AdminChildInfo;
import com.iamberry.wechat.core.entity.admin.AdminChildPermissionErmissions;
import com.iamberry.wechat.core.entity.admin.AdminChildPermissions;
import com.iamberry.wechat.core.entity.admin.AdminInfo;
import com.iamberry.wechat.core.entity.admin.AdminLoginDTO;
import com.iamberry.wechat.face.admin.AdminLoginService;
import com.iamberry.wechat.service.admin.dao.AdminLoginDAO;

@Service
public class AdminLoginServiceImpl implements AdminLoginService {

	@Autowired
	private AdminLoginDAO adminLoginDAO;
	public void setAdminLoginDAO(AdminLoginDAO adminLoginDAO) {
		this.adminLoginDAO = adminLoginDAO;
	}
	
	MD5 md5 = new MD5("inlongadMD5");
	
	@Autowired
	private RatFWLogger logger;
	public void setLogger(RatFWLogger logger) {
		this.logger = logger;
	}
	
	public Object selectAdminInfoByNameAndPwd(AdminLoginDTO dto) throws Exception {
		try {
			if (dto == null || dto.getAdminLoginPwd() == null) {
				return null;
			}
			dto.setAdminLoginPwd(md5.encrypt(dto.getAdminLoginPwd()));
			// 权限错误最小化
			/*AdminChildInfo adminChildInfo = adminLoginDAO.selectAdminChildInfoByNameAndPwd(dto);
			if (adminChildInfo != null && adminChildInfo.getAdminId() != null) {
				return adminChildInfo;
			}*/
			AdminInfo adminInfo = adminLoginDAO.selectAdminInfoByNameAndPwd(dto);
			if (adminInfo != null && adminInfo.getAdminId() != null) {
				return adminInfo;
			}
			
			if(adminInfo==null){
				AdminChildInfo adminChildInfo=
						adminLoginDAO.selectAdminChildInfoByNameAndPwd(dto);
				return adminChildInfo;
			}
			
			return null;
		
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this, e.getMessage());
		}
		return null;
	}

	
	public List<AdminChildPermissionErmissions> selectAdminChildHavePermissionById(
			Integer id) {
		return adminLoginDAO.selectAdminChildHavePermissionById(id);
	}

	
	public List<AdminChildPermissions> selectMenusById(Integer id, boolean flag) {
		if (flag) {
			return adminLoginDAO.selectAdminMenuInfoByAdminId(id);
		} else {
			// return adminLoginDAO.selectAdinChildPermissionByAdminChildId(id);
			// 二级权限
			List<AdminChildPermissionErmissions> list = adminLoginDAO.selectAdminChildPermissionEntityById(id);
			// 一级权限
			List<AdminChildPermissions> permissions = adminLoginDAO.selectAllPermission();
			for (AdminChildPermissionErmissions ermissionsChild : list) {
				for (AdminChildPermissions permissions2 : permissions) {
					if (permissions2.getPermissionsId().intValue() == ermissionsChild.getPermissionId().intValue()) {
						permissions2.getMenus().add(ermissionsChild);
					}
				}
			}
			List<AdminChildPermissions> lists = new ArrayList<AdminChildPermissions>();
			for (AdminChildPermissions adminChildPermissions : permissions) {
				if (adminChildPermissions.getMenus().size() > 0) {
					lists.add(adminChildPermissions);
				}
			}
			return lists;
		}
	}

}
