package com.kingdee.eas.custom.integration.perf.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.perf.dto.BatchPerfEvalSyncRequest;
import com.kingdee.eas.custom.integration.perf.dto.PerfEvalSyncResponse;
import com.kingdee.eas.custom.integration.perf.service.PerfEvalSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 第三方绩效系统数据同步接口。
 * X-Api-Secret 认证，单次上限 200 条，支持部分成功。
 */
@RestController
@RequestMapping("/perf")
public class PerfEvalSyncController extends AbstractIntegrationController {

    @Autowired
    private PerfEvalSyncService perfEvalSyncService;

    /**
     * 绩效考评结果批量同步接口。
     * 以工号+年月为唯一键写入 PerfEvalSync 实体，存在则更新（仅暂存状态），不存在则新增。
     * 入参：工号、年月(yyyy-MM)、绩效系数。
     */
    @PostMapping("/eval-result/sync")
    @ApiDoc(name = "绩效考评结果同步", desc = "接收第三方绩效系统推送的考评结果数据，支持批量写入")
    public ApiResult<PerfEvalSyncResponse> syncEvalResult(
            @RequestBody BatchPerfEvalSyncRequest request) {
        requireNotEmpty(request.getRecords(), "records");
        requireTrue(request.getRecords().size() <= 200,
                "单次写入记录数不能超过200条，当前: " + request.getRecords().size());
        log.info("收到绩效考评结果同步请求 - 记录数: {}",
                request.getRecords() != null ? request.getRecords().size() : 0);
        PerfEvalSyncResponse response = perfEvalSyncService.writePerfEvalSync(request);
        return success(response);
    }
}
