
package com.tmerclub.cloud.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.user.dto.RechargeCouponDTO;
import com.tmerclub.cloud.user.mapper.UserRechargeCouponMapper;
import com.tmerclub.cloud.user.model.UserRechargeCoupon;
import com.tmerclub.cloud.user.service.UserRechargeCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 余额优惠券关联表
 *
 * @author tmerclub
 * @date 2021-04-27 15:51:36
 */
@Service
public class UserRechargeCouponServiceImpl implements UserRechargeCouponService {

    @Autowired
    private UserRechargeCouponMapper userRechargeCouponMapper;

    @Override
    public PageVO<UserRechargeCoupon> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> userRechargeCouponMapper.list());
    }

    @Override
    public UserRechargeCoupon getByRechargeId(Long rechargeId) {
        return userRechargeCouponMapper.getByRechargeId(rechargeId);
    }

    @Override
    public void save(UserRechargeCoupon userRechargeCoupon) {
        userRechargeCouponMapper.save(userRechargeCoupon);
    }

    @Override
    public void update(UserRechargeCoupon userRechargeCoupon) {
        userRechargeCouponMapper.update(userRechargeCoupon);
    }

    @Override
    public void deleteById(Long rechargeId) {
        userRechargeCouponMapper.deleteById(rechargeId);
    }

    @Override
    public void insertBatch(Long rechargeId, List<RechargeCouponDTO> couponList) {
        List<UserRechargeCoupon> userRechargeCouponList = Lists.newArrayList();
        for (RechargeCouponDTO coupon : couponList) {
            UserRechargeCoupon rechargeCoupon = new UserRechargeCoupon();
            rechargeCoupon.setRechargeId(rechargeId);
            rechargeCoupon.setCouponId(coupon.getCouponId());
            rechargeCoupon.setCouponNum(coupon.getCouponNum());
            userRechargeCouponList.add(rechargeCoupon);
        }
        userRechargeCouponMapper.saveBatch(userRechargeCouponList);
    }

    @Override
    public void removeByRechargeIdAndCouponId(Long rechargeId, Set<Long> couponIdList) {
        userRechargeCouponMapper.removeByRechargeIdAndCouponId(rechargeId, couponIdList);
    }

    @Override
    public void updateBatchByCoupons(Long rechargeId, List<RechargeCouponDTO> updateList) {
        List<UserRechargeCoupon> couponList = BeanUtil.mapAsList(updateList, UserRechargeCoupon.class);
        userRechargeCouponMapper.updateBatchByCoupons(rechargeId, couponList);
    }

    @Override
    public void cancelBindingCoupons(List<Long> couponIds) {
        List<Long> rechargeIds = userRechargeCouponMapper.listRechargeIdByCouponIds(couponIds);
        if (CollUtil.isEmpty(rechargeIds)) {
            return;
        }
        userRechargeCouponMapper.removeByCouponIds(couponIds);
        List<String> keys = new ArrayList<>(Constant.INITIAL_CAPACITY);
        // 清除余额充值模板缓存
        for (Long rechargeId : rechargeIds) {
            keys.add(CacheNames.USER_RECHARGE_INFO + CacheNames.UNION + rechargeId);
        }
        keys.add(CacheNames.USER_RECHARGE_LIST);
        RedisUtil.deleteBatch(keys);
    }

}
