package com.hzs.activity.declaration.service.impl;

import com.hzs.activity.declaration.mapper.AcDeclarationGiftRecordMapper;
import com.hzs.activity.declaration.param.AcDeclarationListParam;
import com.hzs.activity.declaration.param.AcDeclarationListTotalParam;
import com.hzs.activity.declaration.param.AcDeclarationProductParam;
import com.hzs.activity.declaration.service.IAcDeclarationGiftDetailService;
import com.hzs.activity.declaration.service.IAcDeclarationGiftRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.activity.declaration.vo.AcDeclarationTotalListVO;
import com.hzs.common.domain.activity.declaration.AcDeclarationGiftRecord;
import com.hzs.activity.declaration.vo.AcDeclarationListVO;
import com.hzs.common.core.enums.ERegistrationAuthority;
import com.hzs.common.domain.activity.base.ext.AcGiftConfigExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 报单赠品明细表 服务实现类
 * </p>
 *
 * @author bd
 * @since 2025-10-13
 */
@Service
public class AcDeclarationGiftRecordServiceImpl extends ServiceImpl<AcDeclarationGiftRecordMapper, AcDeclarationGiftRecord> implements IAcDeclarationGiftRecordService {

    @Autowired
    private IAcDeclarationGiftDetailService iAcDeclarationGiftDetailService;



    @Override
    public List<AcDeclarationListVO> selectDeclarationList(AcDeclarationListParam param) {
        // 一次性查询所有主记录
        List<AcDeclarationListVO> voList = baseMapper.selectDeclarationList(param);
        if (voList.isEmpty()) {
            return Collections.emptyList();
        }

        // 提取所有规则主键
        List<Long> ruleIds = voList.stream()
                .map(AcDeclarationListVO::getPkRuleId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());

        // 批量查询明细
        List<AcDeclarationProductParam> allDetails = iAcDeclarationGiftDetailService.listByPKRuleIds(ruleIds);

        // 按规则ID分组
        Map<Long, List<AcDeclarationProductParam>> detailMap = allDetails.stream()
                .collect(Collectors.groupingBy(AcDeclarationProductParam::getPkRuleId));

        // 组装数据
        voList.forEach(item -> {
            // 绑定明细列表
            List<AcDeclarationProductParam> productList = detailMap.getOrDefault(item.getPkRuleId(), new ArrayList<>());
            item.setAcDeclarationProductParamList(productList);

            // 报单权限转义
            item.setRegisterAuthorityVal(
                    Optional.ofNullable(ERegistrationAuthority.getEnumByValue(item.getRegisterAuthority()))
                            .map(ERegistrationAuthority::getLabel)
                            .orElse("未知")
            );

            // 拼接产品信息字符串
            if (!productList.isEmpty()) {
                String giftInfo = productList.stream()
                        .map(p -> String.format("%s(%s) x%d",
                                p.getProductName(),
                                p.getSpecsName(),
                                p.getQuantity()))
                        .collect(Collectors.joining("；"));
                item.setGiftProductInfo(giftInfo);
            } else {
                item.setGiftProductInfo("无赠送产品");
            }
        });

        return voList;
    }

    @Override
    public AcDeclarationGiftRecord selectLastRecord() {
        return baseMapper.selectLastRecord();
    }

    @Override
    public List<AcGiftConfigExt> queryListByRuleIdList(List<Long> pkDataId, Integer pkCountry) {
        return baseMapper.queryListByRuleList(pkDataId,pkCountry);
    }

    @Override
    public List<AcDeclarationTotalListVO> selectDeclarationListTotal(AcDeclarationListTotalParam param) {
        return baseMapper.selectDeclarationListTotal(param);
    }

}
