package com.yihu.hos.system.service;

import com.yihu.hos.system.dao.MenuActionDao;
import com.yihu.hos.system.dao.MenuDao;
import com.yihu.hos.system.model.SystemMenu;
import com.yihu.hos.system.model.SystemMenuAction;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.DataGridResult;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by chenweida on 2016/1/18.
 */
@Service("menuManager")
public class MenuManager{

    public static final String BEAN_ID = "menuManager";

    @Resource(name = MenuDao.BEAN_ID)
    private MenuDao menuDao;
    @Resource(name = MenuActionDao.BEAN_ID)
    private MenuActionDao menuActionDao;

   
    public Result getMenuList(Map<String, Object> params) throws Exception {
        DataGridResult actionResult = new DataGridResult();
        actionResult.setDetailModelList(menuDao.getAllMenut(params));
        return actionResult;
    }

   
    public SystemMenu getMenuById(String menuId) throws Exception {
        return menuDao.getEntity(SystemMenu.class, menuId);
    }

    @Transactional
    public Result addMenu(SystemMenu obj) throws Exception {
        SystemMenu smTemp = menuDao.getMenuByCode(obj.getCode());
        if (smTemp != null) {
            return Result.error("菜单代码重复");
        }
        menuDao.saveEntity(obj);
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateMenu(SystemMenu obj) throws Exception {
        SystemMenu smTemp = menuDao.getMenuByCodeWithOutId(obj.getCode(), obj.getId());
        if (smTemp != null) {
            return Result.error("菜单代码重复");
        }
        SystemMenu systemMenu = menuDao.getEntity(SystemMenu.class, obj.getId());
        systemMenu.setCode(obj.getCode());
        systemMenu.setName(obj.getName());
        systemMenu.setOpenType(obj.getOpenType());
        systemMenu.setPid(obj.getPid());
        systemMenu.setPyCode(obj.getPyCode());
        systemMenu.setUrl(obj.getUrl());
        systemMenu.setRemark(obj.getRemark());
        return Result.success("更新成功");
    }

    @Transactional
    public Result activityMenu(String orgId, String activityFlag) throws Exception {
        try {
            menuDao.activityMenu(orgId, activityFlag);
            return new ActionResult(true, "状态修改成功！");
        } catch (Exception ex) {
            return new ActionResult(false, "状态修改失败！");
        }
    }

    @Transactional
    public Result deleteMenu(String ids) throws Exception {
        for (String id : ids.split(",")) {
            menuDao.deleteMenu(id);
        }
        return Result.success("删除成功");
    }

    //==============================menuAction==================================
   
    public Result getMenuActionList(Map<String, Object> params) throws Exception {
        DataGridResult actionResult = new DataGridResult();
        actionResult.setDetailModelList(menuActionDao.getAllMenutAction(params));
        return actionResult;
    }

   
    public SystemMenuAction getMenuActionById(String menuId) throws Exception {
        return menuActionDao.getEntity(SystemMenuAction.class, menuId);
    }

    @Transactional
    public Result addMenuAction(SystemMenuAction obj) throws Exception {
        SystemMenuAction objTemp = menuActionDao.getMenuActionByNameAndMenuId(obj.getName(), obj.getMenuId());
        if (objTemp != null) {
            return Result.error("菜单按钮名称已经存在");
        }
        menuActionDao.saveEntity(obj);
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateMenuAction(SystemMenuAction obj) throws Exception {
        SystemMenuAction objTemp = menuActionDao.getMenuActionByNameAndMenuIdWithOutId(obj.getName(), obj.getMenuId(), obj.getId());
        if (objTemp != null) {
            return Result.error("菜单按钮名称已经存在");
        }
        SystemMenuAction systemMenu = menuActionDao.getEntity(SystemMenuAction.class, obj.getId());
        systemMenu.setName(obj.getName());
        systemMenu.setUrl(obj.getUrl());
        systemMenu.setRemark(obj.getFlag());
        systemMenu.setMenuId(obj.getMenuId());
        systemMenu.setRemark(obj.getRemark());
        return Result.success("更新成功");
    }

    @Transactional
    public Result activityMenuAction(String menuActionID, String activityFlag) throws Exception {
        try {
            menuActionDao.activityMenuAction(menuActionID, activityFlag);
            return new ActionResult(true, "状态修改成功！");
        } catch (Exception ex) {
            return new ActionResult(false, "状态修改失败！");
        }
    }

    @Transactional
    public Result deleteMenuAction(String id) throws Exception {
        SystemMenuAction smc = menuActionDao.getEntity(SystemMenuAction.class, id);
        menuActionDao.deleteEntity(smc);
        return Result.success("删除成功");
    }
}
