package com.hai.service.impl;

import com.hai.dao.HighCouponHandselMapper;
import com.hai.entity.HighCoupon;
import com.hai.entity.HighCouponHandsel;
import com.hai.entity.HighCouponHandselExample;
import com.hai.model.HighCouponHandselModel;
import com.hai.service.HighCouponHandselService;
import com.hai.service.HighCouponService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName HighCouponHandselServiceImpl
 * @Description: TODO ()
 * @Author 胡锐
 * @Date 2021/3/16
 **/
@Service("highCouponHandselService")
public class HighCouponHandselServiceImpl implements HighCouponHandselService {

    @Resource
    private HighCouponHandselMapper highCouponHandselMapper;

    @Resource
    private HighCouponService highCouponService;

    @Override
    public void insertCouponHandsel(HighCouponHandsel highCouponHandsel) {
        highCouponHandselMapper.insert(highCouponHandsel);
    }

    @Override
    public void updateCouponHandsel(HighCouponHandsel highCouponHandsel) {
        highCouponHandselMapper.updateByPrimaryKey(highCouponHandsel);
    }

    @Override
    public HighCouponHandselModel getCouponHandselById(Long id) {
        HighCouponHandsel handsel = highCouponHandselMapper.selectByPrimaryKey(id);
        if (handsel != null) {
            HighCouponHandselModel model = new HighCouponHandselModel();
            BeanUtils.copyProperties(handsel, model);
            model.setHighCouponModel(highCouponService.getCouponDetail(handsel.getId()));
            return model;
        }
        return null;
    }

    @Override
    public List<HighCouponHandselModel> getHandselListByCoupon(Long couponId) {
        HighCouponHandselExample example = new HighCouponHandselExample();
        example.createCriteria().andCouponIdEqualTo(couponId).andStatusEqualTo(1);
        List<HighCouponHandsel> list = highCouponHandselMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            HighCouponHandselModel model;
            List<HighCouponHandselModel> modelList = new ArrayList<>();
            for (HighCouponHandsel handsel : list) {
                model = new HighCouponHandselModel();
                BeanUtils.copyProperties(handsel, model);
                model.setHighCouponModel(highCouponService.getCouponDetail(handsel.getHandselCouponId()));
                modelList.add(model);
            }
            return modelList;
        }
        return new ArrayList<>();
    }

}
