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 org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 店铺首页轮播Controller
 *
 * @author admin
 * @since 2024-06-05
 */
@RestController
@RequestMapping("/shop/carousel")
public class PlayShopCarouselInfoController {
    @Resource
    private IPlayShopCarouselInfoService playCarouselInfoService;

    @Resource
    private IOssFileService ossFileService;

    /**
     * 查询店铺首页轮播列表
     */
    //@PreAuthorize("@customSs.hasPermission('shop:carousel:list')")
    @PostMapping("/listByPage")
    public R listByPage(@Validated @RequestBody PlayShopCarouselInfoQueryVo vo) {
        IPage<PlayShopCarouselInfoEntity> list = playCarouselInfoService.selectByPage(vo);
        return R.ok(list);
    }

    //@PreAuthorize("@customSs.hasPermission('shop:carousel:add')")
    @PostMapping("/uploadFile")
    public R uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String fileAddress = ossFileService.upload(file.getInputStream(), SecurityUtils.getTenantId(), file.getOriginalFilename());
        return R.ok(fileAddress);
    }


    /**
     * 新增页轮播列表
     */
    //@PreAuthorize("@customSs.hasPermission('shop:carousel:add')")
    @PostMapping("/create")
    public R create(@Validated @RequestBody PlayShopCarouselInfoAddVo vo) {
        PlayShopCarouselInfoEntity entity = ConvertUtil.entityToVo(vo, PlayShopCarouselInfoEntity.class);
        playCarouselInfoService.create(entity);
        return R.ok();
    }


    /**
     * 修改页轮播状态
     */
    //@PreAuthorize("@customSs.hasPermission('shop:carousel:update')")
    @PostMapping("/handleUpdateState")
    public R listByPage(@Validated @RequestBody PlayShopCarouselInfoUpdateStateVo vo) {
        PlayShopCarouselInfoEntity entity = ConvertUtil.entityToVo(vo, PlayShopCarouselInfoEntity.class);
        playCarouselInfoService.update(entity);
        return R.ok();
    }

    /**
     * 删除店铺首页轮播
     */
    //@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));
    }


}
