package com.gic.haoban.manage.service.context.chat;

import com.gic.api.base.commons.Constant;
import com.gic.enterprise.api.service.ScreeningDetailService;
import com.gic.haoban.manage.api.dto.chat.*;
import com.gic.haoban.manage.api.enums.chat.ChatActivityRewardEnum;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivity;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivityJoin;
import com.gic.marketing.api.enums.MarketingBusinessTypeEnum;
import com.gic.marketing.api.enums.MarketingTagModuleEnum;
import com.gic.marketing.api.qdto.CouponReferAlarmQDTO;
import com.gic.marketing.api.service.CouponReferAlarmService;
import com.gic.member.tag.api.dto.MemberTagBusinessGroupDTO;
import com.gic.member.tag.api.service.MemberTagBusinessGroupApiService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by wangzubo on 2024/8/23.
 */
@Component
public class BusinessManager {

    private static final Logger logger = LoggerFactory.getLogger(BusinessManager.class);

    @Autowired
    private MemberTagBusinessGroupApiService memberTagBusinessGroupApiService;
    @Autowired
    private ScreeningDetailService screeningDetailService;
    @Autowired
    private CouponReferAlarmService couponReferAlarmService;

    public void dealMemberTagByActivity(TabGroupChatActivity activityDTO, TabGroupChatActivityJoin activityJoin , boolean delete) {
        String enterpriseId = activityDTO.getEnterpriseId();
        Long chatActivityId = activityDTO.getChatActivityId();
        if (delete) {
            logger.info("删除计划使用人群分组关联关系chatActivityId:{}", chatActivityId);
            memberTagBusinessGroupApiService.deleteByBusinessIdAndEnterpriseId(String.valueOf(chatActivityId), enterpriseId);
            return;
        }
        Integer memberType = activityJoin.getMemberType();
        String filterJson = activityJoin.getFilterJson();
        String filterJsonView = activityJoin.getFilterJsonView();
        if (StringUtils.isNotBlank(filterJson) && Objects.equals(memberType,1)) {
            String[] split = filterJson.split(",");
            MemberTagBusinessGroupDTO memberTagBusinessGroupDTO = new MemberTagBusinessGroupDTO();
            memberTagBusinessGroupDTO.setTagGroupIdList(Arrays.asList(split));
            memberTagBusinessGroupDTO.setBusinessId(String.valueOf(chatActivityId));
            memberTagBusinessGroupDTO.setEnterpriseId(enterpriseId);
            memberTagBusinessGroupDTO.setBusinessType(MarketingBusinessTypeEnum.Marketing_business_type.getCode());
            memberTagBusinessGroupDTO.setBusinessModuleName(MarketingTagModuleEnum.GROUP_CHAT_ACTIVITY.getModuleTypeName());
            memberTagBusinessGroupDTO.setBusinessName(activityDTO.getActivityName());
            memberTagBusinessGroupDTO.setBusinessLink(MarketingTagModuleEnum.GROUP_CHAT_ACTIVITY.getPagePath());
            logger.info("处理计划使用人群分组关联关系chatActivityId:{}", chatActivityId);
            memberTagBusinessGroupApiService.batchDelAndAddByBusinessId(memberTagBusinessGroupDTO);
        }
        if (Objects.equals(memberType, 0) && filterJsonView != null) {
            screeningDetailService.save("GROUP_CHAT_SEARCH_ID_" + activityDTO.getChatActivityId() + "-" + activityJoin.getIndex(), filterJsonView);
        }
    }

    public void dealCouponAlarmByActivity(GroupChatActivityDTO activityDTO,boolean delete) {
        if (Objects.equals(activityDTO.getExceptionNotice(), Constant.FLAG_TRUE) && !delete) {
            CouponReferAlarmQDTO couponReferAlarmQDTO = new CouponReferAlarmQDTO();
            couponReferAlarmQDTO.setSystemType(0);
            couponReferAlarmQDTO.setStartTime(activityDTO.getStartDate());
            couponReferAlarmQDTO.setEndTime(activityDTO.getLimitDate() != null ? activityDTO.getLimitDate() : activityDTO.getEndDate());
            couponReferAlarmQDTO.setCreatorId(activityDTO.getCreatorId());
            List<String> couponIdList = GroupChatActivityRewardBaseDTO.mapConvert(activityDTO.getRewardMap()).stream()
                    .filter(x -> Objects.equals(x.getRewardType(), ChatActivityRewardEnum.COUPON.value))
                    .map(GroupChatActivityRewardBaseDTO::getRewardValue)
                    .flatMap(x -> Arrays.stream(x.split(",")))
                    .distinct().collect(Collectors.toList());
            couponReferAlarmQDTO.setCouponIdList(couponIdList);
            couponReferAlarmQDTO.setEnterpriseId(activityDTO.getEnterpriseId());
            couponReferAlarmQDTO.setReferType(3);
            couponReferAlarmQDTO.setSystemType(1);
            couponReferAlarmQDTO.setReferId(String.valueOf(activityDTO.getChatActivityId()));
            couponReferAlarmQDTO.setTitle("社群邀请有礼-" + activityDTO.getActivityName());
            couponReferAlarmQDTO.setAlarmStock(activityDTO.getExceptionNoticeNum());
            couponReferAlarmQDTO.setDelAlarmFlag(1);
            couponReferAlarmService.add(couponReferAlarmQDTO);
        }else {
            couponReferAlarmService.del(String.valueOf(activityDTO.getChatActivityId()),2);
        }

    }

}
