package com.fzm.mall.server.admin.opration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.opration.entity.dto.BannerDTO;
import com.fzm.mall.server.admin.opration.entity.dto.BannerSaveDTO;
import com.fzm.mall.server.admin.opration.entity.dto.BannerUpdateDTO;
import com.fzm.mall.server.admin.opration.mapper.BannerMapper;
import com.fzm.mall.server.admin.opration.model.Banner;
import com.fzm.mall.server.admin.opration.service.IBannerService;
import com.fzm.mall.server.admin.util.CopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 运营中心-banner 服务实现类
 * </p>
 *
 * @author wulixian
 * @since 2021-03-22
 */
@Service
@Transactional
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {

    @Resource
    private BannerMapper bannerMapper;

    @Override
    public void save(BannerSaveDTO bannerSaveDTO) {
        Banner banner = CopyUtil.to(bannerSaveDTO, Banner.class);
        banner.init();
        bannerMapper.insert(banner);
    }

    @Override
    public void update(BannerUpdateDTO bannerUpdateDTO) {
        Banner banner = CopyUtil.to(bannerUpdateDTO, Banner.class);

        UpdateWrapper<Banner> wrapper = new UpdateWrapper<>();
        wrapper.eq("operation_banner_id", banner.getOperationBannerId())
                .eq("merchant_id", banner.getMerchantId())
                .eq("update_time", banner.getUpdateTime());
        int update = bannerMapper.update(banner, wrapper);
        if (update == 0) {
            throw new MyException(MallResponseError.UPDATE_FAIL);
        }
    }

    /**
     * 逻辑删除
     *
     * @param operationBannerId
     * @param merchantId
     */
    @Override
    public void delete(String operationBannerId, String merchantId) {
        UpdateWrapper<Banner> wrapper = new UpdateWrapper<>();
        wrapper.eq("operation_banner_id", operationBannerId)
                .eq("merchant_id", merchantId)
                .set("status", true);
        bannerMapper.update(null, wrapper);
    }

    @Override
    public BannerDTO read(String operationBannerId, String merchantId) {
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("operation_banner_id", operationBannerId)
                .eq("merchant_id", merchantId);
        Banner banner = bannerMapper.selectOne(queryWrapper);

        return CopyUtil.to(banner, BannerDTO.class);
    }

    @Override
    public List<BannerDTO> list(String merchantId) {
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", merchantId)
                .eq("status", false)
                .orderByAsc("display_order")
                .orderByDesc("update_time");
        List<Banner> result = bannerMapper.selectList(queryWrapper);
        return CopyUtil.toList(result, BannerDTO.class);
    }
}
