package com.microservices.zone.member.controller;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.domain.R;
import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.domain.GenericsAjaxResult;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.common.security.annotation.InnerAuth;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.zone.member.domain.ZoneMember;
import com.microservices.zone.member.domain.vo.*;
import com.microservices.zone.member.service.IZoneMemberService;
import com.microservices.zone.utils.ZoneConstants;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 特色专区会员Controller
 *
 * @author otto
 * @date 2023-05-16
 */
@RestController
@RequestMapping("/member")
@Api(tags = "特色专区会员聚合接口")
public class ZoneMemberController extends BaseController {
    @Autowired
    private IZoneMemberService zoneMemberService;

    /**
     * 查询特色专区会员列表
     */
    @RequiresPermissions("zone:member:list")
    @GetMapping("/list")
    @ApiOperation("查询特色专区会员列表")
    public GenericsTableDataInfo<ZoneMemberDetailVo> list(ZoneMemberSearchVo zoneMemberSearchVo) {
        zoneMemberSearchVo.setIsAudit(ZoneConstants.MEMBER_AUDIT_PASS);
        startPage();
        return zoneMemberService.selectZoneMemberList(zoneMemberSearchVo, false);
    }

    /**
     * 查询特色专区审核会员列表
     */
    @RequiresPermissions("zone:member:auditList")
    @GetMapping("/auditList")
    @ApiOperation("查询特色专区审核会员列表")
    public GenericsTableDataInfo<ZoneMemberDetailVo> auditList(ZoneMemberAuditSearchVo zoneMemberSearchVo) {
        startPage();
        return zoneMemberService.selectZoneMemberAuditList(zoneMemberSearchVo.toZoneMemberSearchVo());
    }

    /**
     * 查询当前登录用户会员审核状态
     */
    @GetMapping("/zone/{zoneId}/auditStatus")
    @ApiOperation("查询当前登录用户会员审核状态")
    public AjaxResult auditStatus(@PathVariable("zoneId") Long zoneId) {
        String auditStatus = zoneMemberService.selectZoneMemberAuditStatus(zoneId);
        return AjaxResult.success("审核状态获取成功",auditStatus);
    }

    /**
     * 查询GitLink用户列表
     */
    @GetMapping("/gitlinkUserList")
    @ApiOperation("查询GitLink用户列表")
    public GenericsTableDataInfo<JSONObject> gitlinkUserList(GitLinkUserSearchVo gitLinkUserSearchVo) {
        return zoneMemberService.selectGitlinkUserList(null, gitLinkUserSearchVo);
    }

    /**
     * 查询组织下GitLink用户列表
     */
    @GetMapping("/dept/{deptId}/gitlinkUserList")
    @ApiOperation("查询组织下GitLink用户列表")
    public GenericsTableDataInfo<JSONObject> gitlinkUserListByDept(
            @ApiParam(name = "deptId", value = "组织Id") @PathVariable Long deptId
            , GitLinkUserSearchVo gitLinkUserSearchVo) {
        return zoneMemberService.selectGitlinkUserListByDept(deptId, gitLinkUserSearchVo);
    }

    /**
     * 查询GitLink用户列表
     */
    @GetMapping("/zone/{zoneId}/gitlinkUserList")
    @ApiOperation("查询专区下GitLink用户列表")
    public GenericsTableDataInfo<JSONObject> gitlinkUserListByZone(
            @ApiParam(name = "zoneId", value = "专区Id") @PathVariable Long zoneId
            , GitLinkUserSearchVo gitLinkUserSearchVo) {
        return zoneMemberService.selectGitlinkUserList(zoneId, gitLinkUserSearchVo);
    }

    /**
     * 获取特色专区会员详细信息
     */
    @RequiresPermissions("zone:member:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取特色专区会员详细信息")
    public GenericsAjaxResult<ZoneMember> getInfo(@PathVariable("id") Long id) {
        return genericsSuccess(zoneMemberService.selectZoneMemberById(id));
    }

    /**
     * 检查并获取组织下的会员列表
     */
    @InnerAuth
    @GetMapping(value = "/dept/{deptId}/users/{userIds}")
    @ApiOperation(value = "检查并获取组织下的会员列表", hidden = true)
    public R<List<Long>> checkUsersIsMemberInDept(@PathVariable("deptId") Long deptId, @PathVariable("userIds") Long[] userIds) {
        return R.ok(zoneMemberService.checkUsersIsMemberInDept(deptId, userIds));
    }

    /**
     * 检查并获取用户所在组织的会员列表
     */
    @InnerAuth
    @GetMapping(value = "/user/{userId}/deptIds/{deptIds}")
    @ApiOperation(value = "检查并获取用户所在组织的会员列表", hidden = true)
    public R<List<Long>> checkUserIsMemberInDepts(@PathVariable("userId") Long userId, @PathVariable("deptIds") Long[] deptIds) {
        return R.ok(zoneMemberService.checkUserIsMemberInDepts(userId, deptIds));
    }

    /**
     * 新增特色专区会员
     */
    @RequiresPermissions("zone:member:add")
    @Log(title = "特色专区会员", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增特色专区会员")
    public AjaxResult add(@RequestBody @Validated ZoneMemberInputVo zoneMemberInputVo) {
        return toAjax(zoneMemberService.insertZoneMember(zoneMemberInputVo));
    }

    /**
     * 申请加入专区
     */
    @Log(title = "特色专区会员-申请加入专区", businessType = BusinessType.INSERT)
    @PostMapping("/applyToJoin/zone/{zoneId}")
    @ApiOperation("申请加入专区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "zoneId", value = "专区Id", paramType = "path", dataType = "Long"),
    })
    public AjaxResult applyToJoinZone(@PathVariable("zoneId") Long zoneId,
                                      @RequestBody @Validated ZoneMemberApplyVo zoneMemberApplyVo) {
        return toAjax(zoneMemberService.applyToJoinZone(zoneId, zoneMemberApplyVo));
    }

    /**
     * 修改特色专区会员
     */
    @RequiresPermissions("zone:member:edit")
    @Log(title = "特色专区会员", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    @ApiOperation("修改特色专区会员")
    public AjaxResult edit(@PathVariable("id") Long id, @RequestBody ZoneMemberUpdateVo zoneMemberUpdateVo) {
        return toAjax(zoneMemberService.updateZoneMember(id, zoneMemberUpdateVo));
    }

    /**
     * 审核特色专区会员
     */
    @RequiresPermissions("zone:member:audit")
    @Log(title = "审核特色专区会员", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/member/{memberId}")
    @ApiOperation("审核特色专区会员")
    public AjaxResult audit(@PathVariable("memberId") Long memberId, @RequestBody AuditInputVo auditInputVo) {
        return toAjax(zoneMemberService.auditZoneMember(memberId, auditInputVo));
    }

    /**
     * 删除特色专区会员
     */
    @RequiresPermissions("zone:member:remove")
    @Log(title = "特色专区会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除特色专区会员")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(zoneMemberService.deleteZoneMemberByIds(ids));
    }

    /**
     * 设置是否首页展示
     */
    @RequiresPermissions("zone:member:homepage")
    @Log(title = "特色专区会员", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/homepage")
    @ApiOperation("设置是否首页展示")
    public AjaxResult updateHomepageShow(@PathVariable Long id) {
        return toAjax(zoneMemberService.updateZoneMemberHomePageShow(id));
    }
}
