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

import cn.hutool.core.util.BooleanUtil;
import com.tmerclub.cloud.biz.constant.FileType;
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.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 lth
 * @Date 2021/6/1 15:20
 */
@RestController("supplierAttachFileController")
@RequestMapping("/s/attachFile")
@Tag(name = "supplier-上传文件记录表")
public class AttachFileController {

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


    @DeleteMapping("/deleteByIds")
    @Operation(summary = "根据文件id列表批量删除文件记录", description = "根据文件id列表批量删除文件记录")
    public ServerResponseEntity<Void> deleteByIds(@RequestBody List<Long> ids) {
        Long shopId = AuthUserContext.get().getTenantId();
        attachFileService.deleteByIdsAndShopId(ids, shopId);
        return ServerResponseEntity.success();
    }

    @PutMapping("/batchMove")
    @Operation(summary = "根据文件id列表与分组id批量移动文件", description = "根据文件id列表与分组id批量移动文件")
    public ServerResponseEntity<Void> batchMove(@RequestBody List<Long> ids, @RequestParam(value = "groupId", required = false, defaultValue = "") Long groupId) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        attachFileService.batchMoveByShopIdAndIdsAndGroupId(shopId, ids, groupId, sysType);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getFileById")
    @Operation(summary = "根据文件id获取文件信息")
    public ServerResponseEntity<AttachFileVO> getFileById(Long fileId) {
        AttachFileVO attachFileVO = attachFileService.getById(fileId);
        Long shopId = AuthUserContext.get().getTenantId();
        if (!Objects.equals(shopId, attachFileVO.getShopId())) {
            throw new LuckException("无法查看非本店铺的文件");
        }
        return ServerResponseEntity.success(attachFileVO);
    }

    @PostMapping("/savePdfFile")
    @Operation(summary = "保存商家发票")
    public ServerResponseEntity<AttachFileVO> savePdfFile(@RequestBody AttachFileDTO attachFileDTO) {
        AttachFile attachFile = BeanUtil.map(attachFileDTO, AttachFile.class);
        attachFile.setSysType(AuthUserContext.get().getSysType());
        attachFile.setType(FileType.FILE.value());
        attachFile.setShopId(AuthUserContext.get().getTenantId());
        attachFileService.saveFile(attachFile);
        return ServerResponseEntity.success(attachFileService.getById(attachFile.getFileId()));
    }

    @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(3);
        attachFileDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<AttachFileVO> attachFilePage = attachFileService.page(pageDTO, attachFileDTO);
        return ServerResponseEntity.success(attachFilePage);
    }

    @DeleteMapping
    @Operation(summary = "删除上传文件记录", description = "根据上传文件记录表id删除上传文件记录")
    public ServerResponseEntity<Void> delete(@RequestParam Long fileId) {
        Long tenantId = AuthUserContext.get().getTenantId();
        boolean isHome = Objects.equals(AuthUserContext.get().getSysType(), SysTypeEnum.SUPPLIER.value()) && Objects.equals(tenantId, 69L);
        if (BooleanUtil.isFalse(permission) && isHome) {
            throw new LuckException("抱歉，当前无操作权限");
        }
        attachFileService.deleteById(fileId);
        return ServerResponseEntity.success();
    }
}
