package com.yihu.hos.system.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.hos.system.dao.*;
import com.yihu.hos.system.model.SystemMenu;
import com.yihu.hos.system.model.SystemMenuAction;
import com.yihu.hos.system.model.SystemRoleAction;
import com.yihu.hos.system.model.SystemRoleMenu;
import com.yihu.hos.web.framework.model.DataGridResult;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenweida on 2016/1/20.
 */
@Service("authorityManager")
public class AuthorityManager {

    public static final String BEAN_ID = "authorityManager";

    @Resource(name = MenuDao.BEAN_ID)
    private MenuDao menuDao;
    @Resource(name = MenuActionDao.BEAN_ID)
    private MenuActionDao menuActionDao;
    @Resource(name = RoleDao.BEAN_ID)
    private RoleDao roleDao;
    @Resource(name = SystemRoleActionDao.BEAN_ID)
    private SystemRoleActionDao systemRoleActionDao;
    @Resource(name = SystemRoleMenuDao.BEAN_ID)
    private SystemRoleMenuDao systemRoleMenuDao;
    @Resource(name = AppDao.BEAN_ID)
    private AppDao appdao;
    @Autowired
    private ObjectMapper objectMapper;


   
    public Result getMenuList(String roleId) throws Exception {
        DataGridResult r = new DataGridResult();
        //找出全部的
        List<SystemMenu> systemMenus = menuDao.getAllMenut(new HashMap<>());
        List<SystemMenuAction> systemMenuActions = menuActionDao.getAllMenutAction(new HashMap<>());

        //找出已经选中
        List<SystemRoleMenu> systemRoleMenus = systemRoleMenuDao.getSystemRoleMenuListByRoleId(roleId);
        List<SystemRoleAction> systemRoleActions = systemRoleActionDao.getSystemRoleActionListByRoleId(roleId);
        List<ObjectNode> jr = new ArrayList<>();
        //设置角色菜单回填
        for (int i = 0; i < systemMenus.size(); i++) {
            boolean addFlag = true;
            for (int j = 0; j < systemRoleMenus.size(); j++) {
                if (systemRoleMenus.get(j).getMenuId().equals(systemMenus.get(i).getId())) {
                    ObjectNode jo = objectMapper.createObjectNode();

                    jo.put("id", systemMenus.get(i).getId());
                    jo.put("pid", systemMenus.get(i).getPid());
                    jo.put("name", systemMenus.get(i).getName());
                    jo.put("systemRoleMenuId", systemRoleMenus.get(j).getId());
                    jo.put("checked", "true");
                    jo.put("type", "menu");
                    jr.add(jo);
                    addFlag = false;
                }
            }
            if (addFlag) {
                ObjectNode jo = objectMapper.createObjectNode();
                jo.put("id", systemMenus.get(i).getId());
                jo.put("pid", systemMenus.get(i).getPid());
                jo.put("name", systemMenus.get(i).getName());
                jo.put("checked", "false");
                jo.put("type", "menu");
                jr.add(jo);
            }
        }
        //设置角色按钮菜单回填
        for (int i = 0; i < systemMenuActions.size(); i++) {
            boolean addFlag = true;
            for (int j = 0; j < systemRoleActions.size(); j++) {
                if (systemRoleActions.get(j).getActionId().equals(systemMenuActions.get(i).getId())) {
                    ObjectNode jo = objectMapper.createObjectNode();
                    jo.put("id", systemMenuActions.get(i).getId());
                    jo.put("pid", systemMenuActions.get(i).getMenuId());
                    jo.put("name", systemMenuActions.get(i).getName());
                    jo.put("name", systemMenuActions.get(i).getName());
                    jo.put("systemRoleActionId", systemRoleActions.get(j).getId());
                    jo.put("checked", "true");
                    jo.put("type", "action");
                    jr.add(jo);
                    addFlag = false;
                }
            }
            if (addFlag) {
                ObjectNode jo = objectMapper.createObjectNode();
                jo.put("id", systemMenuActions.get(i).getId());
                jo.put("pid", systemMenuActions.get(i).getMenuId());
                jo.put("name", systemMenuActions.get(i).getName());
                jo.put("checked", "false");
                jo.put("type", "action");
                jr.add(jo);
            }
        }

        r.setDetailModelList(jr);
        return r;
    }

    @Transactional
    public Result changeRoleMenu(String roleId, String menuId, String flag) throws Exception {
        if ("true".equals(flag)) {
            SystemRoleMenu srm = new SystemRoleMenu();
            srm.setMenuId(menuId);
            srm.setRoleId(roleId);
            systemRoleMenuDao.saveEntity(srm);
        } else {
            SystemRoleMenu srm = systemRoleMenuDao.getRoleMenuByRoleIdAndMenuId(roleId, menuId);
            systemRoleMenuDao.deleteEntity(srm);
        }
        return Result.success("修改成功");
    }

    @Transactional
    public Result changeRoleAction(String roleId, String actionId, String flag) throws Exception {
        if ("true".equals(flag)) {
            SystemRoleAction srm = new SystemRoleAction();
            srm.setActionId(actionId);
            srm.setRoleId(roleId);
            systemRoleActionDao.saveEntity(srm);
        } else {
            SystemRoleAction srm = systemRoleActionDao.getRoleActionByRoleIdAndActionId(roleId, actionId);
            systemRoleActionDao.deleteEntity(srm);
        }
        return Result.success("修改成功");
    }

    @Transactional
    public Result allowAll(String roleId) throws Exception {
        List<SystemMenu> systemMenus = menuDao.getAllMenut(new HashMap<>());
        List<SystemMenuAction> systemMenuActions = menuActionDao.getAllMenutAction(new HashMap<>());
        for (SystemMenu sm : systemMenus) {
            SystemRoleMenu srm = new SystemRoleMenu();
            srm.setMenuId(sm.getId());
            srm.setRoleId(roleId);
            systemRoleMenuDao.saveEntity(srm);
        }
        for (SystemMenuAction sm : systemMenuActions) {
            SystemRoleAction srm = new SystemRoleAction();
            srm.setActionId(sm.getId());
            srm.setRoleId(roleId);
            systemRoleMenuDao.saveEntity(srm);
        }
        return Result.success("修改成功");
    }

    @Transactional
    public Result forbidAll(String roleId) throws Exception {
        systemRoleActionDao.deleteAllByRoleId(roleId);
        systemRoleMenuDao.deleteAllByRoleId(roleId);
        return Result.success("修改成功");
    }
}
