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

import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.ledger.domain.*;
import com.sundot.airport.ledger.service.*;
import com.sundot.airport.ledger.service.ILedgerCombinedImportService;
import com.sundot.airport.ledger.domain.LedgerDailyTraining;
import com.sundot.airport.ledger.domain.LedgerLeaderDuty;
import com.sundot.airport.ledger.domain.LedgerHealthSoldier;
import com.sundot.airport.ledger.domain.LedgerDormFireSafety;
import com.sundot.airport.ledger.domain.LedgerTrainingIssue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 台账一键导入Controller
 * 提供所有台账类型的 Excel 批量上传接口
 */
@RestController
@RequestMapping("/ledger/import")
public class LedgerImportController extends BaseController {

    @Autowired private ILedgerSupervisionProblemService supervisionProblemService;
    @Autowired private ILedgerPatrolInspectionService patrolInspectionService;
    @Autowired private ILedgerRealtimeInterceptionService realtimeInterceptionService;
    @Autowired private ILedgerServicePatrolService servicePatrolService;
    @Autowired private ILedgerComplaintService complaintService;
    @Autowired private ILedgerSecurityTestService securityTestService;
    @Autowired private ILedgerChannelPassRateService channelPassRateService;
    @Autowired private ILedgerUnsafeEventService unsafeEventService;
    @Autowired private ILedgerSeizureStatsService seizureStatsService;
    @Autowired private ILedgerTerminalBonusService terminalBonusService;
    @Autowired private ILedgerExamScoreService examScoreService;
    @Autowired private ILedgerRewardApprovalService rewardApprovalService;
    @Autowired private ILedgerCombinedImportService combinedImportService;
    @Autowired private ILedgerDailyTrainingService dailyTrainingService;
    @Autowired private ILedgerLeaderDutyService leaderDutyService;
    @Autowired private ILedgerHealthSoldierService healthSoldierService;
    @Autowired private ILedgerDormFireSafetyService dormFireSafetyService;
    @Autowired private ILedgerTrainingIssueService trainingIssueService;
    @Autowired private ILedgerRewardPenaltyService rewardPenaltyService;
    @Autowired private ILedgerLeaveSpecialService leaveSpecialService;
    @Autowired private ILedgerBannerLetterService bannerLetterService;


    private String generateBatchNo() {
        return DateUtils.dateTimeNow("yyyyMMddHHmmss") + UUID.randomUUID().toString().substring(0, 6).toUpperCase();
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:supervision')")
    @Log(title = "台账导入-部门监察问题", businessType = BusinessType.IMPORT)
    @PostMapping("/supervisionProblem")
    public AjaxResult importSupervisionProblem(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerSupervisionProblem> util = new ExcelUtil<>(LedgerSupervisionProblem.class);
        List<LedgerSupervisionProblem> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        supervisionProblemService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:patrol')")
    @Log(title = "台账导入-队室三级质控巡查", businessType = BusinessType.IMPORT)
    @PostMapping("/patrolInspection")
    public AjaxResult importPatrolInspection(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerPatrolInspection> util = new ExcelUtil<>(LedgerPatrolInspection.class);
        List<LedgerPatrolInspection> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        patrolInspectionService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:interception')")
    @Log(title = "台账导入-实时质控拦截", businessType = BusinessType.IMPORT)
    @PostMapping("/realtimeInterception")
    public AjaxResult importRealtimeInterception(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerRealtimeInterception> util = new ExcelUtil<>(LedgerRealtimeInterception.class);
        List<LedgerRealtimeInterception> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        realtimeInterceptionService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:servicePatrol')")
    @Log(title = "台账导入-服务巡查", businessType = BusinessType.IMPORT)
    @PostMapping("/servicePatrol")
    public AjaxResult importServicePatrol(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerServicePatrol> util = new ExcelUtil<>(LedgerServicePatrol.class);
        List<LedgerServicePatrol> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        servicePatrolService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:complaint')")
    @Log(title = "台账导入-投诉情况", businessType = BusinessType.IMPORT)
    @PostMapping("/complaint")
    public AjaxResult importComplaint(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerComplaint> util = new ExcelUtil<>(LedgerComplaint.class);
        List<LedgerComplaint> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        complaintService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:securityTest')")
    @Log(title = "台账导入-安保测试", businessType = BusinessType.IMPORT)
    @PostMapping("/securityTest")
    public AjaxResult importSecurityTest(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerSecurityTest> util = new ExcelUtil<>(LedgerSecurityTest.class);
        List<LedgerSecurityTest> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        securityTestService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:channelPassRate')")
    @Log(title = "台账导入-通道过检率", businessType = BusinessType.IMPORT)
    @PostMapping("/channelPassRate")
    public AjaxResult importChannelPassRate(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerChannelPassRate> util = new ExcelUtil<>(LedgerChannelPassRate.class);
        List<LedgerChannelPassRate> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        channelPassRateService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:unsafeEvent')")
    @Log(title = "台账导入-不安全事件", businessType = BusinessType.IMPORT)
    @PostMapping("/unsafeEvent")
    public AjaxResult importUnsafeEvent(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerUnsafeEvent> util = new ExcelUtil<>(LedgerUnsafeEvent.class);
        List<LedgerUnsafeEvent> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        unsafeEventService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:seizureStats')")
    @Log(title = "台账导入-查获违规品统计", businessType = BusinessType.IMPORT)
    @PostMapping("/seizureStats")
    public AjaxResult importSeizureStats(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerSeizureStats> util = new ExcelUtil<>(LedgerSeizureStats.class);
        List<LedgerSeizureStats> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        seizureStatsService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:terminalBonus')")
    @Log(title = "台账导入-航站楼加分", businessType = BusinessType.IMPORT)
    @PostMapping("/terminalBonus")
    public AjaxResult importTerminalBonus(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerTerminalBonus> util = new ExcelUtil<>(LedgerTerminalBonus.class);
        // titleNum=1 表示跳过第1行标题，从第2行读取表头
        List<LedgerTerminalBonus> list = util.importExcel(file.getInputStream(), 1);
        
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        terminalBonusService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:examScore')")
    @Log(title = "台账导入-成绩收集", businessType = BusinessType.IMPORT)
    @PostMapping("/examScore")
    public AjaxResult importExamScore(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerExamScore> util = new ExcelUtil<>(LedgerExamScore.class);
        List<LedgerExamScore> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        examScoreService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:rewardApproval')")
    @Log(title = "台账导入-小额奖励审批单", businessType = BusinessType.IMPORT)
    @PostMapping("/rewardApproval")
    public AjaxResult importRewardApproval(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerRewardApproval> util = new ExcelUtil<>(LedgerRewardApproval.class);
        List<LedgerRewardApproval> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> {
            item.setImportBatch(batchNo);
            item.setSourceType("1");
            item.setCreateBy(getUsername());
        });
        rewardApprovalService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:dailyTraining')")
    @Log(title = "台账导入-日常培训记录", businessType = BusinessType.IMPORT)
    @PostMapping("/dailyTraining")
    public AjaxResult importDailyTraining(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerDailyTraining> util = new ExcelUtil<>(LedgerDailyTraining.class);
        List<LedgerDailyTraining> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> { item.setImportBatch(batchNo); item.setSourceType("1"); item.setCreateBy(getUsername()); });
        dailyTrainingService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:leaderDuty')")
    @Log(title = "台账导入-组长履职情况记录", businessType = BusinessType.IMPORT)
    @PostMapping("/leaderDuty")
    public AjaxResult importLeaderDuty(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerLeaderDuty> util = new ExcelUtil<>(LedgerLeaderDuty.class);
        List<LedgerLeaderDuty> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> { item.setImportBatch(batchNo); item.setSourceType("1"); item.setCreateBy(getUsername()); });
        leaderDutyService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:healthSoldier')")
    @Log(title = "台账导入-健康锐兵", businessType = BusinessType.IMPORT)
    @PostMapping("/healthSoldier")
    public AjaxResult importHealthSoldier(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerHealthSoldier> util = new ExcelUtil<>(LedgerHealthSoldier.class);
        List<LedgerHealthSoldier> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> { item.setImportBatch(batchNo); item.setSourceType("1"); item.setCreateBy(getUsername()); });
        healthSoldierService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:dormFireSafety')")
    @Log(title = "台账导入-宿舍消防安全专项自查", businessType = BusinessType.IMPORT)
    @PostMapping("/dormFireSafety")
    public AjaxResult importDormFireSafety(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerDormFireSafety> util = new ExcelUtil<>(LedgerDormFireSafety.class);
        List<LedgerDormFireSafety> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> { item.setImportBatch(batchNo); item.setSourceType("1"); item.setCreateBy(getUsername()); });
        dormFireSafetyService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    @PreAuthorize("@ss.hasPermi('ledger:import:trainingIssue')")
    @Log(title = "台账导入-培训台账问题通报", businessType = BusinessType.IMPORT)
    @PostMapping("/trainingIssue")
    public AjaxResult importTrainingIssue(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerTrainingIssue> util = new ExcelUtil<>(LedgerTrainingIssue.class);
        List<LedgerTrainingIssue> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> { item.setImportBatch(batchNo); item.setSourceType("1"); item.setCreateBy(getUsername()); });
        trainingIssueService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    /**
     * 部门奖惩记录表导入
     * LedgerRewardPenalty
     */
    @PreAuthorize("@ss.hasPermi('ledger:import:rewardPenalty')")
    @Log(title = "台账导入-部门奖惩记录表", businessType = BusinessType.IMPORT)
    @PostMapping("/rewardPenalty")
    public AjaxResult importRewardPenalty(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerRewardPenalty> util = new ExcelUtil<>(LedgerRewardPenalty.class);
        List<LedgerRewardPenalty> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> { item.setImportBatch(batchNo); item.setSourceType("1"); item.setCreateBy(getUsername()); });
        rewardPenaltyService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    /**
     * 请休假记录（特殊）表导入
     * LedgerLeaveSpecial
     */
    @PreAuthorize("@ss.hasPermi('ledger:import:leaveSpecial')")
    @Log(title = "台账导入-请休假记录特殊", businessType = BusinessType.IMPORT)
    @PostMapping("/leaveSpecial")
    public AjaxResult importLeaveSpecial(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerLeaveSpecial> util = new ExcelUtil<>(LedgerLeaveSpecial.class);
        List<LedgerLeaveSpecial> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> { item.setImportBatch(batchNo); item.setSourceType("1"); item.setCreateBy(getUsername()); });
        leaveSpecialService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }

    /**
     * 锦旗及感谢信记录表导入
     * LedgerBannerLetter
     */
    @PreAuthorize("@ss.hasPermi('ledger:import:bannerLetter')")
    @Log(title = "台账导入-锦旗及感谢信", businessType = BusinessType.IMPORT)
    @PostMapping("/bannerLetter")
    public AjaxResult importBannerLetter(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        ExcelUtil<LedgerBannerLetter> util = new ExcelUtil<>(LedgerBannerLetter.class);
        List<LedgerBannerLetter> list = util.importExcel(file.getInputStream(), 1);
        // 过滤null元素，避免空指针异常
        if (list != null) {
            list.removeIf(item -> item == null);
        }
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("导入数据为空或解析失败，请检查Excel表头是否与模板一致");
        }
        list.forEach(item -> { item.setImportBatch(batchNo); item.setSourceType("1"); item.setCreateBy(getUsername()); });
        bannerLetterService.batchInsert(list);
        return AjaxResult.success("导入成功，共" + list.size() + "条");
    }


    /**
     * 一键全量导入 - 解析多Sheet合并Excel（旅检三部"三三"数字管理平台.xlsx）
     */
    @PreAuthorize("@ss.hasPermi('ledger:import:combined')")
    @Log(title = "台账导入-一键全量导入", businessType = BusinessType.IMPORT)
    @PostMapping("/combined")
    public AjaxResult importCombined(@RequestParam("file") MultipartFile file) throws Exception {
        String batchNo = generateBatchNo();
        Map<String, String> result = combinedImportService.importAll(file, batchNo, getUsername());
        return AjaxResult.success(result);
    }

    /**
     * 按导入时间范围清理全部台账数据（20张表 + 台账同步配分事项）
     */
    @PreAuthorize("@ss.hasPermi('ledger:clear:all')")
    @Log(title = "台账数据清理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clear")
    public AjaxResult clearByTimeRange(
            @RequestParam String beginTime,
            @RequestParam String endTime) {
        if (beginTime == null || beginTime.isEmpty() || endTime == null || endTime.isEmpty()) {
            return AjaxResult.error("请选择清理时间范围");
        }
        //开始时间给
        beginTime = " " + beginTime + " 00:00:00";
        endTime = " " + endTime + " 23:59:59";
        Map<String, Integer> result = combinedImportService.clearByCreateTime(beginTime, endTime);
        int total = result.values().stream().mapToInt(Integer::intValue).sum();
        return AjaxResult.success("清理完成，共删除 " + total + " 条记录", result);
    }


}