package com.willalp.event.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.event.domain.HsEventBusinessRelation;
import com.willalp.event.mapper.HsEventBusinessRelationMapper;
import com.willalp.event.service.IHsEventBusinessRelationService;
import org.springframework.stereotype.Service;

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

/**
 * 事件相关业务关系Service业务层处理
 *
 * @author willalp
 * @date 2023-01-29
 */
@Service
public class HsEventBusinessRelationServiceImpl extends ServiceImpl<HsEventBusinessRelationMapper, HsEventBusinessRelation> implements IHsEventBusinessRelationService {
    @Resource
    private HsEventBusinessRelationMapper hsEventBusinessRelationMapper;

    /**
     * 查询事件相关业务关系列表
     *
     * @param hsEventBusinessRelation 事件相关业务关系
     * @return 事件相关业务关系
     */
    @Override
    public List<HsEventBusinessRelation> selectHsEventBusinessRelationList(HsEventBusinessRelation hsEventBusinessRelation) {
        return hsEventBusinessRelationMapper.selectHsEventBusinessRelationList(hsEventBusinessRelation);
    }

    @Override
    public boolean doBindingRelation(HsEventBusinessRelation hsEventBusinessRelation) {
        String eventId = hsEventBusinessRelation.getEventId();
        if (StringUtils.isBlank(eventId)) {
            throw new BaseException("事件ID不能为空");
        }
        String businessType = hsEventBusinessRelation.getBusinessType();
        if (StringUtils.isBlank(businessType)) {
            throw new BaseException("事件绑定类型不能为空");
        }
        List<String> eventBusinessIds = hsEventBusinessRelation.getEventBusinessIds();
        if (CollectionUtil.isEmpty(eventBusinessIds)) {
            throw new BaseException("业务IDs不能为空");
        }
        List<HsEventBusinessRelation> relationList = getBaseMapper().selectList(null/*new QueryWrapper<HsEventBusinessRelation>()
                .eq("event_id", eventId)*/);
        List<HsEventBusinessRelation> list = new ArrayList<>();
        for (String id : eventBusinessIds) {
            int count = 0;
            //去重
            for (HsEventBusinessRelation eventBusinessRelation : relationList) {
                String businessId = eventBusinessRelation.getBusinessId();
                if (StringUtils.isNotBlank(businessId)) {
                    if (StringUtils.equals(businessId, id) &&
                            StringUtils.isNotBlank(eventBusinessRelation.getEventId()) &&
                            !StringUtils.equals(eventId, eventBusinessRelation.getEventId())) {
                        throw new BaseException("有商品已绑定其他活动，请检查");
                    } else if (StringUtils.equals(businessId, id)) {
                        count++;
                    }
                }
            }
            if (count == 0) {
                HsEventBusinessRelation businessRelation = new HsEventBusinessRelation();
                businessRelation.setEventId(eventId);
                businessRelation.setBusinessType(businessType);
                businessRelation.setBusinessId(id);
                list.add(businessRelation);
            }
        }
        if (list.size() == 0) {
            throw new BaseException("已有绑定数据");
        }
        return saveBatch(list);
    }

    @Override
    public int agencyDeleteShopCar(String id) {
        return baseMapper.agencyDeleteShopCar(id);
    }
}
