package com.yhy.module.core.controller.admin.workbench;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.workbench.vo.sync.ApplySyncReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.sync.SetSyncReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.sync.SyncLibraryDivisionRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.sync.SyncLibraryDivisionUpdateReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.sync.SyncPendingRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.sync.SyncSourceUnitRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.sync.UnsetSyncReqVO;
import com.yhy.module.core.service.workbench.SyncLibraryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 同步库 Controller
 *
 * @author yhy
 */
@Tag(name = "工作台 - 同步库")
@RestController
@RequestMapping("/core/wb/sync-library")
@Validated
public class SyncLibraryController {

    @Resource
    private SyncLibraryService syncLibraryService;

    @GetMapping("/tree")
    @Operation(summary = "获取项目同步库的分部分项树")
    @Parameter(name = "projectId", description = "项目ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:query')")
    public CommonResult<List<SyncLibraryDivisionRespVO>> getTree(
            @RequestParam("projectId") Long projectId) {
        return success(syncLibraryService.getTree(projectId));
    }

    @GetMapping("/source-units")
    @Operation(summary = "获取同步库的来源单位工程列表")
    @Parameter(name = "projectId", description = "项目ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:query')")
    public CommonResult<List<SyncSourceUnitRespVO>> getSourceUnits(
            @RequestParam("projectId") Long projectId) {
        return success(syncLibraryService.getSourceUnits(projectId));
    }

    @PutMapping("/update-division")
    @Operation(summary = "更新同步库分部分项节点")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:update')")
    public CommonResult<Boolean> updateDivision(@Valid @RequestBody SyncLibraryDivisionUpdateReqVO reqVO) {
        syncLibraryService.updateDivision(reqVO);
        return success(true);
    }

    @PostMapping("/set-sync")
    @Operation(summary = "设为同步")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:update')")
    public CommonResult<Boolean> setSync(@Valid @RequestBody SetSyncReqVO reqVO) {
        syncLibraryService.setSync(reqVO);
        return success(true);
    }

    @PostMapping("/unset-sync")
    @Operation(summary = "解除同步")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:update')")
    public CommonResult<Boolean> unsetSync(@Valid @RequestBody UnsetSyncReqVO reqVO) {
        syncLibraryService.unsetSync(reqVO);
        return success(true);
    }

    @PostMapping("/apply-sync")
    @Operation(summary = "套用同步")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:update')")
    public CommonResult<Boolean> applySync(@Valid @RequestBody ApplySyncReqVO reqVO) {
        syncLibraryService.applySync(reqVO);
        return success(true);
    }

    @GetMapping("/check-pending")
    @Operation(summary = "检查是否有待同步的变更")
    @Parameter(name = "compileTreeId", description = "单位工程ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:query')")
    public CommonResult<List<SyncPendingRespVO>> checkPending(
            @RequestParam("compileTreeId") Long compileTreeId) {
        return success(syncLibraryService.checkPending(compileTreeId));
    }
}
