package com.aegis.web.controller.resource.team;

import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.core.page.TableDataInfo;
import com.aegis.common.enums.BusinessType;
import com.aegis.resource.team.domain.TeamMember;
import com.aegis.resource.team.service.IResourceTeamMemberService;

/**
 * 队伍成员Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/resources/teams/{teamId}/members")
public class ResourceTeamMemberController extends BaseController {

    @Autowired
    private IResourceTeamMemberService teamMemberService;

    /**
     * 查询队伍成员列表
     */
    @PreAuthorize("@ss.hasPermi('resource:team:member:list')")
    @GetMapping
    public TableDataInfo list(@PathVariable Long teamId, TeamMember teamMember) {
        teamMember.setTeamId(teamId);
        startPage();
        List<TeamMember> list = teamMemberService.listByCondition(teamMember);
        return getDataTable(list);
    }

    /**
     * 根据ID获取成员详情
     */
    @PreAuthorize("@ss.hasPermi('resource:team:member:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable Long teamId, @PathVariable Long memberId) {
        TeamMember member = teamMemberService.getById(memberId);
        if (member != null && !member.getTeamId().equals(teamId)) {
            return error("成员不属于该队伍");
        }
        return success(member);
    }

    /**
     * 新增队伍成员
     */
    @PreAuthorize("@ss.hasPermi('resource:team:member:add')")
    @Log(title = "队伍成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@PathVariable Long teamId, @Validated @RequestBody TeamMember teamMember) {
        teamMember.setTeamId(teamId);
        return toAjax(teamMemberService.save(teamMember) ? 1 : 0);
    }

    /**
     * 修改队伍成员
     */
    @PreAuthorize("@ss.hasPermi('resource:team:member:edit')")
    @Log(title = "队伍成员", businessType = BusinessType.UPDATE)
    @PutMapping("/{memberId}")
    public AjaxResult edit(@PathVariable Long teamId, @PathVariable Long memberId, @Validated @RequestBody TeamMember teamMember) {
        TeamMember existingMember = teamMemberService.getById(memberId);
        if (existingMember == null) {
            return error("成员不存在");
        }
        if (!existingMember.getTeamId().equals(teamId)) {
            return error("成员不属于该队伍");
        }
        teamMember.setId(memberId);
        teamMember.setTeamId(teamId);
        return toAjax(teamMemberService.updateById(teamMember) ? 1 : 0);
    }

    /**
     * 删除队伍成员
     */
    @PreAuthorize("@ss.hasPermi('resource:team:member:remove')")
    @Log(title = "队伍成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long teamId, @PathVariable Long[] memberIds) {
        // 验证所有成员都属于该队伍
        for (Long memberId : memberIds) {
            TeamMember member = teamMemberService.getById(memberId);
            if (member == null || !member.getTeamId().equals(teamId)) {
                return error("存在不属于该队伍的成员");
            }
        }
        boolean removed = teamMemberService.removeByIds(Arrays.asList(memberIds));
        return removed ? success() : error("删除失败");
    }
}