package com.hzs.bonus.bonus.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.bonus.mapper.BdWaresBonusItemsMapper;
import com.hzs.bonus.bonus.service.IBdWaresBonusItemsService;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.bonus.BdWaresBonusItems;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 特殊商品分配ev奖金明细 服务实现类
 *
 */
@Service
public class BdWaresBonusItemsServiceImpl extends ServiceImpl<BdWaresBonusItemsMapper, BdWaresBonusItems> implements IBdWaresBonusItemsService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insetWaresBonusItems(List<BdWaresBonusItems> waresBonusItemsList) {
        saveBatch(waresBonusItemsList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWaresBonusItems(List<BdWaresBonusItems> waresBonusItemsList) {
        updateBatchById(waresBonusItemsList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteWaresBonusItems(List<BdWaresBonusItems> waresBonusItemsList, Long pkWaresBonus, Long userId) {
        UpdateWrapper<BdWaresBonusItems> updateWrapper = new UpdateWrapper<>();
        if(waresBonusItemsList != null && waresBonusItemsList.size() > 0) {
            Set<Long> itemIdList = waresBonusItemsList.stream().map(BdWaresBonusItems::getPkId).collect(Collectors.toSet());
            updateWrapper.notIn(SystemFieldConstants.PK_ID, itemIdList);
        }
        updateWrapper.eq("PK_WARES_BONUS", pkWaresBonus);
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, DateUtils.currentDateTime());
        update(updateWrapper);
    }

}
