package com.lightinit.hsdatashow.controller;

import com.lightinit.hsdatashow.common.MenuUtils;
import com.lightinit.hsdatashow.common.RoleComboUtils;
import com.lightinit.hsdatashow.common.RoleModulesUtilsPro;
import com.lightinit.hsdatashow.json.pojo.JsonResult;
import com.lightinit.hsdatashow.json.pojo.ListResult;
import com.lightinit.hsdatashow.json.pojo.RoleComboEntity;
import com.lightinit.hsdatashow.entity.Menu;
import com.lightinit.hsdatashow.entity.MenuDataEntity;
import com.lightinit.hsdatashow.entity.RoleModule;
import com.lightinit.hsdatashow.entity.SentryRole;
import com.lightinit.hsdatashow.service.IMenuService;
import com.lightinit.hsdatashow.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * Created by Mr.Yao on 2017/4/24.
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private RoleModulesUtilsPro roleModulesUtilsPro;
    @Autowired
    private IMenuService menuService;

    @RequestMapping(value = "view/list.action")
    public String print_role_view_list(){
        return "role/role";
    }

    @ResponseBody
    @RequestMapping(value = "json/list.action",method = RequestMethod.GET)
    public ListResult<SentryRole> print_Json_Role(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows){
        ListResult<SentryRole> result=new ListResult<SentryRole>();
        result.setTotal(roleService.QueryCount(null));
        result.setRows(roleService.QueryList(page,rows,null));
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "json/combox/list.action",method = RequestMethod.GET)
    public List<RoleComboEntity> print_Json_Combox_Role(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows, @RequestParam(defaultValue = "true") boolean combox){
        List<RoleComboEntity> result=new ArrayList<RoleComboEntity>();
        List<SentryRole> list=roleService.QueryList(page,rows,null);
        if(combox) {
            RoleComboEntity tipEntity = new RoleComboEntity();
            tipEntity.setId(-1L);
            tipEntity.setText("-请选择-");
            tipEntity.setChildren(null);
            tipEntity.setIconCls("fi-page");
            result.add(tipEntity);
        }
        if (list!=null){
            for (SentryRole item:list){
                result.add(RoleComboUtils.RoleComboxConverter(item));
            }
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "menu/list.action",method = RequestMethod.GET)
    public List<MenuDataEntity> print_Json_Menu_Role(@RequestParam(defaultValue = "0") long role){
        List<MenuDataEntity> resultList=new ArrayList<MenuDataEntity>();
        List<MenuDataEntity> list=new ArrayList<MenuDataEntity>();
        list.addAll(MenuUtils.InitSystemTopMenu());
        List<Menu> menuList= menuService.QueryList(1,1000,null);
        for (Menu item:menuList) {
            MenuDataEntity _item= MenuUtils.MenuConverter(item,new ArrayList<RoleModule>(),true);
            if(_item!=null)
                list.add(_item);
        }
        list.addAll(MenuUtils.InitSystemBottomMenu());
        if(role>0){
            SentryRole sentryRole= roleService.QueryOne(role);
            if(sentryRole.getParentid()!=null && sentryRole.getParentid()>0) {
                List<Long> roleModules = roleModulesUtilsPro.RoleHasModules(sentryRole.getParentid());
                resultList = MenuUtils.UserRolePermission(list, roleModules);
            }else{
                resultList.clear();
                resultList.addAll(list);
            }
        }else{
            resultList.clear();
            resultList.addAll(list);
        }
        return resultList;
    }

    @ResponseBody
    @RequestMapping(value = "json/module/list.action",method = RequestMethod.POST)
    public List<RoleModule> print_Json_Module_Role(@RequestParam(defaultValue = "0") long role){
        if(role>0) {
            List<RoleModule> result = roleModulesUtilsPro.RoleModules(role);
            return result;
        }else
            return new ArrayList<RoleModule>();
    }
    @ResponseBody
    @RequestMapping(value = "json/module/save.action",method = RequestMethod.POST)
    public JsonResult print_Save_Module_Role(@RequestParam(defaultValue = "0") long role, @RequestParam(defaultValue = "") String ids){
        JsonResult jsonResult=new JsonResult();
        if(role>0) {
            String[]list=ids.split(";");
            List<Long> idList=new ArrayList<Long>();
            for (String id:list) {
                if (StringUtils.isEmpty(id)==false){
                    idList.add(Long.valueOf(id));
                }
            }
            roleService.SaveRoleModule(role,idList, null);
            jsonResult.setStatus(1);
            jsonResult.setMessage("授权成功");
        }else{
            jsonResult.setStatus(-1);
            jsonResult.setMessage("参数错误");
        }
        return jsonResult;
    }
    @ResponseBody
    @RequestMapping(value = "json/role/save.action",method = RequestMethod.POST)
    public JsonResult print_Save_Role(@RequestParam(defaultValue = "0") long role,@RequestParam(defaultValue = "-1") long parentid,@RequestParam String code,@RequestParam String name){
        JsonResult jsonResult=new JsonResult();
        boolean success=false;
        if(StringUtils.isEmpty(code)){
            jsonResult.setStatus(0);
            jsonResult.setMessage("角色编码不能为空");
        }else if(StringUtils.isEmpty(name)){
            jsonResult.setStatus(0);
            jsonResult.setMessage("角色名称不能为空");
        }else {
            if (role > 0) {
                SentryRole item = roleService.QueryOne(role);
                if(item==null){
                    jsonResult.setStatus(0);
                    jsonResult.setMessage("当前角色不存在");
                }else {
                    item.setParentid(parentid <= 0 ? null : parentid);
                    item.setRolecode(code.toLowerCase());
                    item.setRolename(name);
                    success = roleService.UpdateRole(item);
                }
            } else {
                if(roleService.IsExitsRole(code)){
                    jsonResult.setStatus(0);
                    jsonResult.setMessage("当前角色已存在");
                }else {
                    SentryRole sentryRole = new SentryRole();
                    sentryRole.setAddtiime(new Date());
                    sentryRole.setChildren(null);
                    sentryRole.setParentid(parentid <= 0 ? null : parentid);
                    sentryRole.setRolename(name);
                    sentryRole.setRolecode(code.toLowerCase());
                    success = roleService.InsertRole(sentryRole);
                }
            }
            jsonResult.setStatus(success ? 1 : 0);
            jsonResult.setMessage(success ? "添加角色成功" : "添加角色失败");
        }
        return jsonResult;
    }
    @ResponseBody
    @RequestMapping(value = "json/role/delete.action",method = RequestMethod.POST)
    public JsonResult print_delete_Role(@RequestParam(defaultValue = "0") long role){
        JsonResult jsonResult=new JsonResult();
        boolean success=false;
        if (role > 0) {
            SentryRole item = roleService.QueryOne(role);
            if(item==null){
                jsonResult.setStatus(0);
                jsonResult.setMessage("当前角色已不存在");
            }else if(item.getChildren()!=null && item.getChildren().size()>0){
                jsonResult.setStatus(-1);
                jsonResult.setMessage("角色【"+item.getRolename()+"】还有子角色，当前角色不能删除");
            }else {
                List<Long> roleIds=roleModulesUtilsPro.RoleIds(item);
                if(roleIds.size()==0){
                    jsonResult.setStatus(-1);
                    jsonResult.setMessage("参数错误");
                }else {
                    if (roleService.CheckRoleHasModules(roleIds)) {
                        jsonResult.setStatus(-1);
                        jsonResult.setMessage("角色【"+item.getRolename()+"】已经分配权限，当前角色不能删除");
                    }else if (roleService.CheckRoleHasUser(roleIds)) {
                        jsonResult.setStatus(-1);
                        jsonResult.setMessage("角色【"+item.getRolename()+"】已经分配用户，当前角色不能删除");
                    }else{
                       if(roleService.DeleteRole(role)){
                           jsonResult.setStatus(1);
                           jsonResult.setMessage("角色【"+item.getRolename()+"】删除成功");
                       }else{
                           jsonResult.setStatus(-1);
                           jsonResult.setMessage("角色【"+item.getRolename()+"】删除失败");
                       }
                    }
                }
            }
        } else {
            jsonResult.setStatus(-1);
            jsonResult.setMessage("参数错误");
        }
        return jsonResult;
    }
}
