package com.ruoyi.business.controller;

import com.ruoyi.business.domain.FileExchangePackageLog;
import com.ruoyi.business.service.IFileExchangePackageLogService;
import com.ruoyi.common.core.utils.file.FileUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 对外文件打包日志Controller
 * 
 * @author ruoyi
 * @date 2024-01-29
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/filePackageLog")
@RequiredArgsConstructor
public class FileExchangePackageLogController extends BaseController {

    private final IFileExchangePackageLogService fileExchangePackageLogService;

    /**
     * 查询对外文件打包日志列表
     */
    @RequiresPermissions("business_package_list")
    @GetMapping
    public AjaxResult list(FileExchangePackageLog fileExchangePackageLog) {
        List<FileExchangePackageLog> list = fileExchangePackageLogService.selectFileExchangePackageLogList(fileExchangePackageLog);
        return success(list);
    }

    /**
     * 查询对外文件打包日志列表(分页)
     */
    @RequiresPermissions("business_package_list")
    @GetMapping("/list")
    public TableDataInfo pageList(FileExchangePackageLog fileExchangePackageLog) {
        startPage();
        List<FileExchangePackageLog> list = fileExchangePackageLogService.selectFileExchangePackageLogList(fileExchangePackageLog);
        return getDataTable(list);
    }

    /**
     * 获取对外文件打包日志详细信息
     */
    @RequiresPermissions("business_package_query")
    @GetMapping(value = "/{id}")
    public AjaxResult getFileExchangePackageLogById(@PathVariable("id") Long id) {
        return success(fileExchangePackageLogService.selectFileExchangePackageLogById(id));
    }

    /**
     * 新增对外文件打包日志
     */
    @RequiresPermissions("business_package_add")
    @Log(title = "对外文件打包日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addFileExchangePackageLog(@RequestBody FileExchangePackageLog fileExchangePackageLog) {
        fileExchangePackageLog.setDeptIdPath(SecurityUtils.getDeptIdPath());
        fileExchangePackageLog.setDeptName(SecurityUtils.getDeptName());
        fileExchangePackageLog.setCreateUserId(SecurityUtils.getUserId());
        fileExchangePackageLog.setCreateBy(SecurityUtils.getUsername());
        return toAjax(fileExchangePackageLogService.insertFileExchangePackageLog(fileExchangePackageLog));
    }

    /**
     * 批量删除对外文件打包日志
     */
    @RequiresPermissions("business_package_delete")
    @Log(title = "对外文件打包日志", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteByIds/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return toAjax(fileExchangePackageLogService.deleteFileExchangePackageLogByIds(ids));
    }

    /**
     * 删除对外文件打包日志
     */
    @RequiresPermissions("business_package_delete")
    @Log(title = "对外文件打包日志", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteById/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(fileExchangePackageLogService.deleteFileExchangePackageLogById(id));
    }

    /**
     * 导出对外文件打包日志列表
     */
    @RequiresPermissions("business_package_export")
    @Log(title = "对外文件打包日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FileExchangePackageLog fileExchangePackageLog) {
        List<FileExchangePackageLog> list = fileExchangePackageLogService.selectFileExchangePackageLogList(fileExchangePackageLog);
        ExcelUtil<FileExchangePackageLog> util = new ExcelUtil<>(FileExchangePackageLog.class);
        util.exportExcel(response, list, "对外文件打包日志数据");
    }

    /**
     * 删除对外文件打包日志及文件
     */
    @RequiresPermissions("business_package_query")
    @DeleteMapping(value = "deleteFileById/{id}")
    public AjaxResult deleteLogFileById(@PathVariable("id") Long id) {
        FileExchangePackageLog fileExchangePackageLog = fileExchangePackageLogService.selectFileExchangePackageLogById(id);
        FileUtils.deleteFile(fileExchangePackageLog.getFilePosition() + File.separator + fileExchangePackageLog.getPackageFileName());
        fileExchangePackageLogService.deleteFileExchangePackageLogById(id);
        return success();
    }
}
