package com.tourism.webadmin.back.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ReflectUtil;
import com.tourism.common.additional.utils.StringUtils;
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.model.constant.AuditState;
import com.tourism.webadmin.back.model.constant.Enable;
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.apache.commons.collections4.CollectionUtils;
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/tourismProjectTravelNotesWrite")
public class TourismProjectTravelNotesWriteController {

    @Autowired
    private ApplicationConfig appConfig;
    @Autowired
    private SessionCacheHelper cacheHelper;
    @Autowired
    private UpDownloaderFactory upDownloaderFactory;
    @Autowired
    private TourismProjectTravelNotesWriteService tourismProjectTravelNotesWriteService;
    @Autowired
    private TourTravelNotesTopicService tourTravelNotesTopicService;
    @Autowired
    private TourTravelNotesImgService tourTravelNotesImgService;
    /**
     * 新增发布游记数据。
     *
     * @param tourismProjectTravelNotesWriteDto 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {
            "tourismProjectTravelNotesWriteDto.id",
            "tourismProjectTravelNotesWriteDto.searchString",
            "tourismProjectTravelNotesWriteDto.departureTimeStart",
            "tourismProjectTravelNotesWriteDto.departureTimeEnd",
            "tourismProjectTravelNotesWriteDto.createTimeStart",
            "tourismProjectTravelNotesWriteDto.createTimeEnd"})
    @SaCheckPermission("tourismProjectTravelNotesWrite.add")
    @OperationLog(type = SysOperationLogType.ADD)
    @PostMapping("/add")
    public ResponseResult<Long> add(@MyRequestBody TourismProjectTravelNotesWriteDto tourismProjectTravelNotesWriteDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourismProjectTravelNotesWriteDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        TourismProjectTravelNotesWrite tourismProjectTravelNotesWrite = MyModelUtil.copyTo(tourismProjectTravelNotesWriteDto, TourismProjectTravelNotesWrite.class);
        tourismProjectTravelNotesWrite = tourismProjectTravelNotesWriteService.saveNew(tourismProjectTravelNotesWrite);
        return ResponseResult.success(tourismProjectTravelNotesWrite.getId());
    }

    /**
     * 更新发布游记数据。
     *
     * @param tourismProjectTravelNotesWriteDto 更新对象。
     * @return 应答结果对象。
     */
    @ApiOperationSupport(ignoreParameters = {
            "tourismProjectTravelNotesWriteDto.searchString",
            "tourismProjectTravelNotesWriteDto.departureTimeStart",
            "tourismProjectTravelNotesWriteDto.departureTimeEnd",
            "tourismProjectTravelNotesWriteDto.createTimeStart",
            "tourismProjectTravelNotesWriteDto.createTimeEnd"})
    @SaCheckPermission("tourismProjectTravelNotesWrite.update")
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/update")
    public ResponseResult<Void> update(@MyRequestBody TourismProjectTravelNotesWriteDto tourismProjectTravelNotesWriteDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourismProjectTravelNotesWriteDto, true);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        TourismProjectTravelNotesWrite tourismProjectTravelNotesWrite = MyModelUtil.copyTo(tourismProjectTravelNotesWriteDto, TourismProjectTravelNotesWrite.class);
        TourismProjectTravelNotesWrite originalTourismProjectTravelNotesWrite = tourismProjectTravelNotesWriteService.getById(tourismProjectTravelNotesWrite.getId());
        if (originalTourismProjectTravelNotesWrite == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [数据] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!tourismProjectTravelNotesWriteService.update(tourismProjectTravelNotesWrite, originalTourismProjectTravelNotesWrite)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }


    @SaCheckPermission("tourismProjectTravelNotesWrite.update")
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/audit")
    public ResponseResult<Void> audit(@MyRequestBody TourismProjectTravelNotesWriteDto tourismProjectTravelNotesWriteDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourismProjectTravelNotesWriteDto, true);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        if(tourismProjectTravelNotesWriteDto.getState().equals(AuditState.DRAFT)){
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, "审核状态不能为草稿");
        }
        if(tourismProjectTravelNotesWriteDto.getState().equals(AuditState.UNAUDITSTATE)){
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, "审核状态不能为未审核");
        }
        else if(tourismProjectTravelNotesWriteDto.getState().equals(AuditState.FAILAUDIT) && StringUtils.isBlank(tourismProjectTravelNotesWriteDto.getReason())){
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, "审核未通过，审核原因不能为空");
        }else if(tourismProjectTravelNotesWriteDto.getState().equals(AuditState.SUCCEEDAUDIT)){
            if (tourismProjectTravelNotesWriteDto.getIsHotspot() == null){
                return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, "审核通过，是否热点不能为空");
            }else if(StringUtils.isBlank(tourismProjectTravelNotesWriteDto.getNoteLabel())){
                return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, "审核通过，标签不能为空");
            }else if(tourismProjectTravelNotesWriteDto.getLikeCount() == null){
                return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, "审核通过，点赞数不能为空");
            }else if(tourismProjectTravelNotesWriteDto.getPageViewCount() == null){
                return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, "审核通过，浏览量不能为空");
            }else if(tourismProjectTravelNotesWriteDto.getHotValue() == null){
                return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, "审核通过，热度值不能为空");
            }else if(tourismProjectTravelNotesWriteDto.getIsOriginal() == null){
                return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, "审核通过，是否原创不能为空");
            }

        }
        TourismProjectTravelNotesWrite tourismProjectTravelNotesWrite = MyModelUtil.copyTo(tourismProjectTravelNotesWriteDto, TourismProjectTravelNotesWrite.class);
        TourismProjectTravelNotesWrite originalTourismProjectTravelNotesWrite = tourismProjectTravelNotesWriteService.getById(tourismProjectTravelNotesWrite.getId());
        if (originalTourismProjectTravelNotesWrite == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [数据] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }

        //审核通过的话，则进行查询话题，查看是否话题是否存在
        //话题不存在的话，则进行新增话题
        if(tourismProjectTravelNotesWrite.getState() == 3) {
            if (StringUtils.isNotBlank(tourismProjectTravelNotesWrite.getTourTravelNotesTopicRelationName())) {
                String[] tourTravelNotesTopicRelationNameArray = tourismProjectTravelNotesWrite.getTourTravelNotesTopicRelationName().split(",");
                Set<String> tourTravelNotesTopicRelationNameSet = new HashSet<>(Arrays.asList(tourTravelNotesTopicRelationNameArray));

                //查询哪些是存在于数据库中的，反之获得哪些是不存在于数据库中
                List<TourTravelNotesTopic> tourTravelNotesTopicServiceInList = tourTravelNotesTopicService.getInList("name", tourTravelNotesTopicRelationNameSet);
                List<String> topicNameList = tourTravelNotesTopicServiceInList.stream().map(TourTravelNotesTopic::getName).toList();
                tourTravelNotesTopicRelationNameSet.removeAll(topicNameList);

                //对不存在数据库中的话题，进行新增话题
                List<TourTravelNotesTopic> tourTravelNotesTopicList = new ArrayList<>();
                tourTravelNotesTopicRelationNameSet.stream().forEach(item->{
                    TourTravelNotesTopic tourTravelNotesTopic = new TourTravelNotesTopic();
                    tourTravelNotesTopic.setName(item);
                    tourTravelNotesTopic.setEnable(Enable.ENABLE_YES);
                    tourTravelNotesTopicList.add(tourTravelNotesTopic);
                });
                tourTravelNotesTopicService.saveNewBatch(tourTravelNotesTopicList);
            }
        }
        tourismProjectTravelNotesWriteService.audit(tourismProjectTravelNotesWrite);
//        if (!tourismProjectTravelNotesWriteService.update(tourismProjectTravelNotesWrite, originalTourismProjectTravelNotesWrite)) {
//            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
//        }
        return ResponseResult.success();
    }


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

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

    /**
     * 列出符合过滤条件的发布游记列表。
     *
     * @param tourismProjectTravelNotesWriteDtoFilter 过滤对象。
     * @param orderParam 排序参数。
     * @param pageParam 分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @SaCheckPermission("tourismProjectTravelNotesWrite.view")
    @PostMapping("/list")
    public ResponseResult<MyPageData<TourismProjectTravelNotesWriteVo>> list(
            @MyRequestBody TourismProjectTravelNotesWriteDto tourismProjectTravelNotesWriteDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getCount());
        }
        TourismProjectTravelNotesWrite tourismProjectTravelNotesWriteFilter = MyModelUtil.copyTo(tourismProjectTravelNotesWriteDtoFilter, TourismProjectTravelNotesWrite.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, TourismProjectTravelNotesWrite.class);
        List<TourismProjectTravelNotesWrite> tourismProjectTravelNotesWriteList =
                tourismProjectTravelNotesWriteService.getTourismProjectTravelNotesWriteListWithRelation(tourismProjectTravelNotesWriteFilter, orderBy);

        tourismProjectTravelNotesWriteList.stream().forEach(item->{
            if(CollectionUtils.isNotEmpty(item.getTourTravelNotesTopicRelationList())){
                // 使用Stream API提取topicName并拼接成一个逗号分隔的字符串
                item.setTourTravelNotesTopicRelationName(item.getTourTravelNotesTopicRelationList().stream()
                        .map(TourTravelNotesTopicRelation::getTopicName) // 获取每个对象的topicName
                        .collect(Collectors.joining(",")));
            }
        });
        return ResponseResult.success(MyPageUtil.makeResponseData(tourismProjectTravelNotesWriteList, TourismProjectTravelNotesWriteVo.class));
    }

    /**
     * 查看指定发布游记对象详情。
     *
     * @param id 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @SaCheckPermission("tourismProjectTravelNotesWrite.view")
    @GetMapping("/view")
    public ResponseResult<TourismProjectTravelNotesWriteVo> view(@RequestParam Long id) {
        TourismProjectTravelNotesWrite tourismProjectTravelNotesWrite = tourismProjectTravelNotesWriteService.getByIdWithRelation(id, MyRelationParam.full());
        if (tourismProjectTravelNotesWrite == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        if(CollectionUtils.isNotEmpty(tourismProjectTravelNotesWrite.getTourTravelNotesTopicRelationList())){
            // 使用Stream API提取topicName并拼接成一个逗号分隔的字符串
            tourismProjectTravelNotesWrite.setTourTravelNotesTopicRelationName(tourismProjectTravelNotesWrite.getTourTravelNotesTopicRelationList().stream()
                    .map(TourTravelNotesTopicRelation::getTopicName) // 获取每个对象的topicName
                    .collect(Collectors.joining(",")));
        }

        TourismProjectTravelNotesWriteVo tourismProjectTravelNotesWriteVo = MyModelUtil.copyTo(tourismProjectTravelNotesWrite, TourismProjectTravelNotesWriteVo.class);

        TourTravelNotesImg tourTravelNotesImg = new TourTravelNotesImg();
        tourTravelNotesImg.setTravelNotesId(tourismProjectTravelNotesWrite.getId());
        List<TourTravelNotesImg> tourTravelNotesImgList = tourTravelNotesImgService.getTourTravelNotesImgList(tourTravelNotesImg, "");
        List<String> tourTravelNotesImgUrlList = tourTravelNotesImgList.stream().map(TourTravelNotesImg::getImgUrl).collect(Collectors.toList());
        tourismProjectTravelNotesWriteVo.setImgUrls(tourTravelNotesImgUrlList);
        return ResponseResult.success(tourismProjectTravelNotesWriteVo);
    }

    /**
     * 附件文件下载。
     * 这里将图片和其他类型的附件文件放到不同的父目录下，主要为了便于今后图片文件的迁移。
     *
     * @param id 附件所在记录的主键Id。
     * @param fieldName 附件所属的字段名。
     * @param filename  文件名。如果没有提供该参数，就从当前记录的指定字段中读取。
     * @param asImage   下载文件是否为图片。
     * @param response  Http 应答对象。
     */
//    @SaCheckPermission("tourismProjectTravelNotesWrite.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 {
                TourismProjectTravelNotesWrite tourismProjectTravelNotesWrite = tourismProjectTravelNotesWriteService.getById(id);
                if (tourismProjectTravelNotesWrite == null) {
                    ResponseResult.output(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
                String fieldJsonData = (String) ReflectUtil.getFieldValue(tourismProjectTravelNotesWrite, 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(TourismProjectTravelNotesWrite.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(),
                    TourismProjectTravelNotesWrite.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("tourismProjectTravelNotesWrite.view")
    @OperationLog(type = SysOperationLogType.UPLOAD, saveResponse = false)
    @PostMapping("/upload")
    public void upload(
            @RequestParam String fieldName,
            @RequestParam Boolean asImage,
            @RequestParam("uploadFile") MultipartFile uploadFile) throws IOException {
        if(uploadFile.getSize()>1024*1024*5){
            ResponseResult.output(HttpServletResponse.SC_FORBIDDEN,
                    ResponseResult.error(ErrorCodeEnum.INVALID_UPLOAD_FILE_IOERROR, "文件大小不能超过5M"));
            return;
        }
        UploadStoreInfo storeInfo = MyModelUtil.getUploadStoreInfo(TourismProjectTravelNotesWrite.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(), TourismProjectTravelNotesWrite.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<Void> doDelete(Long id) {
        String errorMessage;
        // 验证关联Id的数据合法性
        TourismProjectTravelNotesWrite originalTourismProjectTravelNotesWrite = tourismProjectTravelNotesWriteService.getById(id);
        if (originalTourismProjectTravelNotesWrite == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [对象] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!tourismProjectTravelNotesWriteService.remove(id)) {
            errorMessage = "数据操作失败，删除的对象不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        return ResponseResult.success();
    }
}
