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.SysUserConvert;
import top.imwork.window.silos.entity.upms.AllotRoles;
import top.imwork.window.silos.pojo.bo.SysUserBO;
import top.imwork.window.silos.pojo.bo.SysUserResultBO;
import top.imwork.window.silos.pojo.po.SysUserListParamVO;
import top.imwork.window.silos.pojo.po.SysUserParamVO;
import top.imwork.window.silos.service.upms.ISysUserService;

import java.util.List;

/**
 * 用户信息
 *
 * @author jiangxiaowei
 * email: e-jiangxiaowei@outlook.com
 * date: 2025-12-09 16:29:22
 */
@RestController
@RequestMapping("/upms/sysuser")
public class SysUserServiceController extends BaseController {

    @Resource
    public ISysUserService sysUserService;

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

    /**
     * 新增/修改info
     * @param sysUserParamVO 实体信息
     * @return responseMsg 操作结果
     */
    @PostMapping("/save")
    public ResponseMsg save(@RequestBody @Valid SysUserParamVO sysUserParamVO){
        SysUserBO sysUserBO = sysUserService.save(SysUserConvert.sysUserParamVoToDto(sysUserParamVO));
        return ResponseMsg.buildResponse(SysUserConvert.sysUserBoToVo(sysUserBO));
    }

    /**
     * 根据id删除用户信息
     * @param id id
     * @return true/false 成功/失败
     */
    @GetMapping("/delete/{id}")
    public ResponseMsg delete(@PathVariable("id") Long id){
        String[] idArr = new String[1];
        idArr[0] = String.valueOf(id);
        return sysUserService.deleteByIds(idArr)? ResponseMsg.ok() : ResponseMsg.fail();
    }

    /**
     * 批量逻辑删除信息
     *
     * @param dto 删除条件
     * @return true/false 是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResponseMsg deleteByIds(@RequestBody DeleteParams dto){
        return sysUserService.deleteByIds(dto.getIdsArr())? ResponseMsg.ok() : ResponseMsg.fail();
    }

    @PostMapping("/queryPage")
    public ResponseMsg queryPage(@RequestBody @Valid SysUserListParamVO sysUserListParamVO) {
        SysUserResultBO sysUserResultBO = sysUserService.queryPage(SysUserConvert.sysUserListParamVoToDTO(sysUserListParamVO));
        return ResponseMsg.buildResponse(SysUserConvert.sysUserResultBoToVo(sysUserResultBO));
    }

    /**
     * 根据用户id查询所拥有的角色
     * @param id
     * @return
     */
    @GetMapping("/queryAllotRoles/{id}")
    public ResponseMsg queryAllotRoles(@PathVariable("id") Long id){
        List<AllotRoles> list = sysUserService.queryAllotRoles(id);
        return ResponseMsg.buildResponse(list);
    }

}
