package com.ruoyi.info.collection.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.collection.domain.excel.CcdiBaseStaffAssetInfoExcel;
import com.ruoyi.info.collection.domain.vo.BaseStaffAssetImportFailureVO;
import com.ruoyi.info.collection.domain.vo.ImportResultVO;
import com.ruoyi.info.collection.service.ICcdiBaseStaffAssetImportService;
import com.ruoyi.info.collection.utils.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工资产信息导入Controller
 *
 * @author ruoyi
 * @date 2026-03-13
 */
@Tag(name = "员工资产信息导入管理")
@RestController
@RequestMapping("/ccdi/baseStaff/asset")
public class CcdiBaseStaffAssetImportController extends BaseController {

    @Resource
    private ICcdiBaseStaffAssetImportService baseStaffAssetImportService;

    /**
     * 下载导入模板
     */
    @Operation(summary = "下载员工资产导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        EasyExcelUtil.importTemplateWithDictDropdown(response, CcdiBaseStaffAssetInfoExcel.class, "员工资产信息");
    }

    /**
     * 导入员工资产信息
     */
    @Operation(summary = "导入员工资产信息")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:import')")
    @Log(title = "员工资产信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        List<CcdiBaseStaffAssetInfoExcel> list = EasyExcelUtil.importExcel(file.getInputStream(), CcdiBaseStaffAssetInfoExcel.class);
        if (list == null || list.isEmpty()) {
            return warn("至少需要一条数据");
        }

        String taskId = baseStaffAssetImportService.importAssetInfo(list);
        ImportResultVO result = new ImportResultVO();
        result.setTaskId(taskId);
        result.setStatus("PROCESSING");
        result.setMessage("导入任务已提交,正在后台处理");
        return AjaxResult.success("导入任务已提交,正在后台处理", result);
    }

    /**
     * 查询导入状态
     */
    @Operation(summary = "查询员工资产导入状态")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:import')")
    @GetMapping("/importStatus/{taskId}")
    public AjaxResult getImportStatus(@PathVariable String taskId) {
        return success(baseStaffAssetImportService.getImportStatus(taskId));
    }

    /**
     * 查询导入失败记录
     */
    @Operation(summary = "查询员工资产导入失败记录")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:import')")
    @GetMapping("/importFailures/{taskId}")
    public TableDataInfo getImportFailures(
            @PathVariable String taskId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        List<BaseStaffAssetImportFailureVO> failures = baseStaffAssetImportService.getImportFailures(taskId);
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, failures.size());
        if (fromIndex >= failures.size()) {
            return getDataTable(new ArrayList<>(), failures.size());
        }
        return getDataTable(failures.subList(fromIndex, toIndex), failures.size());
    }
}
