package com.keyware.shandan.bianmu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keyware.shandan.bianmu.entity.DirectoryMembersVo;
import com.keyware.shandan.bianmu.service.DirectoryMembersService;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.common.util.StreamUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/business/directory/members")
public class DirectoryMembersController {

    @Autowired
    private DirectoryMembersService membersService;

    @GetMapping("/page")
    public Result<Page<DirectoryMembersVo>> page(Page<DirectoryMembersVo> page, DirectoryMembersVo vo) {
        return Result.of(membersService.page(page, new QueryWrapper<>(vo)));
    }

    @GetMapping("/list/{dirId}")
    public Result<List<DirectoryMembersVo>> list(DirectoryMembersVo vo, @PathVariable String dirId) {
        vo.setDirectoryId(dirId);
        return Result.of(membersService.list(new QueryWrapper<>(vo)));
    }

    @PostMapping("/save")
    public Result<Object> save(SaveParams params) {
        return Result.of(membersService.saveOrUpdateBatch(params.getMembers(), 1));
    }

    @PostMapping("/delete")
    public Result<Object> delete(DeleteParams params) {
        return Result.of(membersService.removeByIds(params.getIds()));
    }

    @GetMapping("/user/get/{dirId}")
    public Result<Object> getUser(@PathVariable String dirId) {
        List<DirectoryMembersVo> members = membersService.listByDirId(dirId);
        List<MemberUser> users = StreamUtil.as(members).filter(DirectoryMembersVo::isUser)
                .map(dir -> MemberUser.of(dir.getMemberId(), dir.getMemberName()))
                .toList();
        return Result.of(users);
    }

    @GetMapping("/org/get/{dirId}")
    public Result<Object> getOrg(@PathVariable String dirId){
        List<DirectoryMembersVo> members = membersService.listByDirId(dirId);
        List<MemberOrg> orgs = StreamUtil.as(members).filter(DirectoryMembersVo::isOrg)
                .map(dir -> MemberOrg.of(dir.getMemberId(), dir.getMemberName()))
                .toList();
        return Result.of(orgs);
    }

    /**
     * 批量保存请求参数封装实体类
     */
    @Getter
    @Setter
    public static class SaveParams implements Serializable {
        private static final long serialVersionUID = 5687949552116728077L;
        private List<DirectoryMembersVo> members;
    }

    /**
     * 批量删除请求参数封装实体类
     */
    @Getter
    @Setter
    public static class DeleteParams implements Serializable {
        private static final long serialVersionUID = 5687949552116728077L;
        private List<Integer> ids;
    }

    @Getter
    @Setter
    @AllArgsConstructor
    public static class MemberUser {
        private String userId;
        private String userName;

        public static MemberUser of(String userId, String userName) {
            return new MemberUser(userId, userName);
        }
    }

    @Getter
    @Setter
    @AllArgsConstructor
    public static class MemberOrg {
        private String id;
        private String orgName;

        public static MemberOrg of(String id, String orgName) {
            return new MemberOrg(id, orgName);
        }
    }
}
