package jnpf.notice.controller.app;


import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.common.PageDto;
import jnpf.model.notice.dto.FtbNoticeUserGroupMembersDto;
import jnpf.model.notice.dto.FtbNoticeUserGroupsDto;
import jnpf.model.notice.req.group.AddUserGroupReq;
import jnpf.model.notice.req.group.ListGroupUserMemberReq;
import jnpf.model.notice.req.group.UpdateUserGroupReq;
import jnpf.notice.service.FtbNoticeUserGroupMembersService;
import jnpf.notice.service.FtbNoticeUserGroupsService;
import jnpf.util.FtbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * app用户分组表模块
 *
 * @author xgl
 * @since 2024-05-08 09:51:36
 */
@RestController
@RequestMapping("/app/noticeUserGroup")
public class AppNoticeUserGroupsController {
    /**
     * 服务对象
     */
    @Autowired
    private FtbNoticeUserGroupsService ftbNoticeUserGroupsService;

    @Autowired
    private FtbNoticeUserGroupMembersService ftbNoticeUserGroupMembersService;

    @Autowired
    private UserApiV2Util userApiV2Util;


    /**
     * 分页查询分组列表
     *
     * @return {@link ActionResult}<{@link FtbNoticeUserGroupsDto}>
     */
    @GetMapping("/queryPage")
    public ActionResult<PageListVO<FtbNoticeUserGroupsDto>> queryAppPage(@Validated PageDto req) {
        PageInfo<FtbNoticeUserGroupsDto> pageVo = ftbNoticeUserGroupsService.queryAppPage(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }


    /**
     * 分页查询一个分组的 人员列表
     *
     * @param req
     * @return {@link ActionResult}<{@link FtbNoticeUserGroupMembersDto}>
     */
    @GetMapping("/queryList/{groupId}")
    public ActionResult<PageListVO<FtbNoticeUserGroupMembersDto>> pageLists(@PathVariable("groupId") String groupId, @Validated PageDto req) {
        ListGroupUserMemberReq selectReq = new ListGroupUserMemberReq();
        selectReq.setCurrentPage(req.getCurrentPage());
        selectReq.setPageSize(req.getPageSize());
        selectReq.setSelectFlowerAndHeadLogo(true);
        //查询离职人员
//        List<String> leaveUserIds = userApiV2Util.queryLeaveUserId();
        List<String> powerUserIds = userApiV2Util.getPermissionUserIds();
        PageInfo<FtbNoticeUserGroupMembersDto> pageVo = ftbNoticeUserGroupMembersService.getPageList(powerUserIds, groupId, selectReq);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }


    /**
     * 添加分组
     *
     * @param req 参数
     * @return {@link ActionResult}<{@link String}>
     */
    @PostMapping("/add")
    public ActionResult<String> add(@Validated @RequestBody AddUserGroupReq req) {
        return ActionResult.success("成功",  ftbNoticeUserGroupsService.insertData(req));
    }


    /**
     * 编辑分组
     *
     * @param id  分组主键ID
     * @param req
     * @return {@link ActionResult}
     */
    @PutMapping("/update/{id}")
    public ActionResult update(@PathVariable("id") String id, @Validated @RequestBody UpdateUserGroupReq req) {
        ftbNoticeUserGroupsService.updateData(id, req);
        return ActionResult.success();
    }


    /**
     * 查询分组详情及分组的全部人员
     *
     * @param id 分组主键ID
     * @return {@link ActionResult}
     */
    @GetMapping("/get/{id}")
    public ActionResult<FtbNoticeUserGroupsDto> get(@PathVariable("id") String id) {
        return ActionResult.success("成功", ftbNoticeUserGroupsService.getInfo(id));
    }

    /**
     * 批量查询分组的人员列表
     *
     * @param groupIds 分组主键ID,逗号分开
     * @return {@link ActionResult}
     */
    @GetMapping("/queryBatchGroupUser/{groupIds}")
    public ActionResult<List<FtbNoticeUserGroupMembersDto>> queryBatchGroupUser(@PathVariable("groupIds") String groupIds) {
        return ActionResult.success("成功", ftbNoticeUserGroupsService.queryBatchGroupUserApp(groupIds));
    }

    /**
     * 删除分组
     *
     * @param id 分组主键ID，必传
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        ftbNoticeUserGroupsService.deleteData(id);
        return ActionResult.success();
    }

}

