package com.srs.web.controller.dormitory;

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

import com.srs.routine.domain.RtEnlistmentReserveAttach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.dormitory.domain.DmsOutsideAccommodationAttachment;
import com.srs.dormitory.service.IDmsOutsideAccommodationAttachmentService;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;

/**
 * 外宿申请附件Controller
 *
 * @author srs
 * @date 2025-12-05
 */
@RestController
@RequestMapping("/dormitory/outsideAccommodationAttachment")
@Api(value = "外宿申请附件管理", tags = "外宿申请附件管理")
public class DmsOutsideAccommodationAttachmentController extends BaseController {
    @Autowired
    private IDmsOutsideAccommodationAttachmentService dmsOutsideAccommodationAttachmentService;

    /**
     * 查询外宿申请附件列表
     */
    @PreAuthorize("@ss.hasPermi('dormitory:outsideAccommodationAttachment:list')")
    @GetMapping("/list")
    @ApiOperation("查询外宿申请附件列表")
    public TableDataInfo list(DmsOutsideAccommodationAttachment dmsOutsideAccommodationAttachment) {
        startPage();
        List<DmsOutsideAccommodationAttachment> list = dmsOutsideAccommodationAttachmentService.selectDmsOutsideAccommodationAttachmentList(dmsOutsideAccommodationAttachment);
        return getDataTable(list);
    }

    /**
     * 导出外宿申请附件列表
     */
    @PreAuthorize("@ss.hasPermi('dormitory:outsideAccommodationAttachment:export')")
    @Log(title = "外宿申请附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出外宿申请附件列表")
    public void export(HttpServletResponse response, DmsOutsideAccommodationAttachment dmsOutsideAccommodationAttachment) {
        List<DmsOutsideAccommodationAttachment> list = dmsOutsideAccommodationAttachmentService.selectDmsOutsideAccommodationAttachmentList(dmsOutsideAccommodationAttachment);
        ExcelUtil<DmsOutsideAccommodationAttachment> util = new ExcelUtil<DmsOutsideAccommodationAttachment>(DmsOutsideAccommodationAttachment.class);
        util.exportExcel(response, list, "外宿申请附件数据");
    }

    /**
     * 获取外宿申请附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('dormitory:outsideAccommodationAttachment:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取外宿申请附件详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dmsOutsideAccommodationAttachmentService.selectDmsOutsideAccommodationAttachmentById(id));
    }

    /**
     * 新增外宿申请附件
     */
    @PreAuthorize("@ss.hasPermi('dormitory:outsideAccommodationAttachment:add')")
    @Log(title = "外宿申请附件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增外宿申请附件")
    public AjaxResult add(@RequestBody DmsOutsideAccommodationAttachment dmsOutsideAccommodationAttachment) {
        return toAjax(dmsOutsideAccommodationAttachmentService.insertDmsOutsideAccommodationAttachment(dmsOutsideAccommodationAttachment));
    }

    /**
     * 批量新增保留学籍申请附件（入伍通知书等）
     */
    @PreAuthorize("@ss.hasPermi('dormitory:outsideAccommodationAttachment:add')")
    @Log(title = "外宿申请附件", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd") // 注意：修改接口路径避免与原单条接口冲突
    @ApiOperation("批量新增外宿申请附件")
    public AjaxResult batchAdd(@RequestBody List<DmsOutsideAccommodationAttachment> list) {
        // 1. 校验参数合法性
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("批量插入失败：附件列表不能为空");
        }

        // 2. 调用Service层批量插入方法
        int rows = dmsOutsideAccommodationAttachmentService.batchInsertDmsOutsideAccommodationAttachment(list);

        // 3. 返回结果（rows为成功插入的条数）
        return rows > 0 ? AjaxResult.success("批量插入成功，共插入 " + rows + " 条记录")
                : AjaxResult.error("批量插入失败");
    }

    /**
     * 修改外宿申请附件
     */
    @PreAuthorize("@ss.hasPermi('dormitory:outsideAccommodationAttachment:edit')")
    @Log(title = "外宿申请附件", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改外宿申请附件")
    public AjaxResult edit(@RequestBody DmsOutsideAccommodationAttachment dmsOutsideAccommodationAttachment) {
        return toAjax(dmsOutsideAccommodationAttachmentService.updateDmsOutsideAccommodationAttachment(dmsOutsideAccommodationAttachment));
    }

    /**
     * 删除外宿申请附件
     */
    @PreAuthorize("@ss.hasPermi('dormitory:outsideAccommodationAttachment:remove')")
    @Log(title = "外宿申请附件", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除外宿申请附件")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dmsOutsideAccommodationAttachmentService.deleteDmsOutsideAccommodationAttachmentByIds(ids));
    }

    /**
     * 删除保留学籍申请附件（入伍通知书等）
     */
    @PreAuthorize("@ss.hasPermi('dormitory:outsideAccommodationAttachment:remove')")
    @Log(title = "外宿申请附件", businessType = BusinessType.DELETE)
    @PostMapping("/OutsideAccommodationAttachment")
    @ApiOperation("删除外宿申请附件")
    public AjaxResult deleteOutsideAccommodationAttachmentNameAndStuName(String attachmentName, String studentName) {
        return toAjax(dmsOutsideAccommodationAttachmentService.deleteOutsideAccommodationAttachmentNameAndStuName(attachmentName, studentName));
    }
}
