package com.njcn.user.controller;


import cn.hutool.core.util.ObjectUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.user.pojo.param.RoleSystemParam;
import com.njcn.user.pojo.po.SysRoleSystem;
import com.njcn.user.service.ISysRoleSystemService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xy
 * @since 2026-01-05
 */
@RestController
@RequestMapping("/sysRoleSystem")
@Validated
@Slf4j
    @Api(tags = "角色与系统管理")
@AllArgsConstructor
public class SysRoleSystemController extends BaseController {

    private final ISysRoleSystemService sysRoleSystemService;
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getSystemByRoleId")
    @ApiOperation("根据角色id获取系统信息")
    @ApiImplicitParam(name = "id", value = "角色id", required = true)
    public HttpResult<RoleSystemParam> getSystemByRoleId(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getSystemByRoleId");
        SysRoleSystem sysRoleSystem = sysRoleSystemService.selectByMultiId(
                new SysRoleSystem().setRoleId(id)
        );
        RoleSystemParam result = new RoleSystemParam();
        if (ObjectUtil.isNotNull(sysRoleSystem)) {
            result.setRoleId(sysRoleSystem.getRoleId());
            result.setSystemIds(Arrays.asList(sysRoleSystem.getSystemId().split(",")));
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增角色与系统关系")
    @ApiImplicitParam(name = "param", value = "新增数据", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated RoleSystemParam param) {
        String methodDescribe = getMethodDescribe("add");
        SysRoleSystem sysRoleSystem = new SysRoleSystem()
                .setRoleId(param.getRoleId())
                .setSystemId(String.join(",",param.getSystemIds()));
        boolean result = sysRoleSystemService.saveOrUpdateByMultiId(sysRoleSystem);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


}

