package com.mortals.xhx.base.system.role.web;


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.menu.model.MenuTreeSelect;
import com.mortals.xhx.base.system.resource.model.ResourceEntity;
import com.mortals.xhx.base.system.resource.service.ResourceService;
import com.mortals.xhx.base.system.role.model.RoleAuthEntity;
import com.mortals.xhx.base.system.role.model.RoleAuthQuery;
import com.mortals.xhx.base.system.role.service.RoleAuthService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色资源权限
 *
 * @author: zxfei
 * @date: 2022/5/7 15:19
 */
@Slf4j
@RestController
@RequestMapping("role/auth")
public class RoleAuthController extends BaseCRUDJsonBodyMappingController<RoleAuthService, RoleAuthEntity, Long> {

    @Autowired
    private ResourceService resourceService;

    public RoleAuthController() {
        super.setModuleDesc("角色资源权限");
    }

    /**
     * 分配资源
     */
    @ApiOperation(value = "分配资源")
    @PostMapping(value = "distributionSource")
    public String distributionSource(@RequestBody RoleAuthQuery query) {
        try {
            service.doDistributionSource(query, this.getContext());
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            return ret.toJSONString();
        } catch (Exception e) {
            log.error("分配角色资源错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
     * 分配菜单
     */
    @ApiOperation(value = "分配菜单")
    @PostMapping(value = "distributionMenu")
    public String distributionMenu(@RequestBody RoleAuthQuery query) {
        try {
            service.doDistributionMenu(query, this.getContext());
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            return ret.toJSONString();
        } catch (Exception e) {
            log.error("分配角色资源错误", e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }


    /**
     * 根据角色编辑菜单
     */
    @ApiOperation(value = "根据角色编辑菜单")
    @PostMapping(value = "editMenu")
    public String editMenu(@RequestBody RoleAuthQuery query) {
        JSONObject ret = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        try {
            List<MenuTreeSelect> menuTreeSelects = service.editMenu(query, this.getContext());
            ret.put(KEY_RESULT_DATA, menuTreeSelects);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            return ret.toJSONString();
        } catch (Exception e) {
            log.error("角色编辑菜单错误", e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

    /**
     * 根据角色编辑资源
     */
    @ApiOperation(value = "根据角色编辑资源")
    @PostMapping(value = "editResource")
    public String editResource(@RequestBody RoleAuthQuery query) {
        JSONObject ret = new JSONObject();
        try {
            Map<String, List<ResourceEntity>> group = resourceService.group(query.getRoleId(), getContext());
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_DATA, group);
            return ret.toJSONString();
        } catch (Exception e) {
            log.error("角色编辑资源错误", e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }

}