package org.ccpit.base.role;

import org.apache.commons.lang.StringUtils;
import org.ccpit.base.controller.Convert;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageBo;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.utils.UrlRolesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/6.
 */
@Repository
@Transactional
public class RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UrlRolesMapper mapper;

    public boolean addRole(String roleName, String url, String description) {
        Role role = new Role();
        role.setRoleName(roleName);
        role.setDescription(description);
        List<RoleUrl> urlList = new ArrayList<RoleUrl>();
        for (String u : url.split(",")) {
            RoleUrl ru = new RoleUrl();
            ru.setUrl(u);
            urlList.add(ru);
        }
        role.setUrlPerfixs(urlList);
        return roleDao.save(role);
    }

    public boolean addRole(Role role) {
        boolean result = roleDao.save(role);
        if (result)
            mapper.reload();
        return result;
    }

    public boolean editRole(long id, String roleName, String urlPrefixs, String description) {
        Role role = roleDao.queryById(id);
        urlPrefixs = getPriId(urlPrefixs);
        if (role != null) {
            if (null != roleName && !"".equals(roleName)) {
                role.setRoleName(roleName);
            }
            if (null != roleName && !"".equals(description)) {
                role.setDescription(description);
            }
            List<RoleUrl> urlList = new ArrayList<RoleUrl>();
            for (String u : urlPrefixs.split(",")) {
                RoleUrl ru = new RoleUrl();
                ru.setUrl(u);
                urlList.add(ru);
            }
            role.setUrlPerfixs(urlList);
            boolean result = roleDao.update(role);
            if (result)
                mapper.reload();
            return result;
        }
        return false;
    }

    public boolean deleteRole(long id) {
        boolean result = roleDao.deleteById(id);
        if (result)
            mapper.reload();
        return result;
    }

    public Role get(long id) {
        return roleDao.queryById(id);
    }

    public Page<Role> getAll(PageRequest pr) {
        return roleDao.findPage(pr, "from " + Role.class.getName(), null);
    }

    public Page<Role> getAll(PageRequest pr, String hql) {
        return roleDao.findPage(pr, hql, null);
    }

    public List<Role> getAll(String hql) {
        return roleDao.query(hql);
    }


    public List<Role> getAllRoles() {
        return roleDao.queryAll();
    }

    public Map<String, Object> convertToMap(Role role) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (role == null)
            return map;
        List<String> urls = new ArrayList<String>();
        for (RoleUrl roleUrl : role.getUrlPerfixs()) {
            urls.add(roleUrl.getUrl());
        }
        map.put("urlPrefixs", StringUtils.join(urls, ","));
        map.put("roleName", role.getRoleName());
        map.put("id", role.getId());
        map.put("urlPerfixs", urls);
        map.put("description", role.getDescription());
        return map;
    }

    public PageBo<Role> convert(Page<Role> page) {
        return new PageBo<Role>(page, new Convert<Role>() {
            @Override
            public Map<String, Object> convert(Role obj) {
                return convertToMap(obj);
            }
        });
    }

    /**
     * 根据url查找role
     *
     * @param url
     * @return
     */
    public Role findRoleByUrl(String url) {
        return roleDao.getRoleByUrl(url);
    }

    //去掉重复priId
    private String getPriId(String priIds) {
        List list = new ArrayList();
        String[] str = priIds.split(",");
        if (null != str && !"".equals(str)) {
            for (int i = 0; i < str.length; i++) {
                if (!list.contains(str[i])) {
                    list.add(str[i]);
                }
            }
        }
        String priId = "";
        if (null != list && list.size() > 0) {
            for (int j = 0; j < list.size(); j++) {
                String id = (String) list.get(j);
                priId += id + ",";
            }
        }
        return priId.substring(0, priId.length() - 1);
    }
}
