package com.cpop.system.business.controller.backstage;

import com.cpop.core.base.R;
import com.cpop.core.config.CpopConfig;
import com.cpop.core.config.ServerConfig;
import com.cpop.core.handler.TencentCosHandler;
import com.cpop.core.utils.file.FileUploadUtils;
import com.cpop.core.utils.file.FileUtils;
import com.cpop.system.business.bo.SyncBrandAndStoreBo;
import com.cpop.system.business.service.StoreService;
import com.cpop.system.business.vo.SysFileVo;
import com.qcloud.cos.model.UploadResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author DB
 * @createTime 2023/09/13 15:10
 * @description 通用接口
 */
@Slf4j
@Api(tags = "通用接口")
@RestController
@RequestMapping("/backstage/sysCommon")
public class BackstageSysCommonController {

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private CpopConfig cpopConfig;

    @Autowired
    private TencentCosHandler tencentCosHandler;

    @Autowired
    private StoreService storeService;

    /**
     * 通用上传请求（单个）
     */
    @ApiOperation("通用上传请求（单个）")
    @PostMapping("/upload")
    public R<SysFileVo> uploadFile(MultipartFile file) {
        try {
            // 上传文件路径
            String filePath = cpopConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.getInstance().upload(filePath, file);
            //强制将http变成https
            //String https = serverConfig.getUrl().replace("http:", "https:");
            //String url = https + fileName;
            String url = serverConfig.getUrl() + fileName;
            SysFileVo sysFileVo = new SysFileVo();
            sysFileVo.setUrl(url)
                    .setFileName(fileName)
                    .setNewFileName(FileUtils.getInstance().getName(fileName))
                    .setOriginalFilename(file.getOriginalFilename());
            return R.ok(sysFileVo);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * @descriptions cdn加速上传
     * @author DB
     * @date 2023/10/31 17:24
     * @param file 文件
     * @return: com.cpop.core.base.R<com.qcloud.cos.model.UploadResult>
     */
    @ApiOperation("cdn加速上传")
    @PostMapping("/cdnUpload")
    public R<SysFileVo> cdnUpload(MultipartFile file) {
        UploadResult uploadResult = tencentCosHandler.cdnUpload(file);
        String filename = FileUploadUtils.getInstance().extractFilename(file);
        SysFileVo sysFileVo = new SysFileVo();
        sysFileVo.setUrl( "https://" + uploadResult.getBucketName() + tencentCosHandler.getCdnUrl() + uploadResult.getKey())
                .setFileName(filename)
                .setNewFileName(FileUtils.getInstance().getName(filename))
                .setOriginalFilename(file.getOriginalFilename());
        return R.ok(sysFileVo);
    }

}
