package com.hai.service.impl;

import com.hai.dao.HighActivityInfoMapper;
import com.hai.entity.HighActivityInfo;
import com.hai.entity.HighActivityInfoExample;
import com.hai.entity.HighActivityPartakeRule;
import com.hai.enum_type.ActivityInfoStatus;
import com.hai.service.HighActivityInfoService;
import com.hai.service.HighActivityPartakeRuleService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 活动业务
 * @author hurui
 */
@Service("activityInfoService")
public class HighActivityInfoServiceImpl implements HighActivityInfoService {

    @Resource
    private HighActivityInfoMapper highActivityInfoMapper;

    @Resource
    private HighActivityPartakeRuleService highActivityPartakeRuleService;

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
    public void editActivityBusiness(HighActivityInfo activityInfo, List<HighActivityPartakeRule> partakeRuleList) {
        if (activityInfo.getId() == null) {
            activityInfo.setCreateTime(new Date());
            activityInfo.setUpdateTime(new Date());
            activityInfo.setStatus(ActivityInfoStatus.status1.getNumber());
            highActivityInfoMapper.insert(activityInfo);
        } else {
            activityInfo.setUpdateTime(new Date());
            highActivityInfoMapper.updateByPrimaryKey(activityInfo);
        }

        // 查询活动的参与规则
        List<HighActivityPartakeRule> activityPartakeRuleList = highActivityPartakeRuleService.getListByActivityId(activityInfo.getId());
        // 筛选出已经被删除的
        for (HighActivityPartakeRule rule : activityPartakeRuleList) {
            List<HighActivityPartakeRule> partakeRules = partakeRuleList.stream()
                    .filter(o -> o.getPartakeMode().equals(rule.getPartakeMode())
                            && o.getProductType().equals(rule.getProductType())
                            && o.getProductId().equals(rule.getProductId()))
                    .collect(Collectors.toList());
            if (partakeRules == null || partakeRules.size() == 0) {
                rule.setStatus(0);
                highActivityPartakeRuleService.editRule(rule);
            }
        }
        // 增加规则
        for (HighActivityPartakeRule rule : partakeRuleList) {
            rule.setActivityInfoId(activityInfo.getId());
            // 校验规则是否已存在
            if (highActivityPartakeRuleService.getDetail(rule.getActivityInfoId(), rule.getPartakeMode(), rule.getProductType(),rule.getProductId()) == null) {
                rule.setCreateTime(new Date());
                rule.setStatus(1);
                highActivityPartakeRuleService.editRule(rule);
            }
        }
    }

    @Override
    public void updateActivityInfo(HighActivityInfo activityInfo) {
        highActivityInfoMapper.updateByPrimaryKey(activityInfo);
    }

    @Override
    public HighActivityInfo getDetailById(Long activityId) {
        return highActivityInfoMapper.selectByPrimaryKey(activityId);
    }

    @Override
    public List<HighActivityInfo> getList(Map<String, Object> param) {
        HighActivityInfoExample example = new HighActivityInfoExample();
        HighActivityInfoExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(ActivityInfoStatus.status0.getNumber());

        if (StringUtils.isNotBlank(MapUtils.getString(param, "title"))) {
            criteria.andTitleLike("%"+MapUtils.getString(param, "title")+"%");
        }

        if (MapUtils.getInteger(param, "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(param, "type"));
        }

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

        example.setOrderByClause("create_time desc");
        return highActivityInfoMapper.selectByExample(example);
    }

    @Override
    public Map<String, Object> getNewActivityByCouponId(Long couponId) {
        return highActivityInfoMapper.selectNewActivityByCouponId(couponId);
    }
}
