package cn.pluss.platform.controller.roleInfo;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.MenuInfo;
import cn.pluss.platform.entity.RoleInfo;
import cn.pluss.platform.entity.RoleMenuInfo;
import cn.pluss.platform.entity.UserInfo;
import cn.pluss.platform.menuInfo.MenuInfoService;
import cn.pluss.platform.roleInfo.RoleInfoService;
import cn.pluss.platform.roleMenuInfo.RoleMenuInfoService;
import cn.pluss.platform.userInfo.UserInfoService;
import cn.pluss.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("roleInfo")
public class RoleInfoController extends BaseModelController<RoleInfoService, RoleInfo> {

    private Integer PAGE_SIZE = 12;

    @Autowired
    private RoleInfoService roleInfoService;
    @Autowired
    private RoleMenuInfoService roleMenuInfoService;
    @Autowired
    private MenuInfoService menuInfoService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserInfoService uiService;

    /**
     * 引导页
     *
     * @return
     */
    @GetMapping
    public String index() {
        return "auth/roleInfo/roleInfoList";
    }

    /**
     * 分页查询
     */
    @PostMapping("/queryRoleInfoByPage")
    @ResponseBody
    public Map<String, Object> queryRoleInfoByPage(Integer currPage, String name) {

        if (currPage == null) {
            currPage = 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        if (StringUtil.isNotEmpty(name)) {
            map.put("name", name);
        }
        List<RoleInfo> roleInfoList = roleInfoService.queryRoleInfoPage(map);
        Integer count = roleInfoService.queryRoleInfoPageCount(map);
        Map<String, Object> result = new HashMap<>();
        result.put("roleInfoList", roleInfoList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }


    /**
     * 修改初始化
     *
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit() {
        return "auth/role/editRole";
    }

    @GetMapping("detail")
    public String detail(Model model, Integer id) {
        if (id != null) {
            RoleInfo entity = roleInfoService.getById(id);
            request.setAttribute("entity", entity);
        }

        return "auth/roleInfo/roleInfoDetails";
    }

    @GetMapping("configDetail")
    public String configDetail(Integer id) {
        if (id != null) {
            RoleInfo entity = roleInfoService.getById(id);
            request.setAttribute("entity", entity);
        }

        return "auth/roleInfo/roleInfoConfig";
    }

    @PostMapping("/saveConfig")
    @ResponseBody
    public Result<Object> saveConfig(String id, @RequestParam(value = "menuIds[]", required = false) String[] menuIds) {
        roleMenuInfoService.saveRoleMenuList(id, menuIds);
        return ResultGenerator.genSuccessResult("保存成功");
    }

    /**
     * 保存角色配置
     *
     * @throws IOException
     */
    @Override
    @PostMapping("/save")
    @ResponseBody
    public Result<String> save(RoleInfo roleInfo) {

        String userId = request.getHeader("userId");
        UserInfo userInfo = uiService.getById(userId);

        if (roleInfo.getId() != null && roleInfo.getId() != 0) {
            //修改
            roleInfo.setUpdateTime(new Date());
            roleInfo.setUpdator(userInfo.getId() + "");
            roleInfoService.updateRoleInfo(roleInfo);
        } else {
            //新增
            roleInfo.setCreateTime(new Date());
            roleInfo.setCreator(userInfo.getId() + "");
            roleInfo.setUpdateTime(new Date());
            roleInfo.setUpdator(userInfo.getId() + "");
            roleInfoService.saveRoleInfo(roleInfo);
            RoleInfo roleInfo2 = new RoleInfo();
            roleInfo2.setName(roleInfo.getName());
            List<RoleInfo> roleInfoList = roleInfoService.queryRoleInfoList(roleInfo2);
            roleInfo.setId(roleInfoList.get(0).getId());
        }

        //删除关联关系
        RoleMenuInfo rmInfo = new RoleMenuInfo();
        rmInfo.setRoleId(roleInfo.getId());
        roleMenuInfoService.deleteRoleMenuInfo(rmInfo);

        //保存关联关系
        if (!StringUtil.isEmpty(roleInfo.getMenuIds())) {
            String menuIds = roleInfo.getMenuIds();
            String[] mids = menuIds.split(",");
            RoleMenuInfo roleMenuInfo = null;
            List<RoleMenuInfo> roleMenuInfoList = new ArrayList<>();
            for (String mid : mids) {
                roleMenuInfo = new RoleMenuInfo();
                roleMenuInfo.setMenuId(Long.valueOf(mid));
                roleMenuInfo.setRoleId(roleInfo.getId());
                roleMenuInfoList.add(roleMenuInfo);
                roleMenuInfoService.saveRoleMenuInfo(roleMenuInfo);
            }

            //保存关联关系
        }

        return ResultGenerator.genSuccessResult("保存成功");
    }


    /**
     * 通过id查询角色信息
     *
     * @throws IOException
     */
    @RequestMapping(value = "findById", method = RequestMethod.POST)
    @ResponseBody
    public RoleInfo findById(String id) throws IOException {
        Long roleId = Long.valueOf(id);
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setId(roleId);
        RoleInfo rInfo = roleInfoService.queryRoleInfo(roleInfo);
        return rInfo;
    }

    /**
     * 查询角色目录
     *
     * @throws IOException
     */
    @RequestMapping(value = "roleMenus", method = RequestMethod.POST)
    @ResponseBody
    public List<MenuInfo> roleMenus(String roleId) throws IOException {
        Integer id = Integer.valueOf(roleId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roleId", id);
        List<MenuInfo> menuList = menuInfoService.queryMenuInfoByRoleId(map);
        return menuList;
    }


    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryRoleInfoDetails", method = RequestMethod.GET)
    public String queryRoleInfoDetails(HttpServletRequest request, Long id, Integer pageIndex) {

        if (id != null && id != 0) {
            RoleInfo roleInfo = new RoleInfo();
            roleInfo.setId(id);
            roleInfo = roleInfoService.queryRoleInfo(roleInfo);
            request.setAttribute("roleInfo", roleInfo);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "roleInfo/roleInfoDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryRoleInfoBaseInfo", method = RequestMethod.GET)
    public String queryRoleInfoBaseInfo(HttpServletRequest request, Long id, Integer pageIndex) {

        if (id != null && id != 0) {
            RoleInfo roleInfo = new RoleInfo();
            roleInfo.setId(id);
            roleInfo = roleInfoService.queryRoleInfo(roleInfo);
            request.setAttribute("roleInfo", roleInfo);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "roleInfo/roleInfoBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryRoleInfoExtendsInfo", method = RequestMethod.GET)
    public String queryRoleInfoExtendsInfo() {

        return "roleInfo/roleInfoExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryRoleInfoFunctionsInfo", method = RequestMethod.GET)
    public String queryRoleInfoFunctionsInfo() {

        return "roleInfo/roleInfoFunctionsInfo";
    }

    /**
     * 保存对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/saveRoleInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveRoleInfo(RoleInfo roleInfo) {

        if (roleInfo.getId() != null && roleInfo.getId() != 0) {
            roleInfoService.updateRoleInfo(roleInfo);
        } else {
            roleInfoService.saveRoleInfo(roleInfo);
        }
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 修改对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/updateRoleInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateRoleInfo(Long id) {

        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setId(id);
        roleInfoService.updateRoleInfo(roleInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 删除对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/deleteRoleInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteRoleInfo(Long id) {
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setId(id);
        roleInfoService.deleteRoleInfo(roleInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }


}


