/*
*  Copyright 2019-2025 Tz
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import cn.hutool.core.util.ObjectUtil;
import me.zhengjie.annotation.Log;
import me.zhengjie.config.properties.FileProperties;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.BusCarousel;
import me.zhengjie.modules.system.domain.dto.BusCarouselQueryCriteria;
import me.zhengjie.modules.system.service.BusCarouselService;
import lombok.RequiredArgsConstructor;

import java.io.File;
import java.util.ArrayList;
import java.util.Set;

import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;
import org.springframework.web.multipart.MultipartFile;

/**
* @author tangz
* @date 2025-07-30
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "系统：轮播图")
@RequestMapping("/api/busCarousel")
public class BusCarouselController {

    private final BusCarouselService busCarouselService;
    private final FileProperties fileProperties;

    @GetMapping
    @ApiOperation("查询carousel")
    @PreAuthorize("@el.check('busCarousel:list')")
    public ResponseEntity<PageResult<BusCarousel>> queryBusCarousel(BusCarouselQueryCriteria criteria){
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(busCarouselService.queryAll(criteria,page),HttpStatus.OK);
    }

    @DeleteMapping
    @Log("删除carousel")
    @ApiOperation("删除用户")
    @PreAuthorize("@el.check('busCarousel:del')")
    public ResponseEntity<Object> deleteBusCarousel(@RequestBody Set<Long> ids){
        for (Long id : ids) {
            busCarouselService.deleteAll(new ArrayList(){{ add(id); }});
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("新增carousel")
    @ApiOperation("上传轮播图")
    @PostMapping("/pictures")
    public ResponseEntity<String> uploadPicture(@RequestParam String sort, @RequestParam MultipartFile file){
        // 判断文件是否为图片
        String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
        if(!FileUtil.IMAGE.equals(FileUtil.getFileType(suffix))){
            throw new BadRequestException("只能上传图片");
        }
        String type = FileUtil.getFileType(suffix);
        String path = fileProperties.getPath().getAvatar();
        File uploadFile = FileUtil.upload(file, path + type + File.separator);
        if(ObjectUtil.isNull(uploadFile)){
            throw new BadRequestException("上传失败");
        }
        String[] split = path.split("/");
        String imgUrl = "/" + split[split.length - 1] + "/" + type + "/" + uploadFile.getName();
        BusCarousel resources = new BusCarousel();
        resources.setSort(StringUtils.isEmpty(sort) ? null : Integer.parseInt(sort));
        resources.setPath(imgUrl);
        busCarouselService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
}