package com.kingdee.eas.custom.integration.mdm.controller;

import com.kingdee.eas.custom.integration.base.annotation.ApiDoc;
import com.kingdee.eas.custom.integration.base.controller.AbstractIntegrationController;
import com.kingdee.eas.custom.integration.base.dto.ApiResult;
import com.kingdee.eas.custom.integration.mdm.dto.MdmSyncRequest;
import com.kingdee.eas.custom.integration.mdm.dto.MdmSyncResponse;
import com.kingdee.eas.custom.integration.mdm.enums.MdmEntityEnum;
import com.kingdee.eas.custom.integration.mdm.service.MdmMasterDataService;
import org.springframework.web.bind.annotation.*;

/**
 * MDM 主数据同步接口。
 * 专属端点绑定实体，通用端点通过 bosType 识别。X-Api-Secret 认证，单次上限 200 条。
 */
@RestController
@RequestMapping("/mdm")
public class MdmMasterDataController extends AbstractIntegrationController {

    private final MdmMasterDataService mdmService = new MdmMasterDataService();

    /**
     * 成本中心数据写入接口。
     */
    @PostMapping("/cost-center/sync")
    @ApiDoc(name = "成本中心同步", desc = "接收 MDM 系统推送的成本中心基础资料，支持批量写入")
    public ApiResult<MdmSyncResponse> syncCostCenter(@RequestBody MdmSyncRequest request) {
        requireNotEmpty(request.getRecords(), "records");
        requireTrue(request.getRecords().size() <= 200,
                "单次写入记录数不能超过200条，当前: " + request.getRecords().size());
        log.info("收到成本中心同步请求 - 记录数: {}", request.getRecords().size());
        return success(mdmService.save(MdmEntityEnum.COST_CENTER, request.getRecords()));
    }

    /**
     * 研发项目数据写入接口。
     */
    @PostMapping("/dev-project/sync")
    @ApiDoc(name = "研发项目同步", desc = "接收 MDM 系统推送的研发项目基础资料，支持批量写入")
    public ApiResult<MdmSyncResponse> syncDevProject(@RequestBody MdmSyncRequest request) {
        requireNotEmpty(request.getRecords(), "records");
        requireTrue(request.getRecords().size() <= 200,
                "单次写入记录数不能超过200条，当前: " + request.getRecords().size());
        log.info("收到研发项目同步请求 - 记录数: {}", request.getRecords().size());
        return success(mdmService.save(MdmEntityEnum.DEV_PROJECT, request.getRecords()));
    }

    /**
     * 核算主体数据写入接口。
     */
    @PostMapping("/account-entity/sync")
    @ApiDoc(name = "核算主体同步", desc = "接收 MDM 系统推送的核算主体基础资料，支持批量写入")
    public ApiResult<MdmSyncResponse> syncAccountEntity(@RequestBody MdmSyncRequest request) {
        requireNotEmpty(request.getRecords(), "records");
        requireTrue(request.getRecords().size() <= 200,
                "单次写入记录数不能超过200条，当前: " + request.getRecords().size());
        log.info("收到核算主体同步请求 - 记录数: {}", request.getRecords().size());
        return success(mdmService.save(MdmEntityEnum.ACCOUNT_ENTITY, request.getRecords()));
    }

    /**
     * 通用基础资料写入接口，通过 bosType 识别实体。
     */
    @PostMapping("/{bosType}/sync")
    @ApiDoc(name = "MDM 通用同步", desc = "通过 BOS 类型识别实体，支持任意基础资料的批量写入")
    public ApiResult<MdmSyncResponse> syncByBosType(
            @PathVariable String bosType, @RequestBody MdmSyncRequest request) {
        requireNotEmpty(request.getRecords(), "records");
        requireTrue(request.getRecords().size() <= 200,
                "单次写入记录数不能超过200条，当前: " + request.getRecords().size());
        MdmEntityEnum entity = MdmEntityEnum.fromBosType(bosType);
        log.info("收到 MDM 通用同步请求 - bosType: {}, 记录数: {}", bosType, request.getRecords().size());
        return success(mdmService.save(entity, request.getRecords()));
    }
}
