
package com.tmerclub.cloud.biz.controller.admin;


import cn.hutool.core.util.BooleanUtil;
import com.tmerclub.cloud.biz.dto.AttachFileDTO;
import com.tmerclub.cloud.biz.model.AttachFile;
import com.tmerclub.cloud.biz.service.AttachFileService;
import com.tmerclub.cloud.biz.vo.AttachFileVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.SysTypeEnum;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 上传文件记录表
 *
 * @author YXF
 * @date 2020-11-21 10:21:40
 */
@RestController("adminAttachFileController")
@RequestMapping("/mp/attachFile")
@Tag(name = "admin-上传文件记录表")
public class AttachFileController {

    @Autowired
    private AttachFileService attachFileService;


    @Value("${tmerclub.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/page")
    @Operation(summary = "获取上传文件记录表列表", description = "分页获取上传文件记录表列表")
    public ServerResponseEntity<PageVO<AttachFileVO>> page(@Valid PageDTO pageDTO, AttachFileDTO attachFileDTO) {
        if (Objects.equals(attachFileDTO.getAttachFileGroupId(), 0L)) {
            attachFileDTO.setAttachFileGroupId(null);
        }
        attachFileDTO.setSysType(AuthUserContext.get().getSysType());
        PageVO<AttachFileVO> attachFilePage = attachFileService.page(pageDTO, attachFileDTO);
        return ServerResponseEntity.success(attachFilePage);
    }

    @PostMapping
    @Operation(summary = "保存上传文件记录", description = "保存上传文件记录")
    public ServerResponseEntity<Void> save(@RequestBody List<AttachFileDTO> attachFileDtos) {
        List<AttachFile> attachFiles = BeanUtil.mapAsList(attachFileDtos, AttachFile.class);
        attachFileService.save(attachFiles);
        return ServerResponseEntity.success();
    }

    /**
     * 更改文件名或分组
     */
    @PutMapping("/updateFile")
    @Operation(summary = "更新文件记录", description = "更新文件记录")
    public ServerResponseEntity<Boolean> updateFileName(@RequestBody AttachFileDTO attachFileDto) {
        if (Objects.isNull(attachFileDto.getFileName())) {
            // 文件名称不能为空
            throw new LuckException("文件名称不能为空");
        }
        AttachFile attachFile = BeanUtil.map(attachFileDto, AttachFile.class);
        attachFileService.updateFileName(attachFile);
        return ServerResponseEntity.success(Boolean.TRUE);
    }

    @DeleteMapping
    @Operation(summary = "删除上传文件记录", description = "根据上传文件记录表id删除上传文件记录")
    public ServerResponseEntity<Void> delete(@RequestParam Long fileId) {
        // 屏蔽线上演示账号 平台/商家home 的图片资源删除功能 此处判断可删除
        Integer sysType = AuthUserContext.get().getSysType();
        Long tenantId = AuthUserContext.get().getTenantId();
        boolean isPlatform = Objects.equals(sysType, SysTypeEnum.PLATFORM.value()) && Objects.equals(tenantId, Constant.PLATFORM_SHOP_ID);
        boolean isHome = Objects.equals(sysType, SysTypeEnum.MULTISHOP.value()) && Objects.equals(tenantId, 4L);
        if (BooleanUtil.isFalse(permission) && (isPlatform || isHome)) {
            throw new LuckException("抱歉，当前无操作权限");
        }
        attachFileService.deleteById(fileId);
        return ServerResponseEntity.success();
    }

}
