package com.hai.service.impl;

import com.hai.dao.HighActivityPartakeRuleMapper;
import com.hai.entity.HighActivityPartakeRule;
import com.hai.entity.HighActivityPartakeRuleExample;
import com.hai.service.HighActivityPartakeRuleService;
import org.springframework.stereotype.Service;

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

/**
 * 活动参与规则
 * @author hurui
 */
@Service("activityPartakeRuleService")
public class HighActivityPartakeRuleServiceImpl implements HighActivityPartakeRuleService {

    @Resource
    private HighActivityPartakeRuleMapper highActivityPartakeRuleMapper;

    @Override
    public void editRule(HighActivityPartakeRule partakeRule) {
        if (partakeRule.getId() == null) {
            partakeRule.setStatus(1);
            partakeRule.setCreateTime(new Date());
            highActivityPartakeRuleMapper.insert(partakeRule);
        } else {
            highActivityPartakeRuleMapper.updateByPrimaryKey(partakeRule);
        }
    }

    @Override
    public HighActivityPartakeRule getDetail(Long activityId, Integer partakeMode, Integer productType, Long productId) {
        HighActivityPartakeRuleExample example = new HighActivityPartakeRuleExample();
        example.createCriteria()
                .andActivityInfoIdEqualTo(activityId)
                .andPartakeModeEqualTo(partakeMode)
                .andProductTypeEqualTo(productType)
                .andProductIdEqualTo(productId)
                .andStatusEqualTo(1)
        ;
        List<HighActivityPartakeRule> list = highActivityPartakeRuleMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<HighActivityPartakeRule> getListByActivityId(Long activityId) {
        HighActivityPartakeRuleExample example = new HighActivityPartakeRuleExample();
        example.createCriteria().andActivityInfoIdEqualTo(activityId).andStatusEqualTo(1);
        return highActivityPartakeRuleMapper.selectByExample(example);
    }

    @Override
    public List<HighActivityPartakeRule> getProductList(Long activityId, Integer partakeMode, Integer productType) {
        HighActivityPartakeRuleExample example = new HighActivityPartakeRuleExample();
        example.createCriteria()
                .andStatusNotEqualTo(0)
                .andActivityInfoIdEqualTo(activityId)
                .andPartakeModeEqualTo(partakeMode)
                .andProductTypeEqualTo(productType);
        return highActivityPartakeRuleMapper.selectByExample(example);
    }

    @Override
    public List<Map<String, Object>> countRule(Long activityId) {
        return highActivityPartakeRuleMapper.countRule(activityId);
    }
}
