package com.tourism.webadmin.app.website.controller;

import com.tourism.common.additional.config.ApplicationConfig;
import com.tourism.common.core.annotation.DisableDataFilter;
import com.tourism.common.core.constant.ErrorCodeEnum;
import com.tourism.common.core.object.ResponseResult;
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.core.util.MyModelUtil;
import com.tourism.common.log.annotation.OperationLog;
import com.tourism.common.log.model.constant.SysOperationLogType;
import com.tourism.common.redis.cache.SessionCacheHelper;
import com.tourism.webadmin.back.dto.TourImComplaitDto;
import com.tourism.webadmin.back.model.TourImComplait;
import com.tourism.webadmin.back.service.TourImComplaitService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
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 java.io.IOException;

@Tag(name = "投诉内容管理")
@Slf4j
@RestController
@DisableDataFilter
@RequestMapping("/website/tourComplait")
public class TourComplaitController {
    @Autowired
    private TourImComplaitService tourImComplaitService;
    @Autowired
    private UpDownloaderFactory upDownloaderFactory;
    @Autowired
    private ApplicationConfig appConfig;
    @Autowired
    private SessionCacheHelper cacheHelper;

    /**
     * 新增投诉内容管理数据。
     * @param tourImComplaitDto 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @PostMapping("/add")
    public ResponseResult<String> add(@RequestBody TourImComplaitDto tourImComplaitDto) {

        if (tourImComplaitDto.getObjectType().equals(1)) {
            //用户
            tourImComplaitDto.setGroupId(null);
        }else {
            tourImComplaitDto.setUserId(null);
        }
        TourImComplait tourImComplait = MyModelUtil.copyTo(tourImComplaitDto, TourImComplait.class);

        tourImComplait = tourImComplaitService.saveNew(tourImComplait);
        return ResponseResult.success(tourImComplait.getId().toString());
    }

    /**
     * 文件上传操作。
     *
     * @param fieldName  上传文件名。
     * @param asImage    是否作为图片上传。如果是图片，今后下载的时候无需权限验证。否则就是附件上传，下载时需要权限验证。
     * @param uploadFile 上传文件对象。
     */
    @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(TourImComplait.class, fieldName);
        // 根据字段注解中的存储类型，通过工厂方法获取匹配的上传下载实现类，从而解耦。
        BaseUpDownloader upDownloader = upDownloaderFactory.get(storeInfo.getStoreType());
        UploadResponseInfo responseInfo = upDownloader.doUpload(null,
                appConfig.getUploadFileBaseDir(), TourImComplait.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));
    }
}
