package com.sundot.airport.web.controller.ledger;

import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.ledger.service.ILedgerSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 台账 → 配分事项 同步触发接口
 */
@RestController
@RequestMapping("/ledger/sync")
public class LedgerSyncController extends BaseController {

    @Autowired
    private ILedgerSyncService ledgerSyncService;

    /**
     * 全量同步所有台账到配分事项
     * POST /ledger/sync/all
     */
    @PreAuthorize("@ss.hasPermi('ledger:sync:all')")
    @PostMapping("/all")
    public AjaxResult syncAll() {
        ILedgerSyncService.SyncResult result = ledgerSyncService.syncAll();
        return AjaxResult.success(buildResultMap(result));
    }

    /**
     * 按台账类型同步（支持单表重跑）
     * POST /ledger/sync/{type}
     *
     * type 可选值：
     *   supervisionProblem / realtimeInterception / securityTest / unsafeEvent /
     *   seizureStats / rewardApproval / servicePatrol / complaint / terminalBonus /
     *   bannerLetter / examScore / rewardPenalty / leaveSpecial
     */
    @PreAuthorize("@ss.hasPermi('ledger:sync:all')")
    @PostMapping("/{type}")
    public AjaxResult syncByType(@PathVariable String type) {
        ILedgerSyncService.SyncResult result = ledgerSyncService.syncByType(type);
        return AjaxResult.success(buildResultMap(result));
    }

    private Map<String, Object> buildResultMap(ILedgerSyncService.SyncResult result) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("inserted", result.getTotalInserted());
        map.put("skipped", result.getTotalSkipped());
        map.put("detail", result.getDetail());
        return map;
    }
}
