package jnpf.tempmodule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jnpf.entity.RollImage;
import jnpf.model.tempmodule.RollImageVo;
import jnpf.model.tempmodule.dto.RollImageDto;
import jnpf.tempmodule.mapper.RollImageMapper;
import jnpf.tempmodule.service.RollImageService;
import jnpf.util.ConstantUtil;
import jnpf.util.FtbUtil;
import jnpf.util.JsonUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 轮播图服务实现
 *
 * @author yanwenfu
 * @create 2023-10-10
 */
@Service
public class RollImageServiceImpl implements RollImageService {

    @Resource
    private RollImageMapper rollImageMapper;

    @Override
    public List<RollImageVo> getRollImageList(Integer bannerType) {

        LambdaQueryWrapper<RollImage> queryWrapper = new LambdaQueryWrapper<RollImage>()
                .eq(RollImage::getDeleteMark, ConstantUtil.NUM_FALSE)
                .eq(RollImage::getPublished, ConstantUtil.NUM_TRUE)
                .eq(RollImage::getBannerType, bannerType)
                .orderByAsc(RollImage::getSort);
        List<RollImage> list = rollImageMapper.selectList(queryWrapper);
        return JsonUtil.getJsonToList(list, RollImageVo.class);
    }

    @Override
    public void addRollImage(RollImageDto rollImageDto) {
        // 查询数据库最后一张轮播图
        LambdaQueryWrapper<RollImage> queryWrapper = new LambdaQueryWrapper<RollImage>()
                .eq(RollImage::getDeleteMark, ConstantUtil.NUM_FALSE)
                .eq(RollImage::getBannerType, rollImageDto.getBannerType())
                .orderByDesc(RollImage::getSort)
                .last("limit 1");
        RollImage image = rollImageMapper.selectOne(queryWrapper);
        int sort = null == image ? 1 : image.getSort() + 1;
        RollImage rollImage = new RollImage();
        rollImage.setBannerType(rollImageDto.getBannerType());
        rollImage.setId(FtbUtil.getId());
        rollImage.setImageUrl(rollImageDto.getImageUrl());
        rollImage.setImageType(1);
        rollImage.setPublished(ConstantUtil.NUM_TRUE);
        rollImage.setSort(sort);
        rollImage.setDeleteMark(ConstantUtil.NUM_FALSE);
        rollImageMapper.insert(rollImage);
    }

    @Override
    public void deleteRollImage(String id) {

        LambdaUpdateWrapper<RollImage> updateWrapper = new LambdaUpdateWrapper<RollImage>()
                .eq(RollImage::getId, id)
                .set(RollImage::getDeleteMark, ConstantUtil.NUM_TRUE);
        rollImageMapper.update(null, updateWrapper);
    }
}
