package top.imwork.window.silos.controller.upms;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import top.imwork.commons.core.base.BaseController;
import top.imwork.commons.core.pojo.DeleteParams;
import top.imwork.commons.core.pojo.ResponseMsg;
import top.imwork.window.silos.convert.SysUserRoleConvert;
import top.imwork.window.silos.pojo.bo.SysUserRoleBO;
import top.imwork.window.silos.pojo.bo.SysUserRoleResultBO;
import top.imwork.window.silos.pojo.dto.SysUserRoleDTO;
import top.imwork.window.silos.pojo.po.SysUserRoleListParamVO;
import top.imwork.window.silos.pojo.po.SysUserRoleParamVO;
import top.imwork.window.silos.service.upms.ISysUserRoleService;

/**
 * 用户与角色关联信息
 *
 * @author jiangxiaowei
 * email: e-jiangxiaowei@outlook.com
 * date: 2025-12-09 16:29:22
 */
@RestController
@RequestMapping("/upms/sysuserrole")
public class SysUserRoleServiceController extends BaseController {

    @Resource
    public ISysUserRoleService sysUserRoleService;

    /**
     * 根据id查询数据
     * @param id long类型的id
     * @return 返回查询结果
     */
    @GetMapping("/info/{id}")
    public ResponseMsg info(@PathVariable("id") Long id){
        return ResponseMsg.buildResponse(sysUserRoleService.info(id));
    }

    /**
     * 新增/修改info
     * @param sysUserRoleParamVO 实体信息
     * @return responseMsg 操作结果
     */
    @PostMapping("/save")
    public ResponseMsg save(@RequestBody @Valid SysUserRoleParamVO sysUserRoleParamVO){
        SysUserRoleBO sysUserRoleBO = sysUserRoleService.save(SysUserRoleConvert.sysUserRoleParamVoToDto(sysUserRoleParamVO));
        return ResponseMsg.buildResponse(SysUserRoleConvert.sysUserRoleBoToVo(sysUserRoleBO));
    }

    /**
     * 根据id删除用户与角色关联信息
     * @param id id
     * @return true/false 成功/失败
     */
    @DeleteMapping("/delete/{id}")
    public ResponseMsg delete(@PathVariable("id") Long id){
        return sysUserRoleService.delete(id)? ResponseMsg.ok() : ResponseMsg.fail();
    }

    /**
     * 批量逻辑删除信息
     *
     * @param dto 删除条件
     * @return true/false 是否删除成功
     */
    @DeleteMapping("/deleteByIds")
    public ResponseMsg deleteByIds(@RequestBody DeleteParams dto){
        String[] idArr = dto.getIds().split(",");
        return sysUserRoleService.deleteByIds(idArr)? ResponseMsg.ok() : ResponseMsg.fail();
    }

    @PostMapping("/queryPage")
    public ResponseMsg queryPage(@RequestBody @Valid SysUserRoleListParamVO sysUserRoleListParamVO) {
        SysUserRoleResultBO sysUserRoleResultBO = sysUserRoleService.queryPage(SysUserRoleConvert.sysUserRoleListParamVoToDTO(sysUserRoleListParamVO));
        return ResponseMsg.buildResponse(SysUserRoleConvert.sysUserRoleResultBoToVo(sysUserRoleResultBO));
    }

    /**
     * 新增/修改info
     * @param sysUserRoleParamVO 实体信息
     * @return responseMsg 操作结果
     */
    @PostMapping("/allotRoles")
    public ResponseMsg allotRoles(@RequestBody @Valid SysUserRoleParamVO sysUserRoleParamVO){
        sysUserRoleService.allotRoles(SysUserRoleConvert.sysUserRoleParamVoToDto(sysUserRoleParamVO));
        return ResponseMsg.ok();
    }
}
