package com.jeesharp.business.modules.setting.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.setting.entity.MarketingActivity;
import com.jeesharp.business.modules.setting.mapper.MarketingActivityMapper;
import com.jeesharp.business.modules.setting.service.MarketingActivityService;
import com.jeesharp.business.modules.setting.vo.MarketingActivityQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 活动营销主配置领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-04
 */
@Service
@Transactional(readOnly = true)
public class MarketingActivityServiceImpl extends CrudService<MarketingActivityMapper, MarketingActivity, MarketingActivityQueryVO>
        implements MarketingActivityService {

    @Override
    @Transactional(readOnly = false)
    public void createMarketingActivity(MarketingActivity entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateMarketingActivity(MarketingActivity entity) {
        update(entity);
    }

    @Override
    public List<String> findIdsByNameLike(String keyword) {
        if (StrUtil.isBlank(keyword)) {
            return List.of();
        }
        String trimmed = keyword.trim();
        LambdaQueryWrapper<MarketingActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MarketingActivity::getDelFlag, "0");
        wrapper.and(w -> w.like(MarketingActivity::getActivityName, trimmed)
            .or().like(MarketingActivity::getActivityCode, trimmed));
        return getBaseMapper().selectList(wrapper).stream()
            .map(MarketingActivity::getId)
            .filter(StrUtil::isNotBlank)
            .distinct()
            .collect(Collectors.toList());
    }

    @Override
    public Map<String, String> mapActivityNameByIds(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyMap();
        }
        List<String> distinctIds = ids.stream().filter(StrUtil::isNotBlank).distinct().toList();
        if (distinctIds.isEmpty()) {
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<MarketingActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MarketingActivity::getDelFlag, "0");
        wrapper.in(MarketingActivity::getId, distinctIds);
        Map<String, String> map = new HashMap<>();
        for (MarketingActivity activity : getBaseMapper().selectList(wrapper)) {
            if (activity == null || StrUtil.isBlank(activity.getId())) {
                continue;
            }
            map.put(activity.getId(), resolveActivityLabel(activity));
        }
        return map;
    }

    @Override
    public List<MarketingActivity> listEnabledForMobile(String scene, String activityType, String status) {
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapper<MarketingActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MarketingActivity::getDelFlag, "0");
        wrapper.eq(status != null && !status.isBlank(), MarketingActivity::getStatus, status);
        wrapper.eq(activityType != null && !activityType.isBlank(), MarketingActivity::getActivityType, activityType);
        wrapper.eq(scene != null && !scene.isBlank(), MarketingActivity::getActivityScene, scene);
        wrapper.and(q -> q.isNull(MarketingActivity::getStartTime).or().le(MarketingActivity::getStartTime, now));
        wrapper.and(q -> q.isNull(MarketingActivity::getEndTime).or().ge(MarketingActivity::getEndTime, now));
        wrapper.orderByAsc(MarketingActivity::getSortNo).orderByDesc(MarketingActivity::getCreateDate);
        return list(wrapper);
    }

    private static String resolveActivityLabel(MarketingActivity activity) {
        if (StrUtil.isNotBlank(activity.getActivityName())) {
            return activity.getActivityName();
        }
        return StrUtil.blankToDefault(activity.getActivityCode(), activity.getId());
    }
}
