package com.tourism.webadmin.back.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import cn.hutool.core.util.ReflectUtil;
import com.tourism.common.core.upload.BaseUpDownloader;
import com.tourism.common.core.upload.UpDownloaderFactory;
import com.tourism.common.core.upload.UploadResponseInfo;
import com.tourism.common.core.upload.UploadStoreInfo;
import com.tourism.common.log.annotation.OperationLog;
import com.tourism.common.log.model.constant.SysOperationLogType;
import com.github.pagehelper.page.PageMethod;
import com.tourism.webadmin.back.vo.*;
import com.tourism.webadmin.back.dto.*;
import com.tourism.webadmin.back.model.*;
import com.tourism.webadmin.back.service.*;
import com.tourism.common.core.object.*;
import com.tourism.common.core.util.*;
import com.tourism.common.core.constant.*;
import com.tourism.common.core.annotation.MyRequestBody;
import com.tourism.common.redis.cache.SessionCacheHelper;
import com.tourism.common.additional.config.ApplicationConfig;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 旅游项目游记管理操作控制器类。
 *
 * @author 吃饭睡觉
 * @date 2024-09-06
 */
@Tag(name = "旅游项目游记管理管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/app/tourTourismProjectTravelNotes")
public class TourTourismProjectTravelNotesController {

    @Autowired
    private TourismProjectService tourismProjectService;
    @Autowired
    private ApplicationConfig appConfig;
    @Autowired
    private SessionCacheHelper cacheHelper;
    @Autowired
    private UpDownloaderFactory upDownloaderFactory;
    @Autowired
    private TourTourismProjectTravelNotesService tourTourismProjectTravelNotesService;
    @Autowired
    private TourTravelNotesProjectRelationService tourTravelNotesProjectRelationService;

    /**
     * 新增旅游项目游记管理数据，及其关联的从表数据。
     *
     * @param tourTourismProjectTravelNotesDto 新增主表对象。
     * @param tourTourismTravelNotesFileDto 一对一旅游项目游记文件从表Dto。
     * @param tourTourismTravelNotesContentDto 一对一旅游项目游记富文本从表Dto。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"tourTourismProjectTravelNotesDto.id", "tourTourismProjectTravelNotesDto.searchString"})
    @SaCheckPermission("tourTourismProjectTravelNotes.add")
    @OperationLog(type = SysOperationLogType.ADD)
    @PostMapping("/add")
    public ResponseResult<String> add(
            @MyRequestBody TourTourismProjectTravelNotesDto tourTourismProjectTravelNotesDto,
            @MyRequestBody TourTourismTravelNotesFileDto tourTourismTravelNotesFileDto,
            @MyRequestBody TourTourismTravelNotesContentDto tourTourismTravelNotesContentDto) {
        ResponseResult<Tuple2<TourTourismProjectTravelNotes, JSONObject>> verifyResult = this.doBusinessDataVerifyAndConvert(
                tourTourismProjectTravelNotesDto, false, tourTourismTravelNotesFileDto, tourTourismTravelNotesContentDto);
        if (!verifyResult.isSuccess()) {
            return ResponseResult.errorFrom(verifyResult);
        }
        Tuple2<TourTourismProjectTravelNotes, JSONObject> bizData = verifyResult.getData();
        TourTourismProjectTravelNotes tourTourismProjectTravelNotes = bizData.getFirst();
        tourTourismProjectTravelNotes = tourTourismProjectTravelNotesService.saveNewWithRelation(tourTourismProjectTravelNotes, bizData.getSecond());
        return ResponseResult.success(tourTourismProjectTravelNotes.getId());
    }

    /**
     * 修改旅游项目游记管理数据，及其关联的从表数据。
     *
     * @param tourTourismProjectTravelNotesDto 修改后的对象。
     * @param tourTourismTravelNotesFileDto 一对一旅游项目游记文件从表Dto。
     * @param tourTourismTravelNotesContentDto 一对一旅游项目游记富文本从表Dto。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"tourTourismProjectTravelNotesDto.id", "tourTourismProjectTravelNotesDto.searchString"})
    @SaCheckPermission("tourTourismProjectTravelNotes.update")
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/update")
    public ResponseResult<String> update(
            @MyRequestBody TourTourismProjectTravelNotesDto tourTourismProjectTravelNotesDto,
            @MyRequestBody TourTourismTravelNotesFileDto tourTourismTravelNotesFileDto,
            @MyRequestBody TourTourismTravelNotesContentDto tourTourismTravelNotesContentDto) {
        String errorMessage;
        ResponseResult<Tuple2<TourTourismProjectTravelNotes, JSONObject>> verifyResult = this.doBusinessDataVerifyAndConvert(
                tourTourismProjectTravelNotesDto, true, tourTourismTravelNotesFileDto, tourTourismTravelNotesContentDto);
        if (!verifyResult.isSuccess()) {
            return ResponseResult.errorFrom(verifyResult);
        }
        Tuple2<TourTourismProjectTravelNotes, JSONObject> bizData = verifyResult.getData();
        TourTourismProjectTravelNotes originalTourTourismProjectTravelNotes = bizData.getSecond().getObject("originalData", TourTourismProjectTravelNotes.class);
        TourTourismProjectTravelNotes tourTourismProjectTravelNotes = bizData.getFirst();
        if (!tourTourismProjectTravelNotesService.updateWithRelation(tourTourismProjectTravelNotes, originalTourTourismProjectTravelNotes, bizData.getSecond())) {
            errorMessage = "数据验证失败，[TourTourismProjectTravelNotes] 数据不存在!";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        return ResponseResult.success(tourTourismProjectTravelNotes.getId());
    }

    /**
     * 删除旅游项目游记管理数据。
     *
     * @param id 删除对象主键Id。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.delete")
    @OperationLog(type = SysOperationLogType.DELETE)
    @PostMapping("/delete")
    public ResponseResult<Void> delete(@MyRequestBody String id) {
        if (MyCommonUtil.existBlankArgument(id)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        return this.doDelete(id);
    }

    /**
     * 批量删除旅游项目游记管理数据。
     *
     * @param idList 待删除对象的主键Id列表。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.delete")
    @OperationLog(type = SysOperationLogType.DELETE_BATCH)
    @PostMapping("/deleteBatch")
    public ResponseResult<Void> deleteBatch(@MyRequestBody List<String> idList) {
        if (MyCommonUtil.existBlankArgument(idList)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        for (String id : idList) {
            ResponseResult<Void> responseResult = this.doDelete(id);
            if (!responseResult.isSuccess()) {
                return responseResult;
            }
        }
        return ResponseResult.success();
    }

    /**
     * 列出符合过滤条件的旅游项目游记管理列表。
     *
     * @param tourTourismProjectTravelNotesDtoFilter 过滤对象。
     * @param orderParam 排序参数。
     * @param pageParam 分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.view")
    @PostMapping("/list")
    public ResponseResult<MyPageData<TourTourismProjectTravelNotesVo>> list(
            @MyRequestBody TourTourismProjectTravelNotesDto tourTourismProjectTravelNotesDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getCount());
        }
        TourTourismProjectTravelNotes tourTourismProjectTravelNotesFilter = MyModelUtil.copyTo(tourTourismProjectTravelNotesDtoFilter, TourTourismProjectTravelNotes.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, TourTourismProjectTravelNotes.class);
        List<TourTourismProjectTravelNotes> tourTourismProjectTravelNotesList =
                tourTourismProjectTravelNotesService.getTourTourismProjectTravelNotesListWithRelation(tourTourismProjectTravelNotesFilter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(tourTourismProjectTravelNotesList, TourTourismProjectTravelNotesVo.class));
    }

    /**
     * 导出符合过滤条件的旅游项目游记管理列表。
     *
     * @param tourTourismProjectTravelNotesDtoFilter 过滤对象。
     * @param orderParam 排序参数。
     * @throws IOException 文件读写失败。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.export")
    @OperationLog(type = SysOperationLogType.EXPORT, saveResponse = false)
    @PostMapping("/export")
    public void export(
            @MyRequestBody TourTourismProjectTravelNotesDto tourTourismProjectTravelNotesDtoFilter,
            @MyRequestBody MyOrderParam orderParam) throws IOException {
        TourTourismProjectTravelNotes tourTourismProjectTravelNotesFilter = MyModelUtil.copyTo(tourTourismProjectTravelNotesDtoFilter, TourTourismProjectTravelNotes.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, TourTourismProjectTravelNotes.class);
        List<TourTourismProjectTravelNotes> resultList =
                tourTourismProjectTravelNotesService.getTourTourismProjectTravelNotesListWithRelation(tourTourismProjectTravelNotesFilter, orderBy);
        // 导出文件的标题数组
        // NOTE: 下面的代码中仅仅导出了主表数据，主表聚合计算数据和主表关联字典的数据。
        // 一对一从表数据的导出，可根据需要自行添加。如：headerMap.put("slaveFieldName.xxxField", "标题名称")
        Map<String, String> headerMap = new LinkedHashMap<>(34);
        headerMap.put("id", "主键id");
        headerMap.put("projectTitle", "项目标题");
        headerMap.put("remarks", "项目简述");
        headerMap.put("isHotspotDictMap.name", "是否热点，0非热点，1热点");
        headerMap.put("projectLabel", "标签");
        headerMap.put("showOrder", "显示顺序");
        headerMap.put("enableDictMap.name", "是否启用，0禁用，1启用");
        headerMap.put("belongTabDictMap.name", "所属分类");
        headerMap.put("startPlace", "出发地");
        headerMap.put("endPlace", "目的地");
        headerMap.put("countTimes", "总天数");
        headerMap.put("price", "项目价格");
        headerMap.put("tourismUrl", "项目展示图片");
        headerMap.put("createUserId", "创建用户");
        headerMap.put("createTime", "创建时间");
        headerMap.put("updateUserId", "更新用户");
        headerMap.put("updateTime", "更新时间");
        headerMap.put("dataState", "删除标记(1: 正常 -1: 已删除)");
        headerMap.put("priceUnit", "价格单位");
        headerMap.put("serviceEnsure", "服务保障");
        headerMap.put("contactNumber", "联系电话");
        headerMap.put("sellingPoint", "产品卖点");
        headerMap.put("shortTitle", "短标题");
        headerMap.put("shortDescription", "短描述");
        headerMap.put("homeHotPicture", "首页热门图片");
        headerMap.put("contactDescription", "联系人描述");
        headerMap.put("travelNotesBanner", "游记banner");
        headerMap.put("likeCount", "点赞量");
        headerMap.put("pageViewCount", "浏览量");
        headerMap.put("role", "人物");
        headerMap.put("departureTime", "出发时间");
        headerMap.put("averageCost", "人均费用");
        headerMap.put("recommendationRate", "推荐指数");
//        headerMap.put("projectIds", "旅游项目(以逗号分割，最多为三个项目)");
        ExportUtil.doExport(resultList, headerMap, "tourTourismProjectTravelNotes.xlsx");
    }

    /**
     * 查看指定旅游项目游记管理对象详情。
     *
     * @param id 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.view")
    @GetMapping("/view")
    public ResponseResult<TourTourismProjectTravelNotesVo> view(@RequestParam String id) {
        TourTourismProjectTravelNotes tourTourismProjectTravelNotes = tourTourismProjectTravelNotesService.getByIdWithRelation(id, MyRelationParam.full());
        if (tourTourismProjectTravelNotes == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        TourTourismProjectTravelNotesVo tourTourismProjectTravelNotesVo = MyModelUtil.copyTo(tourTourismProjectTravelNotes, TourTourismProjectTravelNotesVo.class);
        return ResponseResult.success(tourTourismProjectTravelNotesVo);
    }

    /**
     * 列出不与指定旅游项目游记管理存在多对多关系的 [旅游项目管理] 列表数据。通常用于查看添加新 [旅游项目管理] 对象的候选列表。
     *
     * @param id 主表关联字段。
     * @param tourismProjectDtoFilter [旅游项目管理] 过滤对象。
     * @param orderParam 排序参数。
     * @param pageParam 分页参数。
     * @return 应答结果对象，返回符合条件的数据列表。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.update")
    @PostMapping("/listNotInTourTravelNotesProjectRelation")
    public ResponseResult<MyPageData<TourismProjectVo>> listNotInTourTravelNotesProjectRelation(
            @MyRequestBody String id,
            @MyRequestBody TourismProjectDto tourismProjectDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (MyCommonUtil.isNotBlankOrNull(id) && !tourTourismProjectTravelNotesService.existId(id)) {
            return ResponseResult.error(ErrorCodeEnum.INVALID_RELATED_RECORD_ID);
        }
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getCount());
        }
        TourismProject filter = MyModelUtil.copyTo(tourismProjectDtoFilter, TourismProject.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, TourismProject.class);
        List<TourismProject> tourismProjectList =
                tourismProjectService.getNotInTourismProjectListByTravelNotesId(id, filter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(tourismProjectList, TourismProjectVo.class));
    }

    /**
     * 列出与指定旅游项目游记管理存在多对多关系的 [旅游项目管理] 列表数据。
     *
     * @param id 主表关联字段。
     * @param tourismProjectDtoFilter [旅游项目管理] 过滤对象。
     * @param orderParam 排序参数。
     * @param pageParam 分页参数。
     * @return 应答结果对象，返回符合条件的数据列表。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.view")
    @PostMapping("/listTourTravelNotesProjectRelation")
    public ResponseResult<MyPageData<TourismProjectVo>> listTourTravelNotesProjectRelation(
            @MyRequestBody(required = true) String id,
            @MyRequestBody TourismProjectDto tourismProjectDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (!tourTourismProjectTravelNotesService.existId(id)) {
            return ResponseResult.error(ErrorCodeEnum.INVALID_RELATED_RECORD_ID);
        }
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getCount());
        }
        TourismProject filter = MyModelUtil.copyTo(tourismProjectDtoFilter, TourismProject.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, TourismProject.class);
        List<TourismProject> tourismProjectList =
                tourismProjectService.getTourismProjectListByTravelNotesId(id, filter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(tourismProjectList, TourismProjectVo.class));
    }

    /**
     * 批量添加旅游项目游记管理和 [旅游项目管理] 对象的多对多关联关系数据。
     *
     * @param travelNotesId 主表主键Id。
     * @param tourTravelNotesProjectRelationDtoList 关联对象列表。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.update")
    @OperationLog(type = SysOperationLogType.ADD_M2M)
    @PostMapping("/addTourTravelNotesProjectRelation")
    public ResponseResult<Void> addTourTravelNotesProjectRelation(
            @MyRequestBody String travelNotesId,
            @MyRequestBody List<TourTravelNotesProjectRelationDto> tourTravelNotesProjectRelationDtoList) {

        //先验证绑定的项目是否超过三个
        TourTravelNotesProjectRelation tourTravelNotesProjectRelation =
                new TourTravelNotesProjectRelation();
        tourTravelNotesProjectRelation.setTravelNotesId(travelNotesId);
        long countByFilter = tourTravelNotesProjectRelationService.getCountByFilter(tourTravelNotesProjectRelation);
        if(countByFilter>=3){
            return ResponseResult.error(ErrorCodeEnum.DUPLICATED_UNIQUE_KEY,"游记最多绑定三个项目,请先移除绑定项目");
        }
        if (MyCommonUtil.existBlankArgument(travelNotesId, tourTravelNotesProjectRelationDtoList)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        Set<Long> projectIdSet =
                tourTravelNotesProjectRelationDtoList.stream().map(TourTravelNotesProjectRelationDto::getProjectId).collect(Collectors.toSet());
        if (!tourTourismProjectTravelNotesService.existId(travelNotesId)
                || !tourismProjectService.existUniqueKeyList("id", projectIdSet)) {
            return ResponseResult.error(ErrorCodeEnum.INVALID_RELATED_RECORD_ID);
        }
        List<TourTravelNotesProjectRelation> tourTravelNotesProjectRelationList =
                MyModelUtil.copyCollectionTo(tourTravelNotesProjectRelationDtoList, TourTravelNotesProjectRelation.class);
        tourTourismProjectTravelNotesService.addTourTravelNotesProjectRelationList(tourTravelNotesProjectRelationList, travelNotesId);
        return ResponseResult.success();
    }

    /**
     * 更新指定旅游项目游记管理和指定 [旅游项目管理] 的多对多关联数据。
     *
     * @param tourTravelNotesProjectRelationDto 对多对中间表对象。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.update")
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/updateTourTravelNotesProjectRelation")
    public ResponseResult<Void> updateTourTravelNotesProjectRelation(
            @MyRequestBody TourTravelNotesProjectRelationDto tourTravelNotesProjectRelationDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourTravelNotesProjectRelationDto);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        TourTravelNotesProjectRelation tourTravelNotesProjectRelation = MyModelUtil.copyTo(tourTravelNotesProjectRelationDto, TourTravelNotesProjectRelation.class);
        if (!tourTourismProjectTravelNotesService.updateTourTravelNotesProjectRelation(tourTravelNotesProjectRelation)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }

    /**
     * 显示旅游项目游记管理和指定 [旅游项目管理] 的多对多关联详情数据。
     *
     * @param travelNotesId 主表主键Id。
     * @param projectId 从表主键Id。
     * @return 应答结果对象，包括中间表详情。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.update")
    @GetMapping("/viewTourTravelNotesProjectRelation")
    public ResponseResult<TourTravelNotesProjectRelationVo> viewTourTravelNotesProjectRelation(
            @RequestParam String travelNotesId, @RequestParam String projectId) {
        TourTravelNotesProjectRelation tourTravelNotesProjectRelation = tourTourismProjectTravelNotesService.getTourTravelNotesProjectRelation(travelNotesId, projectId);
        if (tourTravelNotesProjectRelation == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        TourTravelNotesProjectRelationVo tourTravelNotesProjectRelationVo = MyModelUtil.copyTo(tourTravelNotesProjectRelation, TourTravelNotesProjectRelationVo.class);
        return ResponseResult.success(tourTravelNotesProjectRelationVo);
    }

    /**
     * 移除指定旅游项目游记管理和指定 [旅游项目管理] 的多对多关联关系。
     *
     * @param travelNotesId 主表主键Id。
     * @param projectId 从表主键Id。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.update")
    @OperationLog(type = SysOperationLogType.DELETE_M2M)
    @PostMapping("/deleteTourTravelNotesProjectRelation")
    public ResponseResult<Void> deleteTourTravelNotesProjectRelation(
            @MyRequestBody String travelNotesId, @MyRequestBody String projectId) {
        if (MyCommonUtil.existBlankArgument(travelNotesId, projectId)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        if (!tourTourismProjectTravelNotesService.removeTourTravelNotesProjectRelation(travelNotesId, projectId)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }

    /**
     * 批量移除指定旅游项目游记管理和指定 [旅游项目管理] 的多对多关联关系。
     *
     * @param travelNotesId 主表主键Id。
     * @param projectIdList 从表主键Id列表。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.update")
    @OperationLog(type = SysOperationLogType.DELETE_M2M_BATCH)
    @PostMapping("/deleteTourTravelNotesProjectRelationList")
    public ResponseResult<Void> deleteTourTravelNotesProjectRelationList(
            @MyRequestBody String travelNotesId, @MyRequestBody List<String> projectIdList) {
        if (MyCommonUtil.existBlankArgument(travelNotesId, projectIdList)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        tourTourismProjectTravelNotesService.removeTourTravelNotesProjectRelationList(travelNotesId, projectIdList);
        return ResponseResult.success();
    }

    /**
     * 附件文件下载。
     * 这里将图片和其他类型的附件文件放到不同的父目录下，主要为了便于今后图片文件的迁移。
     *
     * @param id 附件所在记录的主键Id。
     * @param fieldName 附件所属的字段名。
     * @param filename  文件名。如果没有提供该参数，就从当前记录的指定字段中读取。
     * @param asImage   下载文件是否为图片。
     * @param response  Http 应答对象。
     */
//    @SaCheckPermission("tourTourismProjectTravelNotes.view")
    @SaIgnore
    @OperationLog(type = SysOperationLogType.DOWNLOAD, saveResponse = false)
    @GetMapping("/download")
    public void download(
            @RequestParam(required = false) Long id,
            @RequestParam String fieldName,
            @RequestParam String filename,
            @RequestParam Boolean asImage,
            HttpServletResponse response) {
        if (MyCommonUtil.existBlankArgument(fieldName, filename, asImage)) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        // 使用try来捕获异常，是为了保证一旦出现异常可以返回500的错误状态，便于调试。
        // 否则有可能给前端返回的是200的错误码。
        try {
            // 如果请求参数中没有包含主键Id，就判断该文件是否为当前session上传的。
            if (id == null) {
                if (!cacheHelper.existSessionUploadFile(filename)) {
                    ResponseResult.output(HttpServletResponse.SC_FORBIDDEN);
                    return;
                }
            } else {
                TourTourismProjectTravelNotes tourTourismProjectTravelNotes = tourTourismProjectTravelNotesService.getById(id);
                if (tourTourismProjectTravelNotes == null) {
                    ResponseResult.output(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
                String fieldJsonData = (String) ReflectUtil.getFieldValue(tourTourismProjectTravelNotes, fieldName);
                if (fieldJsonData == null && !cacheHelper.existSessionUploadFile(filename)) {
                    ResponseResult.output(HttpServletResponse.SC_BAD_REQUEST);
                    return;
                }
                if (!BaseUpDownloader.containFile(fieldJsonData, filename)
                        && !cacheHelper.existSessionUploadFile(filename)) {
                    ResponseResult.output(HttpServletResponse.SC_FORBIDDEN);
                    return;
                }
            }
            UploadStoreInfo storeInfo = MyModelUtil.getUploadStoreInfo(TourTourismProjectTravelNotes.class, fieldName);
            if (!storeInfo.isSupportUpload()) {
                ResponseResult.output(HttpServletResponse.SC_NOT_IMPLEMENTED,
                        ResponseResult.error(ErrorCodeEnum.INVALID_UPLOAD_FIELD));
                return;
            }
            BaseUpDownloader upDownloader = upDownloaderFactory.get(storeInfo.getStoreType());
            upDownloader.doDownload(appConfig.getUploadFileBaseDir(),
                    TourTourismProjectTravelNotes.class.getSimpleName(), fieldName, filename, asImage, response);
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 文件上传操作。
     *
     * @param fieldName  上传文件名。
     * @param asImage    是否作为图片上传。如果是图片，今后下载的时候无需权限验证。否则就是附件上传，下载时需要权限验证。
     * @param uploadFile 上传文件对象。
     */
    @SaCheckPermission("tourTourismProjectTravelNotes.view")
    @OperationLog(type = SysOperationLogType.UPLOAD, saveResponse = false)
    @PostMapping("/upload")
    public void upload(
            @RequestParam String fieldName,
            @RequestParam Boolean asImage,
            @RequestParam("uploadFile") MultipartFile uploadFile) throws IOException {
        UploadStoreInfo storeInfo = MyModelUtil.getUploadStoreInfo(TourTourismProjectTravelNotes.class, fieldName);
        // 这里就会判断参数中指定的字段，是否支持上传操作。
        if (!storeInfo.isSupportUpload()) {
            ResponseResult.output(HttpServletResponse.SC_FORBIDDEN,
                    ResponseResult.error(ErrorCodeEnum.INVALID_UPLOAD_FIELD));
            return;
        }
        // 根据字段注解中的存储类型，通过工厂方法获取匹配的上传下载实现类，从而解耦。
        BaseUpDownloader upDownloader = upDownloaderFactory.get(storeInfo.getStoreType());
        UploadResponseInfo responseInfo = upDownloader.doUpload(null,
                appConfig.getUploadFileBaseDir(), TourTourismProjectTravelNotes.class.getSimpleName(), fieldName, asImage, uploadFile);
        if (Boolean.TRUE.equals(responseInfo.getUploadFailed())) {
            ResponseResult.output(HttpServletResponse.SC_FORBIDDEN,
                    ResponseResult.error(ErrorCodeEnum.UPLOAD_FAILED, responseInfo.getErrorMessage()));
            return;
        }
        cacheHelper.putSessionUploadFile(responseInfo.getFilename());
        ResponseResult.output(ResponseResult.success(responseInfo));
    }

    private ResponseResult<Tuple2<TourTourismProjectTravelNotes, JSONObject>> doBusinessDataVerifyAndConvert(
            TourTourismProjectTravelNotesDto tourTourismProjectTravelNotesDto,
            boolean forUpdate,
            TourTourismTravelNotesFileDto tourTourismTravelNotesFileDto,
            TourTourismTravelNotesContentDto tourTourismTravelNotesContentDto) {
        ErrorCodeEnum errorCode = ErrorCodeEnum.DATA_VALIDATED_FAILED;
        String errorMessage = MyCommonUtil.getModelValidationError(tourTourismProjectTravelNotesDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(errorCode, errorMessage);
        }
        errorMessage = MyCommonUtil.getModelValidationError(tourTourismTravelNotesFileDto);
        if (errorMessage != null) {
            return ResponseResult.error(errorCode, "参数 [tourTourismTravelNotesFileDto] " + errorMessage);
        }
        errorMessage = MyCommonUtil.getModelValidationError(tourTourismTravelNotesContentDto);
        if (errorMessage != null) {
            return ResponseResult.error(errorCode, "参数 [tourTourismTravelNotesContentDto] " + errorMessage);
        }
        // 全部关联从表数据的验证和转换
        JSONObject relationData = new JSONObject();
        CallResult verifyResult;
        // 下面是输入参数中，主表关联数据的验证。
        TourTourismProjectTravelNotes tourTourismProjectTravelNotes = MyModelUtil.copyTo(tourTourismProjectTravelNotesDto, TourTourismProjectTravelNotes.class);
        TourTourismProjectTravelNotes originalData = null;
        if (forUpdate && tourTourismProjectTravelNotes != null) {
            originalData = tourTourismProjectTravelNotesService.getById(tourTourismProjectTravelNotes.getId());
            if (originalData == null) {
                return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
            }
            relationData.put("originalData", originalData);
        }
        verifyResult = tourTourismProjectTravelNotesService.verifyRelatedData(tourTourismProjectTravelNotes, originalData);
        if (!verifyResult.isSuccess()) {
            return ResponseResult.errorFrom(verifyResult);
        }
        // 处理主表的一对一关联 [TourTourismTravelNotesFile]
        TourTourismTravelNotesFile tourTourismTravelNotesFile = MyModelUtil.copyTo(tourTourismTravelNotesFileDto, TourTourismTravelNotesFile.class);
        relationData.put("tourTourismTravelNotesFile", tourTourismTravelNotesFile);
        // 处理主表的一对一关联 [TourTourismTravelNotesContent]
        TourTourismTravelNotesContent tourTourismTravelNotesContent = MyModelUtil.copyTo(tourTourismTravelNotesContentDto, TourTourismTravelNotesContent.class);
        relationData.put("tourTourismTravelNotesContent", tourTourismTravelNotesContent);
        return ResponseResult.success(new Tuple2<>(tourTourismProjectTravelNotes, relationData));
    }

    private ResponseResult<Void> doDelete(String id) {
        String errorMessage;
        // 验证关联Id的数据合法性
        TourTourismProjectTravelNotes originalTourTourismProjectTravelNotes = tourTourismProjectTravelNotesService.getById(id);
        if (originalTourTourismProjectTravelNotes == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [对象] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!tourTourismProjectTravelNotesService.remove(id)) {
            errorMessage = "数据操作失败，删除的对象不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        return ResponseResult.success();
    }
}
