package org.springblade.file.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.file.pojo.dto.BusinessUnzipTaskQueryDTO;
import org.springblade.file.pojo.dto.BusinessUnzipTaskUpdateDTO;
import org.springblade.file.pojo.enums.UnzipTask;
import org.springblade.file.pojo.vo.BusinessUnzipTaskListVO;
import org.springblade.file.service.IBusinessUnzipTaskService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author bfhuange
 * @date 2024/10/28
 */
@RestController
@AllArgsConstructor
@RequestMapping("/businessUnzipTask")
@Tag(name = "业务文件解压任务", description = "业务文件解压任务")
public class BusinessUnzipTaskController {

    private final IBusinessUnzipTaskService unzipTaskService;

    /**
     * 文件任务列表
     * @param param
     * @param query
     */
    @GetMapping("/list")
    @Operation(summary = "业务文件解压任务分页查询", description = "业务文件解压任务分页查询")
    @PreAuth("hasPermission('waybill:uploadTask:list')")
    public R<IPage<BusinessUnzipTaskListVO>> list(BusinessUnzipTaskQueryDTO param, Query query) {
        if (param == null) {
            param = new BusinessUnzipTaskQueryDTO();
        }
        // 只查文件任务类型
        param.setRelateType(UnzipTask.RelateType.FILE_TASK.getCode());
        if (param.isCurrentUser()) {
            // 只查自己的
            param.setCurrentUserId(AuthUtil.getUserId());
        }
        IPage<BusinessUnzipTaskListVO> pages = unzipTaskService.selectPage(Condition.getPage(query), param);
        return R.data(pages);
    }

    /**
     *  业务文件解压任务更新（重新上传）
     * @param param
     * @return
     */
    @ApiLog("上传任务-重新上传")
    @PostMapping("/update")
    @Operation(summary = "业务文件解压任务更新（重新上传）", description = "业务文件解压任务更新（重新上传）")
    @PreAuth("hasPermission('waybill:uploadTask:update')")
    public R update(@Validated @RequestBody BusinessUnzipTaskUpdateDTO param) {
        Func.requireNotNull(param.getId(), "id不能为空");
        return R.status(unzipTaskService.updateReupload(param));
    }
}
