package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.dao.HighDiscountCouponRelMapper;
import com.hai.entity.HighCoupon;
import com.hai.entity.HighDiscount;
import com.hai.entity.HighDiscountCouponRel;
import com.hai.entity.HighDiscountCouponRelExample;
import com.hai.service.HighCouponService;
import com.hai.service.HighDiscountCouponRelService;
import com.hai.service.HighDiscountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/4/3 22:52
 */
@Service("highDiscountCouponRelService")
public class HighDiscountCouponRelServiceImpl implements HighDiscountCouponRelService {

    @Resource
    private HighDiscountCouponRelMapper highDiscountCouponRelMapper;

    @Resource
    private HighCouponService highCouponService;

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void insertDiscountCouponList(List<HighDiscountCouponRel> highDiscountCouponRelList) {
        for (HighDiscountCouponRel rel : highDiscountCouponRelList) {
            highDiscountCouponRelMapper.insert(rel);
        }
    }

    @Override
    public void updateDiscountCoupon(HighDiscountCouponRel highDiscountCouponRel) {
        highDiscountCouponRelMapper.updateByPrimaryKey(highDiscountCouponRel);
    }

    @Override
    public void deleteDiscountCoupon(Long id) {
        highDiscountCouponRelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public HighDiscountCouponRel getRelById(Long id) {
        return highDiscountCouponRelMapper.selectByPrimaryKey(id);
    }

    @Override
    public HighDiscountCouponRel getRelByDiscountCoupon(Long discountId, Long couponId) {
        HighDiscountCouponRelExample example = new HighDiscountCouponRelExample();
        example.createCriteria().andDiscountIdEqualTo(discountId).andCouponIdEqualTo(couponId).andStatusEqualTo(1);
        List<HighDiscountCouponRel> list = highDiscountCouponRelMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<HighDiscountCouponRel> getRelByCoupon(Long couponId) {
        HighDiscountCouponRelExample example = new HighDiscountCouponRelExample();
        example.createCriteria().andCouponIdEqualTo(couponId).andStatusEqualTo(1);
        List<HighDiscountCouponRel> list = highDiscountCouponRelMapper.selectByExample(example);
        for (HighDiscountCouponRel rel : list) {
            rel.setHighCoupon(highCouponService.getCouponDetail(rel.getCouponId()));
        }
        return list;
    }

    @Override
    public List<HighDiscountCouponRel> getRelByDiscount(Long discountId) {
        HighDiscountCouponRelExample example = new HighDiscountCouponRelExample();
        example.createCriteria().andDiscountIdEqualTo(discountId).andStatusEqualTo(1);;
        List<HighDiscountCouponRel> list = highDiscountCouponRelMapper.selectByExample(example);
        for (HighDiscountCouponRel rel : list) {
            rel.setHighCoupon(highCouponService.getCouponDetail(rel.getCouponId()));
        }
        return list;
    }
}
