package cn.com.ty.lift.system.banner.service.impl;

import cn.com.ty.lift.system.banner.dao.entity.BannerImage;
import cn.com.ty.lift.system.banner.dao.mapper.BannerImageMapper;
import cn.com.ty.lift.system.banner.service.BannerImageService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * banner图管理 服务实现类
 * </p>
 *
 * @author wang-hai-cheng
 * @since 2019-12-31
 */
@Service
public class BannerImageServiceImpl extends ServiceImpl<BannerImageMapper, BannerImage> implements BannerImageService {

    /**
     * @param banner banner的id
     * @return 操作结果
     */
    public RestResponse modify(BannerImage banner) {
        if (this.saveOrUpdate(banner)) {
            return RestResponse.success(banner.getId());
        }
        return RestResponse.fail();
    }

    /**
     * 删除banner
     *
     * @param banner banner的id
     * @return 操作结果
     */
    public RestResponse delete(BannerImage banner) {
        if (this.removeById(banner.getId())) {
            return RestResponse.success(null, "成功");
        }
        return RestResponse.fail();
    }

    /**
     * banner列表
     *
     * @param banner banner的type
     * @return banner列表
     */
    public RestResponse banners(BannerImage banner) {
        List<BannerImage> banners = this.list(new QueryWrapper<BannerImage>()
                .eq(null != banner.getType(), "type", banner.getType())
                .orderByAsc("serial_no"));
        if (banners.isEmpty()) {
            return RestResponse.success();
        }
        return RestResponse.success(banners);
    }
}
