package com.hotwj.platform.noticeManagerment.signFileDetail.controller;

import com.hotwj.platform.noticeManagerment.signFileDetail.domain.bo.HotSignFileCompleteBo;
import com.hotwj.platform.noticeManagerment.signFileDetail.domain.bo.HotSignFileDetailBo;
import com.hotwj.platform.noticeManagerment.signFileDetail.domain.vo.HotSignFileDetailVo;
import com.hotwj.platform.noticeManagerment.signFileDetail.service.IHotSignFileDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 签订文件明细
 *
 * @author shihongwei
 * @date 2026-01-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/noticeManagerment/signFileDetail")
@Tag(name = "签订文件明细", description = "签订文件明细管理")
public class HotSignFileDetailController extends BaseController {

    private final IHotSignFileDetailService hotSignFileDetailService;

    /**
     * 查询签订文件明细列表
     */
    //@SaCheckPermission("noticeManagerment:signFileDetail:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询签订文件明细列表")
    public TableDataInfo<HotSignFileDetailVo> list(HotSignFileDetailBo bo, PageQuery pageQuery) {
        return hotSignFileDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出签订文件明细列表
     */
    //@SaCheckPermission("noticeManagerment:signFileDetail:export")
    @Log(title = "签订文件明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出签订文件明细列表")
    public void export(HotSignFileDetailBo bo, HttpServletResponse response) {
        List<HotSignFileDetailVo> list = hotSignFileDetailService.queryList(bo);
        if (list == null) {
            list = new ArrayList<>();
        }
        for (HotSignFileDetailVo v : list) {
            if (v == null) {
                continue;
            }
            v.setSignTimeStr(v.getSignTime() != null ? DateUtils.formatDate(v.getSignTime()) : null);
            v.setExpireTimeStr(v.getExpireTime() != null ? DateUtils.formatDate(v.getExpireTime()) : null);
            v.setCreateTimeStr(v.getCreateTime() != null ? DateUtils.formatDate(v.getCreateTime()) : null);
        }
        ExcelUtil.exportExcel(list, "签订文件明细", HotSignFileDetailVo.class, response);
    }

    /**
     * 获取签订文件明细详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("noticeManagerment:signFileDetail:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取签订文件明细详细信息")
    public R<HotSignFileDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                          @Parameter(name = "id", description = "主键", required = true, example = "1")
                                          @PathVariable Long id) {
        return R.ok(hotSignFileDetailService.queryById(id));
    }

    @Log(title = "签订文件详情打印", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPdf/{id}")
    @Operation(summary = "导出签订文件详情PDF")
    @Deprecated // 已经在前端实现打印了，这个可以考虑后续移除
    public void exportDetailPdf(@NotNull(message = "主键不能为空")
                                @Parameter(name = "id", description = "主键", required = true, example = "1")
                                @PathVariable Long id,
                                HttpServletResponse response) {
        hotSignFileDetailService.exportDetailPdf(id, response);
    }

    /**
     * 新增签订文件明细
     */
    //@SaCheckPermission("noticeManagerment:signFileDetail:add")
    @Log(title = "签订文件明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增签订文件明细")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HotSignFileDetailBo bo) {
        return toAjax(hotSignFileDetailService.insertByBo(bo));
    }

    /**
     * 修改签订文件明细
     */
    //@SaCheckPermission("noticeManagerment:signFileDetail:edit")
    @Log(title = "签订文件明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改签订文件明细")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotSignFileDetailBo bo) {
        return toAjax(hotSignFileDetailService.updateByBo(bo));
    }

    /**
     * 删除签订文件明细
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("noticeManagerment:signFileDetail:remove")
    @Log(title = "签订文件明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除签订文件明细")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @Parameter(name = "ids", description = "主键串", required = true, example = "1,2,3")
                          @PathVariable Long[] ids) {
        return toAjax(hotSignFileDetailService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 完成文件签署
     */
    //@SaCheckPermission("noticeManagerment:signFileDetail:edit")
    @Log(title = "完成文件签署", businessType = BusinessType.UPDATE)
    @PostMapping("/sign")
    @Operation(summary = "完成文件签署")
    public R<Boolean> completeSign(@Validated @RequestBody HotSignFileCompleteBo bo) {
        return R.ok(hotSignFileDetailService.completeSign(bo.getTaskId(), bo.getSignOssId()));
    }
}
