package com.yd.csf.service.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yd.csf.feign.request.expectedfortune.ApiExpectedFortunePageRequest;
import com.yd.csf.feign.response.expectedfortune.ApiExpectedFortunePageResponse;
import com.yd.csf.service.dto.UserGradeDto;
import com.yd.csf.service.enums.CurrencyEnum;
import com.yd.csf.service.model.ExpectedFortune;
import com.yd.csf.service.dao.ExpectedFortuneMapper;
import com.yd.csf.service.model.Policy;
import com.yd.csf.service.model.PolicyFollow;
import com.yd.csf.service.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yd.csf.service.vo.ExpectedFortuneStatisticsVO;
import com.yd.csf.service.vo.PayableReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 预计发佣表 服务实现类
 * </p>
 *
 * @author zxm
 * @since 2025-11-17
 */
@Service
@Slf4j
public class ExpectedFortuneServiceImpl extends ServiceImpl<ExpectedFortuneMapper, ExpectedFortune> implements IExpectedFortuneService {

    @Resource
    private PolicyService policyService;

    @Resource
    private PolicyFollowService policyFollowService;

    @Resource
    private IConditionConfigService iConditionConfigService;

    @Autowired
    private IAgentAccumulatedFycService iAgentAccumulatedFycService;

    @Override
    public IPage<ApiExpectedFortunePageResponse> page(Page<ApiExpectedFortunePageResponse> page,
                                                      ApiExpectedFortunePageRequest request) {
        return baseMapper.page(page, request);
    }

    @Override
    public List<ExpectedFortune> listByPolicyBizId(String policyBizId) {
        return baseMapper.listByPolicyBizId(policyBizId);
    }

    /**
     * 查询列表
     *
     * @param policyNo
     * @return
     */
    @Override
    public List<ExpectedFortune> queryList(String policyNo) {
        return baseMapper.selectList(new LambdaQueryWrapper<ExpectedFortune>().eq(ExpectedFortune::getPolicyNo, policyNo));
    }

    @Override
    public Boolean batchDelByPolicyNo(String policyNo) {
        return this.remove(new LambdaQueryWrapper<ExpectedFortune>().eq(ExpectedFortune::getPolicyNo, policyNo));
    }

    @Override
    public ExpectedFortuneStatisticsVO getStatistics(List<Long> expectedFortuneIds) {
        return baseMapper.getStatistics(expectedFortuneIds);
    }

    @Override
    public IPage<ApiExpectedFortunePageResponse> getVOPage(IPage<ApiExpectedFortunePageResponse> iPage) {
        List<ApiExpectedFortunePageResponse> expectedFortuneList = iPage.getRecords();
        Page<ApiExpectedFortunePageResponse> expectedFortuneVOPage = new Page<>(iPage.getCurrent(), iPage.getSize(), iPage.getTotal());
        if (CollUtil.isEmpty(expectedFortuneList)) {
            return expectedFortuneVOPage;
        }

        // 调整数据顺序
        processData(expectedFortuneList);

        // 1. 转介人职级信息
        List<String> brokerBizIds = expectedFortuneList.stream().map(ApiExpectedFortunePageResponse::getBrokerBizId).collect(Collectors.toList());
        Map<String, UserGradeDto> userGradeMap = iAgentAccumulatedFycService.queryUserGradeMap(brokerBizIds);

        // 2. 关联查询保单信息

        for (ApiExpectedFortunePageResponse vo : expectedFortuneList) {
            vo.setCurrencyName("港币");

//            String ratio = finalBrokerRatioMap.get(ef.getBrokerBizId());
//            vo.setCommissionRatio(ratio);

//            Policy policy = policyMap.get(vo.getPolicyNo());
//            PolicyFollow follow = policyFollowMap.get(vo.getPolicyNo());
            UserGradeDto userGradeDto = userGradeMap.get(vo.getBrokerBizId());
            if (userGradeDto != null) {
                vo.setBrokerGradeName(userGradeDto.getGradeName());
            }
//            if (policy != null) {
//                vo.setInsuranceCompany(policy.getInsuranceCompany());
//            }
//            if (follow != null) {
//                vo.setProductName(follow.getProductName());
//                vo.setProductLaunchBizId(follow.getProductLaunchBizId());
//            }
        }

        expectedFortuneVOPage.setRecords(expectedFortuneList);

        return expectedFortuneVOPage;
    }

    /**
     * 调整数据顺序   预计在前，实际在后，按预计发佣月排序
     *
     * @param expectedFortuneList 预计发佣列表
     */
    private void processData(List<ApiExpectedFortunePageResponse> expectedFortuneList) {
        if (CollUtil.isEmpty(expectedFortuneList)) {
            return;
        }

        log.info("顺序调整前: {}", expectedFortuneList.size());

        // 1. 分组
        Map<Integer, List<ApiExpectedFortunePageResponse>> groupMap = expectedFortuneList.stream()
                .collect(Collectors.groupingBy(ApiExpectedFortunePageResponse::getType));

        // 2. 获取预计发佣(type=1)和实际发佣(type=2)列表
        List<ApiExpectedFortunePageResponse> expectedList = groupMap.getOrDefault(1, new ArrayList<>());
        List<ApiExpectedFortunePageResponse> actualList = groupMap.getOrDefault(2, new ArrayList<>());

        // 3. 将 actualList 转换为 Map
        Map<String, List<ApiExpectedFortunePageResponse>> actualMap = actualList.stream()
                .collect(Collectors.groupingBy(ApiExpectedFortunePageResponse::getPayableNo));

        // 4. 调整数据顺序
        List<ApiExpectedFortunePageResponse> sortedList = new ArrayList<>();
        Set<String> matchedActualPayableNos = new HashSet<>();

        for (ApiExpectedFortunePageResponse expected : expectedList) {
            sortedList.add(expected);
            // 查找对应的实际发佣
            List<ApiExpectedFortunePageResponse> matchedList = actualMap.get(expected.getPayableNo());
            if (CollUtil.isNotEmpty(matchedList)) {
                sortedList.addAll(matchedList);
                matchedActualPayableNos.add(expected.getPayableNo());
            }
        }

        // 5. 添加未匹配的实际发佣记录
        for (ApiExpectedFortunePageResponse actual : actualList) {
            if (!matchedActualPayableNos.contains(actual.getPayableNo())) {
                sortedList.add(actual);
            }
        }

        // 6. 将排序后的结果写回原列表
        expectedFortuneList.clear();
        expectedFortuneList.addAll(sortedList);

        log.info("顺序调整后: {}", sortedList.size());
    }

    @Override
    public List<ApiExpectedFortunePageResponse> toVOList(List<ExpectedFortune> expectedFortuneList) {
        if (CollUtil.isEmpty(expectedFortuneList)) {
            return Collections.emptyList();
        }

        // 收集所有 brokerBizId
        List<String> brokerBizIds = expectedFortuneList.stream()
                .map(ExpectedFortune::getBrokerBizId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());

        Map<String, String> brokerRatioMap = new HashMap<>();
        List<UserGradeDto> userGradeDtoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(brokerBizIds)) {
            List<Map<String, Object>> ratioList = iConditionConfigService.selectBrokerRatioByBizIds(brokerBizIds);
            brokerRatioMap = ratioList.stream()
                    .filter(map -> map.get("conditional_value") != null)
                    .collect(Collectors.toMap(
                            map -> (String) map.get("brokerBizId"),
                            map -> (String) map.get("conditional_value"),
                            (v1, v2) -> v1
                    ));
            userGradeDtoList = iAgentAccumulatedFycService.queryUserGradeList(brokerBizIds);
        }
        Map<String, UserGradeDto> userGradeMap = userGradeDtoList.stream()
                .collect(Collectors.toMap(UserGradeDto::getClientUserBizId, Function.identity()));

        List<String> policyNos = expectedFortuneList.stream()
                .map(ExpectedFortune::getPolicyNo)
                .collect(Collectors.toList());
        List<Policy> policyList = policyService.lambdaQuery()
                .in(Policy::getPolicyNo, policyNos).list();
        List<PolicyFollow> policyFollowList = policyFollowService.lambdaQuery()
                .in(PolicyFollow::getPolicyNo, policyNos).list();

        Map<String, Policy> policyMap = policyList.stream()
                .collect(Collectors.toMap(Policy::getPolicyNo, Function.identity()));
        Map<String, PolicyFollow> policyFollowMap = policyFollowList.stream()
                .collect(Collectors.toMap(PolicyFollow::getPolicyNo, Function.identity()));

        // 组装 VO
        Map<String, String> finalBrokerRatioMap = brokerRatioMap;
        List<ApiExpectedFortunePageResponse> voList = expectedFortuneList.stream().map(ef -> {
            ApiExpectedFortunePageResponse vo = new ApiExpectedFortunePageResponse();
            BeanUtils.copyProperties(ef, vo);

            vo.setCurrencyName("港币");

            String ratio = finalBrokerRatioMap.get(ef.getBrokerBizId());
            vo.setCommissionRatio(ratio);

            Policy policy = policyMap.get(ef.getPolicyNo());
            PolicyFollow follow = policyFollowMap.get(ef.getPolicyNo());
            UserGradeDto userGradeDto = userGradeMap.get(ef.getBrokerBizId());
            if (userGradeDto != null) {
                vo.setBrokerGradeName(userGradeDto.getGradeName());
            }
            if (policy != null) {
                vo.setInsuranceCompany(policy.getInsuranceCompany());
                vo.setPremium(policy.getPaymentPremium());
            }
            if (follow != null) {
                vo.setProductName(follow.getProductName());
                vo.setProductLaunchBizId(follow.getProductLaunchBizId());
            }
            return vo;
        }).collect(Collectors.toList());

        return voList;
    }

    @Override
    public IPage<PayableReportVO> payableReportPage(Page<PayableReportVO> page, List<Long> expectedFortuneIds) {
        return baseMapper.payableReportPage(page, expectedFortuneIds);
    }

    @Override
    public void updateBatchByBizId(List<String> expectedFortuneBizIdList, String status) {
        baseMapper.updateBatchByBizId(expectedFortuneBizIdList, status);
    }

    @Override
    public ExpectedFortune getByBizId(String expectedFortuneBizId) {
        return this.getOne(new QueryWrapper<ExpectedFortune>().eq("expected_fortune_biz_id", expectedFortuneBizId));
    }

    @Override
    public Integer getPayableNoCurrentSeq() {

        ExpectedFortune latest = this.getOne(
                new QueryWrapper<ExpectedFortune>()
                        .isNotNull("payable_no")
                        .eq("is_part", 0)
                        .orderByDesc("id")
                        .last("LIMIT 1"),
                true
        );
        //获取当前序号作为起点
        int currentSeq = 0;
        if (!Objects.isNull(latest)) {
            String payableNo = latest.getPayableNo();
            currentSeq = Integer.parseInt(payableNo.substring(payableNo.length() - 6));
        }

        return currentSeq;
    }

    @Override
    public String getPayableNo(String fortuneBizType, LocalDate payoutDate, String policyNo, Integer fortunePeriod) {
        // 1. 安全获取保单号后4位
        String last4Chars = "";
        if (policyNo != null && !policyNo.isEmpty()) {
            int startIdx = Math.max(0, policyNo.length() - 4);
            last4Chars = policyNo.substring(startIdx);
        }

        // 2. 字符串左补0至4位
        // %4s 表示最小宽度为4，不足部分默认用空格填充在左侧
        // replace(' ', '0') 将填充的空格替换为0，实现左补0效果
        String formattedPolicySuffix = String.format("%4s", last4Chars).replace(' ', '0');

        // 3. 组装最终字符串
        return String.format("%s%04d%02d%s%03d",
                fortuneBizType,
                payoutDate.getYear(),
                payoutDate.getMonthValue(),
                formattedPolicySuffix,
                fortunePeriod);
    }

    @Override
    public IPage<ApiExpectedFortunePageResponse> queryListNew(Integer pageNo, Integer pageSize, QueryWrapper<ExpectedFortune> queryWrapper) {
        // 1. 查询列表数据（手动分页）
        List<ApiExpectedFortunePageResponse> records = this.baseMapper.queryListNew(pageNo, pageSize, queryWrapper);

        // 2. 查询总记录数
        Long total = this.baseMapper.queryListNewCount(queryWrapper);
        if (total == null) {
            total = 0L;
        }

        // 3. 手动构建分页对象
        Page<ApiExpectedFortunePageResponse> page = new Page<>(pageNo, pageSize, total);
        page.setRecords(records);

        return page;
    }

    @Override
    public ExpectedFortuneStatisticsVO queryListStatistics(QueryWrapper<ExpectedFortune> queryWrapper) {
        ExpectedFortuneStatisticsVO statistics = this.baseMapper.queryListStatistics(queryWrapper);

        if (statistics == null) {
            return new ExpectedFortuneStatisticsVO();
        }

        return statistics;
    }
}
