package com.ruoyi.web;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.domain.Task;
import com.ruoyi.service.PdfTemplateManagementService;
import com.ruoyi.service.TaskService;
import com.ruoyi.web.request.PdfFileRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author gxk
 */
@Api(tags = "PDF模板管理")
@RestController
@RequestMapping("/pdf")
public class PdfTemplateManagementController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private PdfTemplateManagementService pdfTemplateManagementService;

    /**
     * 检验报告PDF下载
     * @param request
     */
    @Anonymous
    @ApiOperation("检验报告PDF下载")
    @PostMapping("/inspectionReport")
    public R<String> download(@Validated @RequestBody PdfFileRequest request) {
        try {
            // demo-模板
            String url = pdfTemplateManagementService.generateInspectionReport(request.getTaskId());
            return R.ok(url);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 企业留档文件PDF下载
     * @param request
     */
    @Anonymous
    @ApiOperation("企业留档文件PDF下载")
    @PostMapping("/retentionFile")
    public R<String> downloadRetentionFile(@Validated @RequestBody PdfFileRequest request) {
        try {
            // demo-模板
            String url = pdfTemplateManagementService.generateRetentionFile(request.getTaskId());
            return R.ok(url);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 下载PDF通用(三个PDF)
     * @param request
     * @return
     */
    @Anonymous
    @ApiOperation("下载PDF通用")
    @PostMapping("/download")
    public R<String> downloadPdf(@Validated @RequestBody PdfFileRequest request) {
        // 根据taskId、pdf类型判断库里存不存在minio地址
        String url = "";
        Task task = taskService.getOne(new QueryWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()));
        try {
            switch (request.getType()) {
                case 0:
                    url = task.getVehicleInformationUrl();
                    if (StrUtil.isBlank(url)) {
                        // 生成整车信息安全检验报告
                        url = pdfTemplateManagementService.generateInspectionReport(request.getTaskId());
                        taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getVehicleInformationUrl, url));
                    }
                    break;
                case 1:
                    url = task.getRetentionUrl();
                    if (StrUtil.isBlank(url)) {
                        // 根据任务ID新生成
                        url = pdfTemplateManagementService.generateRetentionFile(request.getTaskId());
                        // 更改
                        taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getRetentionUrl, url));
                    }
                    break;
                case 2:
                    url = task.getModelTestUrl();
                    if (StrUtil.isBlank(url)) {
                        // TODO 生成车型
                        url = pdfTemplateManagementService.generateOriginalRecord(request.getTaskId());
                        taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getModelTestUrl, url));
                    }
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok(url);
    }

    /**
     * 更新PDF通用
     * @param request
     */
    @Anonymous
    @ApiOperation("更新PDF通用")
    @PostMapping("/update")
    public R<String> updatePdf(@Validated @RequestBody PdfFileRequest request) {
        String url = "";
        try {
            switch (request.getType()) {
                case 0:
                    // 更新整车PDF文件
                    url = pdfTemplateManagementService.generateInspectionReport(request.getTaskId());
                    taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getVehicleInformationUrl, url));
                    break;
                case 1:
                    // 根据任务ID新生成
                    url = pdfTemplateManagementService.generateRetentionFile(request.getTaskId());
                    // 更改
                    taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getRetentionUrl, url));
                    break;
                case 2:
                    // 更新
                    url = pdfTemplateManagementService.generateOriginalRecord(request.getTaskId());
                    taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getModelTestUrl, url));
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok(url);
    }
}