package cn.iocoder.yudao.module.base.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.common.CommonErrorCodeConstants;
import cn.iocoder.yudao.common.SystemEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.base.entity.RepairStaff;
import cn.iocoder.yudao.module.base.entity.RepairStaffSaveVo;
import cn.iocoder.yudao.module.base.service.RepairStaffService;
import cn.iocoder.yudao.module.staff.entity.CompanyStaff;
import cn.iocoder.yudao.module.staff.service.CompanyStaffService;
import cn.iocoder.yudao.module.staff.vo.CompanyStaffRespVO;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.dto.UserDTO;
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RolePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.permission.PermissionService;
import cn.iocoder.yudao.module.system.service.permission.RoleService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController
@RequestMapping("/base/repair-staff")
@RequiredArgsConstructor
public class RepairStaffController {
    private final AdminUserService userService;
    private final RoleService roleService;

    private final RepairStaffService repairStaffService;

    /**
     * 获取维修员工信息
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/get")
    public CommonResult<?> getRepairStaff(Long id) {
        // 查询信息
        UserDTO staff = repairStaffService.getStaff(id);
        if (staff == null) {
            return CommonResult.error(400, "用户不存在");
        }

        // 查询对应角色
        List<RoleDO> roleDOS = roleService.selectRoleByUserId(id);
        if (roleDOS == null) {
            roleDOS = Collections.emptyList();
        }

        RepairStaff repairStaff = BeanUtil.copyProperties(staff, RepairStaff.class);

        // 过滤出服务套餐为 weixiu 的
        List<RoleDO> weixiuRoleDOS = roleDOS.stream()
                .filter(item -> "weixiu".equals(item.getServicePackageId()))
                .collect(Collectors.toList());
        repairStaff.setRoles(weixiuRoleDOS);

        return success(repairStaff);
    }

    /**
     * 新增员工
     *
     * @param repairStaff 员工信息
     * @return
     */
    @PostMapping
    public CommonResult<?> addStaff(@RequestBody RepairStaffSaveVo repairStaff) {
        repairStaffService.addStaff(repairStaff);
        return CommonResult.ok();
    }

    /**
     * 新增员工
     *
     * @param repairStaff 员工信息
     * @return
     */
    @PostMapping("/insert")
    public CommonResult<?> insert(@RequestBody RepairStaffSaveVo repairStaff) {
        repairStaffService.insert(repairStaff);
        return CommonResult.ok();
    }

    /**
     * @Author 许
     * @Description 更新员工
     * @Date 10:38 2025/8/21
     * @Param [repairStaff]
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<?>
     **/
    @PostMapping("/updateStaff")
    public CommonResult<?> updateStaff(@RequestBody RepairStaffSaveVo repairStaff) {
        repairStaffService.updateStaff(repairStaff);
        return CommonResult.ok();
    }

    @GetMapping("/listSelectUser")
    public CommonResult<?> listSelectUser(@Valid UserPageReqVO pageReqVO) {
        // 查询目前所有的userId
        RolePageReqVO role = new RolePageReqVO();
        role.setPageNo(1);
        role.setPageSize(1000000);
        IPage<UserDTO> userDTOIPage = roleService.selectListByRoleIdRepair(role);
        // 获取所有的userId
        List<Long> ids = userDTOIPage.getRecords().stream().map(UserDTO::getId).collect(Collectors.toList());
        pageReqVO.setUserIds(ids);
        pageReqVO.setUserType("01");
        // 获得用户分页列表
        PageResult<AdminUserDO> pageResult = userService.getUserPage(pageReqVO);
        return success(new PageResult<>(pageResult.getList(), pageResult.getTotal()));
    }

    @DeleteMapping("/deleteStaff")
    public CommonResult<?> deleteStaff(@RequestBody List<Long> ids) {
        repairStaffService.deleteStaff(ids);
        return CommonResult.ok();
    }

    /**
     * @description: 员工新增文件夹
     * @param: userId   员工id
     * @return: cn.iocoder.yudao.framework.common.pojo.CommonResult<?>
     * @author 許
     * @date: 2025/8/11 11:50
     */
    @PostMapping("/addFolder")
    public CommonResult<?> addFolder(@RequestBody Long userId) {
        return success(repairStaffService.addFolder(userId));
    }

}
