package com.microservices.pms.enterprise.controller;

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.pms.enterprise.domain.PmsEnterprise;
import com.microservices.pms.enterprise.domain.vo.*;
import com.microservices.pms.enterprise.service.IPmsEnterpriseService;
import com.microservices.system.api.domain.SimpleSysRole;
import com.microservices.system.api.domain.SimpleSysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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-10-10
 */
@RestController
@Api(tags = "项目管理-企业相关接口")
@RequestMapping("/pmsEnterprise")
public class PmsEnterpriseController extends BaseController
{
    @Autowired
    private IPmsEnterpriseService pmsEnterpriseService;

    /**
     * 切换用户在Gitlink组织团队
     */
    @InnerAuth
    @PutMapping("/changeUserGitlinkRole/user/{userId}/dept/{deptId}")
    @ApiOperation(value = "切换用户在Gitlink组织团队", hidden = true)
    public R<Boolean> changeUserGitlinkRole(@PathVariable("userId") Long userId,
                                            @PathVariable("deptId") Long deptId,
                                            @RequestParam(value = "oldRoleId", required = false) Long oldRoleId,
                                            @RequestParam(value = "newRoleId", required = false) Long newRoleId) {
        Boolean changeRoleResult = pmsEnterpriseService.changeUserGitlinkRole(userId, deptId, oldRoleId, newRoleId);
        return R.ok(changeRoleResult);
    }

    /**
     * 查询我的项目管理企业列表
     */
    @GetMapping("/myList")
    @ApiOperation(value = "查询我的项目管理企业列表")
    public GenericsTableDataInfo<PmsEnterprise> list(PmsEnterpriseSearchVo pmsEnterpriseSearchVo) {
        startPage();
        return pmsEnterpriseService.selectMyPmsEnterpriseList(pmsEnterpriseSearchVo.toPmsEnterprise());
    }

    /**
     * 根据标识获取项目管理企业详细信息
     */
    @GetMapping("/{enterpriseIdentifier}")
    @ApiOperation(value = "根据标识获取项目管理企业详细信息（路由更新删除/ByIdentifier）")
    public GenericsAjaxResult<PmsEnterprise> getInfoByIdentifier(
            @ApiParam(name = "enterpriseIdentifier", value = "企业标识") @PathVariable String enterpriseIdentifier)
    {
        return genericsSuccess(pmsEnterpriseService.selectPmsEnterpriseByIdentifier(enterpriseIdentifier));
    }

    /**
     * 通过企业标识查询企业下用户列表
     */
    @GetMapping("/{enterpriseIdentifier}/userList")
    @ApiOperation("通过企业标识查询企业下用户列表（路由更新删除/ByIdentifier）")
    public GenericsTableDataInfo<SimpleSysUser> getUserListById(
            @ApiParam(name = "enterpriseIdentifier", value = "企业标识") @PathVariable String enterpriseIdentifier,
            @ApiParam(name = "search", value = "模糊查询") String search,
            @ApiParam(name = "pageNum", value = "当前记录起始索引") Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页显示记录数") Integer pageSize) {
        return pmsEnterpriseService.getUserListByEnterpriseIdentifier(enterpriseIdentifier, search, pageNum, pageSize);
    }

    /**
     * 新增项目管理企业
     */
    @Log(title = "项目管理企业", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增项目管理企业")
    public AjaxResult add(@RequestBody @Validated PmsEnterpriseInputVo pmsEnterpriseInputVo)
    {
        return success((Object) pmsEnterpriseService.insertPmsEnterprise(pmsEnterpriseInputVo.toPmsEnterprise()));
    }

    /**
     * Gitlink组织升级为企业
     */
    @Log(title = "项目管理企业", businessType = BusinessType.INSERT)
    @PostMapping("/createByGitlinkOrgId/{gitlinkOrgId}")
    @ApiOperation(value = "Gitlink组织升级为企业")
    public GenericsAjaxResult<String> createByGitlinkOrgId(
            @ApiParam(name = "gitlinkOrgId", value = "gitlink组织Id") @PathVariable Long gitlinkOrgId,
            @RequestBody List<GitLinkUserToEnterpriseInputVo> gitLinkUserToEnterpriseInputVoList) {
        return genericsSuccess(pmsEnterpriseService.createByGitlinkOrgId(gitlinkOrgId, gitLinkUserToEnterpriseInputVoList));
    }

    /**
     * 修改项目管理企业
     */
    @RequiresPermissions("pms:pmsEnterprise:edit")
    @Log(title = "项目管理企业", businessType = BusinessType.UPDATE)
    @PutMapping("/{enterpriseIdentifier}")
    @ApiOperation(value = "修改项目管理企业")
    public AjaxResult edit(
            @ApiParam(name = "enterpriseIdentifier", value = "企业标识") @PathVariable String enterpriseIdentifier,
            @RequestBody @Validated PmsEnterpriseUpdateVo pmsEnterpriseUpdateVo) {
        return toAjax(pmsEnterpriseService.updatePmsEnterprise(pmsEnterpriseUpdateVo.toPmsEnterprise(enterpriseIdentifier)));
    }

    /**
     * 删除项目管理企业
     */
    @RequiresPermissions("pms:pmsEnterprise:remove")
    @Log(title = "项目管理企业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{enterpriseIdentifier}")
    @ApiOperation(value = "删除项目管理企业")
    public AjaxResult remove(@ApiParam(name = "enterpriseIdentifier", value = "企业标识") @PathVariable String enterpriseIdentifier,
                             @ApiParam(name = "password", value = "删除企业需输入用户密码", required = true) @RequestParam("password") String password) {
        return toAjax(pmsEnterpriseService.deletePmsEnterpriseByIdentifier(enterpriseIdentifier, password));
    }

    /**
     * 获取Gitlink组织下用户列表
     */
    @GetMapping("/gitlinkUserList/{gitlinkOrgId}")
    @ApiOperation(value = "获取Gitlink组织下用户列表")
    public GenericsTableDataInfo<GitLinkUserToEnterpriseVo> getGitlinkUserList(@ApiParam(name = "gitlinkOrgId", value = "gitlink组织Id") @PathVariable Long gitlinkOrgId) {
        return pmsEnterpriseService.getGitlinkUserList(gitlinkOrgId);
    }

    /**
     * 获取可选角色列表
     */
    @GetMapping("/roleList")
    @ApiOperation(value = "获取可选角色列表")
    public GenericsTableDataInfo<SimpleSysRole> getRoleList() {
        return pmsEnterpriseService.getRoleList();
    }

    /**
     * 通过组织标识获取工作台访问地址（未开通返回null）
     */
    @GetMapping("/{gitlinkOrgId}/workbenchUrl")
    @ApiOperation(value = "通过组织标识获取工作台访问地址（未开通返回null）")
    public GenericsAjaxResult<WorkbenchUrlDataVo> getWorkbenchUrl(
            @ApiParam(name = "gitlinkOrgId", value = "gitlink组织Id") @PathVariable Long gitlinkOrgId) {
        return genericsSuccess(pmsEnterpriseService.getWorkbenchUrl(gitlinkOrgId));
    }
}
