package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeCarouselImage;
import shop.alien.mapper.LifeCarouselImageMapper;

@Api(tags = {"一期-用户轮播图"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/userCarouselImage")
@RequiredArgsConstructor
public class UserCarouselImageController {

    private final LifeCarouselImageMapper lifeCarouselImageMapper;

    @ApiOperation("首页轮播图")
    @ApiOperationSupport(order = 1)
    @GetMapping("/getUserCarouselImage")
    public R<String> getUserCarouselImage() {
        log.info("UserLunbotuController.getUserCarouselImage");
        LifeCarouselImage stores = lifeCarouselImageMapper.selectById(1);
        if (!ObjectUtils.isEmpty(stores)) {
            return R.data(stores.getImage());
        } else {
            return R.fail("");
        }
    }

    @PostMapping("/uploadUserLunbotu")
    public String uploadUserLunbotu(@RequestParam String imagePath) {
        log.info("UserLunbotuController.uploadUserLunbotu?imagePath={}", imagePath);
        LifeCarouselImage stores = new LifeCarouselImage();
        stores.setImage(imagePath);
        LambdaUpdateWrapper<LifeCarouselImage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(LifeCarouselImage::getId, 1);
        wrapper.set(LifeCarouselImage::getImage, imagePath);
        lifeCarouselImageMapper.update(null, wrapper);
        if (!ObjectUtils.isEmpty(stores)) {
            return stores.getImage();
        } else {
            return ""; // 返回登录页面
        }
    }

}
