package com.yami.shop.platform.controller;

import com.yami.shop.bean.model.DeptUser;
import com.yami.shop.common.util.R;
import com.yami.shop.service.DeptUserService;
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.web.bind.annotation.*;

import java.util.List;

/**
 * 部门用户关联控制器
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("/admin/deptUser")
@AllArgsConstructor
@Api(tags = "部门用户关联接口")
public class DeptUserController {

    private final DeptUserService deptUserService;

    /**
     * 根据用户ID获取部门用户关联列表
     *
     * @param userId 用户ID
     * @return 部门用户关联列表
     */
    @GetMapping("/listByUserId/{userId}")
    @ApiOperation(value = "根据用户ID获取部门用户关联列表", notes = "根据用户ID获取部门用户关联列表")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "path")
    public R<List<DeptUser>> listByUserId(@PathVariable("userId") Long userId) {
        List<DeptUser> deptUserList = deptUserService.listByUserId(userId);
        return R.SUCCESS(deptUserList);
    }

    /**
     * 根据部门ID获取部门用户关联列表
     *
     * @param deptId 部门ID
     * @return 部门用户关联列表
     */
    @GetMapping("/listByDeptId/{deptId}")
    @ApiOperation(value = "根据部门ID获取部门用户关联列表", notes = "根据部门ID获取部门用户关联列表")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, dataType = "Long", paramType = "path")
    public R<List<DeptUser>> listByDeptId(@PathVariable("deptId") Long deptId) {
        List<DeptUser> deptUserList = deptUserService.listByDeptId(deptId);
        return R.SUCCESS(deptUserList);
    }

    /**
     * 保存用户部门关联
     *
     * @param userId  用户ID
     * @param deptIds 部门ID列表
     * @return 保存结果
     */
    @PostMapping("/saveUserDepts")
    @ApiOperation(value = "保存用户部门关联", notes = "保存用户与部门的关联关系")
    public R<Boolean> saveUserDepts(
            @RequestParam("userId") Long userId,
            @RequestBody List<Long> deptIds) {
        Boolean result = deptUserService.saveUserDept(userId, deptIds);
        return R.SUCCESS(result);
    }

    /**
     * 批量保存用户部门关联
     *
     * @param userIds 用户ID列表
     * @param deptId  部门ID
     * @return 保存结果
     */
    @PostMapping("/saveBatchUserDepts")
    @ApiOperation(value = "批量保存用户部门关联", notes = "批量保存用户与部门的关联关系")
    public R<Boolean> saveBatchUserDepts(
            @RequestBody List<Long> userIds,
            @RequestParam("deptId") Long deptId) {
        Boolean result = deptUserService.saveBatchUserDepts(userIds, deptId);
        return R.SUCCESS(result);
    }

    /**
     * 根据用户ID删除部门用户关联
     *
     * @param userId 用户ID
     * @return 删除结果
     */
    @DeleteMapping("/deleteByUserId/{userId}")
    @ApiOperation(value = "根据用户ID删除部门用户关联", notes = "根据用户ID删除部门用户关联")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "path")
    public R<Boolean> deleteByUserId(@PathVariable("userId") Long userId) {
        Boolean result = deptUserService.deleteByUserId(userId);
        return R.SUCCESS(result);
    }

    /**
     * 根据部门ID删除部门用户关联
     *
     * @param deptId 部门ID
     * @return 删除结果
     */
    @DeleteMapping("/deleteByDeptId/{deptId}")
    @ApiOperation(value = "根据部门ID删除部门用户关联", notes = "根据部门ID删除部门用户关联")
    @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, dataType = "Long", paramType = "path")
    public R<Boolean> deleteByDeptId(@PathVariable("deptId") Long deptId) {
        Boolean result = deptUserService.deleteByDeptId(deptId);
        return R.SUCCESS(result);
    }
}
