package com.microservices.cms.project.controller;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.cms.project.domain.CmsProject;
import com.microservices.cms.project.service.ICmsProjectService;
import com.microservices.common.core.domain.R;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
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.system.api.domain.SysDept;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 专区对应项目Controller
 *
 * @author otto
 * @date 2023-03-27
 */
@Api(tags = "组织对应专区项目相关接口")
@RestController
@RequestMapping("/project")
public class CmsProjectController extends BaseController {
    @Autowired
    private ICmsProjectService cmsProjectService;

    /**
     * 修改用户所在GItLink项目中的权限
     */
    @InnerAuth
    @PutMapping("/changeRole/user/{userId}")
    @ApiOperation(value = "修改用户所在GItLink项目中的权限", hidden = true)
    public R<Boolean> changeUserRole(@PathVariable("userId") Long userId,
                                     @RequestParam(value = "oldDeptId", required = false) Long oldDeptId,
                                     @RequestParam(value = "newDeptId", required = false) Long newDeptId,
                                     @RequestParam(value = "oldRoleId", required = false) Long oldRoleId,
                                     @RequestParam(value = "newRoleId", required = false) Long newRoleId
    ) {
        Boolean changeRoleResult = cmsProjectService.changeUserRole(userId, oldDeptId, newDeptId, oldRoleId, newRoleId);
        return R.ok(changeRoleResult);
    }

    /**
     * 查询专区对应项目列表
     */
    @RequiresPermissions("cms:project:list")
    @ApiOperation("查询专区对应项目列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "组织名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc", paramType = "query", dataType = "String")
    })
    public GenericsTableDataInfo<CmsProject> list(@ApiIgnore CmsProject cmsProject) {
        List<CmsProject> list = cmsProjectService.selectCmsProjectList(cmsProject);
        if (list == null) {
            throw new ServiceException("查询专区对应项目列表失败");
        }
        return getAllGenericsDataTableToPage(list);
    }

    /**
     * 通过组织id新增专区对应项目
     */
    @RequiresPermissions("cms:project:add")
    @ApiOperation("基于组织创建特色专区项目")
    @Log(title = "专区对应项目", businessType = BusinessType.INSERT)
    @PostMapping("/dept/{deptId}")
    public AjaxResult addByDept(
            @ApiParam(name = "deptId", value = "组织Id") @PathVariable(value = "deptId") Long deptId) {
        boolean result = false;
        Long projectId = cmsProjectService.insertZoneCmsProjectByDept(deptId);
        if (projectId != null && projectId != 0) {
            result = true;
        }
        return toAjax(result);
    }

    /**
     * 通过组织id获取专区对应项目
     */
    @InnerAuth
    @ApiOperation("通过组织id获取专区对应项目")
    @GetMapping("/getProjectByDeptId/{deptId}")
    public R<String> getProjectByDeptId(
            @ApiParam(name = "deptId", value = "组织Id") @PathVariable(value = "deptId") Long deptId) {
        CmsProject cmsProject = cmsProjectService.selectCmsProjectByDeptId(deptId);
        return R.ok(JSONObject.toJSONString(cmsProject));
    }

    /**
     * 内部接口，新增组织时自动创建GitLink项目
     *
     * @param deptId 组织Id
     * @return 新创建项目的Id
     */
    @InnerAuth
    @PostMapping("/autoAddZoneCmsProjectByDept/{deptId}}")
    @ApiOperation(value = "新增组织时自动创建GitLink项目", hidden = true)
    public R<Long> autoAddZoneCmsProjectByDept(@PathVariable(value = "deptId") Long deptId) {
        Long projectId = cmsProjectService.insertZoneCmsProjectByDept(deptId);
        return R.ok(projectId);
    }


    /**
     * 内部接口，修改组织时自动更新GitLink项目
     */
    @InnerAuth
    @PutMapping("/autoUpdateZoneCmsProjectByDept")
    @ApiOperation(value = "修改组织时自动更新GitLink项目", hidden = true)
    public R<Boolean> autoUpdateZoneCmsProjectByDept(@RequestBody SysDept dept) {
        Boolean addResult = cmsProjectService.updateCmsProjectByDept(dept);
        return R.ok(addResult);
    }

    /**
     * 内部接口，修改专区审核状态时，自动更新GitLink项目分支保护设置
     */
    @InnerAuth
    @PutMapping("/autoUpdateZoneCmsProjectWhenAuditStatusChangedByDept")
    @ApiOperation(value = "修改专区审核状态时自动更新GitLink项目分支保护设置", hidden = true)
    public R<Boolean> autoOperateZoneCmsProjectWhenAuditStatusChangedByDept(@RequestParam(value = "userName", required = true) String userName,
                                                                            @RequestParam(value = "identifier", required = true) String identifier,
                                                                            @RequestParam(value = "deptId", required = true) Long deptId,
                                                                            @ApiParam("操作类型： create || update || delete") @RequestParam(value = "operationType", required = true) String operationType) {
        Boolean addResult = cmsProjectService.operateProtectedBranchByZoneAuditSetting(userName, identifier, deptId, operationType);
        return R.ok(addResult);
    }

    /**
     * 获取当前已创建项目的组织以及对应项目URL
     */
    @RequiresPermissions("cms:project:list")
    @ApiOperation("获取当前已创建项目的组织以及对应项目URL")
    @GetMapping("/deptProjectList")
    public AjaxResult getDeptProjectList() {
        return AjaxResult.success(cmsProjectService.getDeptProjectList());
    }

    /**
     * 通过组织Id批量删除专区对应项目
     */
    @RequiresPermissions("cms:project:remove")
    @Log(title = "专区对应项目", businessType = BusinessType.DELETE)
    @ApiOperation("通过组织Id批量删除专区对应项目")
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@ApiParam(name = "deptIds", value = "组织Id列表") @PathVariable Long[] deptIds) {
        return toAjax(cmsProjectService.deleteCmsProjectByIds(deptIds));
    }

    /**
     * 内部接口-通过组织Id删除专区对应项目
     */
    @InnerAuth
    @Log(title = "专区对应项目", businessType = BusinessType.DELETE)
    @ApiOperation(value = "内部接口-通过组织Id删除专区对应项目", hidden = true)
    @DeleteMapping("/removeByDeptId/{deptId}")
    public R<Boolean> removeByDeptId(@PathVariable(value = "deptId") Long deptId) {
        return R.ok(cmsProjectService.deleteCmsProjectByDeptId(deptId) > 0);
    }
}
