package com.starry.admin.modules.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.common.oss.service.IOssFileService;
import com.starry.admin.modules.shop.module.entity.PlayShopCarouselInfoEntity;
import com.starry.admin.modules.shop.module.vo.PlayShopCarouselInfoAddVo;
import com.starry.admin.modules.shop.module.vo.PlayShopCarouselInfoQueryVo;
import com.starry.admin.modules.shop.module.vo.PlayShopCarouselInfoUpdateStateVo;
import com.starry.admin.modules.shop.service.IPlayShopCarouselInfoService;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.io.IOException;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 店铺首页轮播Controller
 *
 * @author admin
 * @since 2024-06-05
 */
@Api(tags = "店铺轮播图管理", description = "店铺首页轮播图信息管理相关接口，包括查询、新增、修改和删除等操作")
@RestController
@RequestMapping("/shop/carousel")
public class PlayShopCarouselInfoController {
    @Resource
    private IPlayShopCarouselInfoService playCarouselInfoService;

    @Resource
    private IOssFileService ossFileService;

    /**
     * 查询店铺首页轮播列表
     */
    @ApiOperation(value = "分页查询轮播图列表", notes = "分页查询店铺首页轮播图信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayShopCarouselInfoEntity.class, responseContainer = "Page")})
    // @PreAuthorize("@customSs.hasPermission('shop:carousel:list')")
    @PostMapping("/listByPage")
    public R listByPage(
            @ApiParam(value = "查询条件", required = true) @Validated @RequestBody PlayShopCarouselInfoQueryVo vo) {
        IPage<PlayShopCarouselInfoEntity> list = playCarouselInfoService.selectByPage(vo);
        return R.ok(list);
    }

    @ApiOperation(value = "上传轮播图文件", notes = "上传店铺首页轮播图文件")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功，返回文件地址"),
            @ApiResponse(code = 500, message = "上传失败，包含详细错误信息")})
    // @PreAuthorize("@customSs.hasPermission('shop:carousel:add')")
    @PostMapping("/uploadFile")
    public R uploadFile(@ApiParam(value = "轮播图文件", required = true) @RequestParam("file") MultipartFile file)
            throws IOException {
        String fileAddress = ossFileService.upload(file.getInputStream(), SecurityUtils.getTenantId(),
                file.getOriginalFilename());
        return R.ok(fileAddress);
    }

    /**
     * 新增页轮播列表
     */
    @ApiOperation(value = "新增轮播图", notes = "新增店铺首页轮播图信息")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "添加失败，包含详细错误信息")})
    // @PreAuthorize("@customSs.hasPermission('shop:carousel:add')")
    @PostMapping("/create")
    public R create(@ApiParam(value = "轮播图信息", required = true) @Validated @RequestBody PlayShopCarouselInfoAddVo vo) {
        PlayShopCarouselInfoEntity entity = ConvertUtil.entityToVo(vo, PlayShopCarouselInfoEntity.class);
        playCarouselInfoService.create(entity);
        return R.ok();
    }

    /**
     * 修改页轮播状态
     */
    @ApiOperation(value = "更新轮播图状态", notes = "修改店铺首页轮播图状态")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "修改失败，包含详细错误信息")})
    // @PreAuthorize("@customSs.hasPermission('shop:carousel:update')")
    @PostMapping("/handleUpdateState")
    public R listByPage(
            @ApiParam(value = "更新信息", required = true) @Validated @RequestBody PlayShopCarouselInfoUpdateStateVo vo) {
        PlayShopCarouselInfoEntity entity = ConvertUtil.entityToVo(vo, PlayShopCarouselInfoEntity.class);
        playCarouselInfoService.update(entity);
        return R.ok();
    }

    /**
     * 删除店铺首页轮播
     */
    @ApiOperation(value = "删除轮播图", notes = "根据ID批量删除店铺首页轮播图信息")
    @ApiImplicitParam(name = "ids", value = "轮播图ID数组", required = true, paramType = "path", dataType = "String[]", example = "1,2,3")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    // @PreAuthorize("@customSs.hasPermission('shop:carousel:delete')")
    @Log(title = "店铺首页轮播", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.ok(playCarouselInfoService.deletePlayCarouselInfoByIds(ids));
    }

}
