package com.yd.csf.service.service.impl;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yd.auth.core.dto.AuthUserDto;
import com.yd.auth.core.utils.SecurityUtil;
import com.yd.common.enums.ResultCode;
import com.yd.common.exception.BusinessException;
import com.yd.common.utils.RandomStringGenerator;
import com.yd.csf.service.dto.*;
import com.yd.csf.service.enums.FortuneAccountStatusEnum;
import com.yd.csf.service.enums.FortuneStatusEnum;
import com.yd.csf.service.model.*;
import com.yd.csf.service.service.*;
import com.yd.csf.service.dao.FortuneAccountMapper;
import com.yd.csf.service.utils.BusinessNoUtils;
import com.yd.csf.service.vo.FortuneAccountVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author Zhang Jianan
* @description 针对表【fortune_account(出账记录表)】的数据库操作Service实现
* @createDate 2025-09-23 15:45:32
*/
@Service
public class FortuneAccountServiceImpl extends ServiceImpl<FortuneAccountMapper, FortuneAccount>
    implements FortuneAccountService{

    @Resource
    private FortuneService fortuneService;

    @Resource
    private FortuneAccountEditRecordService fortuneAccountEditRecordService;

    @Resource
    private IExpectedFortuneService expectedFortuneService;

    @Resource
    private PolicyBrokerService policyBrokerService;

    private final Gson GSON = new Gson();

    @Override
    public QueryWrapper<FortuneAccount> getQueryWrapper(FortuneAccountQueryRequest fortuneAccountQueryRequest) {
        QueryWrapper<FortuneAccount> queryWrapper = new QueryWrapper<>();
        if (fortuneAccountQueryRequest == null) {
            return queryWrapper;
        }
        List<String> statusList = fortuneAccountQueryRequest.getStatusList();
        if (CollectionUtils.isEmpty(statusList)) {
            statusList = Collections.singletonList("6");
        }
        List<String> brokerBizIdList = fortuneAccountQueryRequest.getBrokerBizIdList();
        List<String> teamBizIdList = fortuneAccountQueryRequest.getTeamBizIdList();
        queryWrapper.in(CollectionUtils.isNotEmpty(brokerBizIdList), "broker_biz_id", brokerBizIdList);
        queryWrapper.in(CollectionUtils.isNotEmpty(teamBizIdList), "team_biz_id", teamBizIdList);

        queryWrapper.eq(StringUtils.isNotBlank(fortuneAccountQueryRequest.getBroker()), "broker", fortuneAccountQueryRequest.getBroker());
        queryWrapper.eq(StringUtils.isNotBlank(fortuneAccountQueryRequest.getTeam()), "team", fortuneAccountQueryRequest.getTeam());
        queryWrapper.in(CollectionUtils.isNotEmpty(statusList), "status", statusList);
        queryWrapper.ge(fortuneAccountQueryRequest.getAccountDateStart() != null, "fortune_account_date", fortuneAccountQueryRequest.getAccountDateStart());
        queryWrapper.le(fortuneAccountQueryRequest.getAccountDateEnd() != null, "fortune_account_date", fortuneAccountQueryRequest.getAccountDateEnd());

        // 默认排序
        queryWrapper.orderByDesc("fortune_account_date");
        return queryWrapper;
    }

    @Override
    public Page<FortuneAccountVO> getFortuneAccountVOPage(Page<FortuneAccount> fortunePage) {
        List<FortuneAccount> fortuneAccountList = fortunePage.getRecords();
        Page<FortuneAccountVO> fortuneAccountVOPage = new Page<>(fortunePage.getCurrent(), fortunePage.getSize(), fortunePage.getTotal());

        if (CollectionUtils.isEmpty(fortuneAccountList)) {
            return fortuneAccountVOPage;
        }

        List<FortuneAccountVO> fortuneAccountVOList = new ArrayList<>();
        for (FortuneAccount fortuneAccount : fortuneAccountList) {
            FortuneAccountVO fortuneAccountVO = FortuneAccountVO.objToVo(fortuneAccount);
            fortuneAccountVO.setFortuneList(GSON.fromJson(fortuneAccount.getContent(), new TypeToken<List<Map<String, Object>>>(){}.getType()));
            fortuneAccountVO.setFortuneAccountMonth(fortuneAccount.getFortuneAccountDate());
            fortuneAccountVOList.add(fortuneAccountVO);
        }

        fortuneAccountVOPage.setRecords(fortuneAccountVOList);
        return fortuneAccountVOPage;
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void saveFortuneAccount(List<FortuneAccountExportDTO> accountExportDTOList, LocalDate actualPayoutDate) {
        if (CollectionUtils.isEmpty(accountExportDTOList)) {
            return;
        }
        // 当前登录用户
        AuthUserDto currentLoginUser = SecurityUtil.getCurrentLoginUser();
        Long loginUserId = currentLoginUser.getId();

        //查询转介人的内部编号列表信息
        List<String> brokerBizIdList = accountExportDTOList.stream().map(FortuneAccountExportDTO::getBrokerBizId).collect(Collectors.toList());
        Map<String, String> map = policyBrokerService.queryInternalCodeMap(brokerBizIdList);
        List<FortuneAccount> fortuneAccountList = new ArrayList<>();
        Date currentDate = new Date();
        String billingYearMonth = actualPayoutDate != null
                ? actualPayoutDate.format(java.time.format.DateTimeFormatter.ofPattern("yyyyMM"))
                : "";
        for (FortuneAccountExportDTO accountExportDTO : accountExportDTOList) {
            FortuneAccount fortuneAccount = new FortuneAccount();
            fortuneAccount.setFortuneAccountBizId(RandomStringGenerator.generateBizId16("fortune_account"));
            String internalCode = map.get(accountExportDTO.getBrokerBizId());
//            LocalDate payoutDate = accountExportDTO.getActualPayoutDate();
//            String billingYearMonth = payoutDate != null
//                    ? actualPayoutDate.format(java.time.format.DateTimeFormatter.ofPattern("yyyyMM"))
//                    : "";
            String businessNo = BusinessNoUtils.generate(billingYearMonth, internalCode);
            //出账业务编号（出账年月 + 转介人内部编号（后6位）（如无，用0补齐）+流水号（6位））
            fortuneAccount.setBusinessNo(businessNo);
            fortuneAccount.setBroker(accountExportDTO.getBroker());
            fortuneAccount.setBrokerBizId(accountExportDTO.getBrokerBizId());
            fortuneAccount.setTeam(accountExportDTO.getTeam());
            fortuneAccount.setTeamBizId(accountExportDTO.getTeamBizId());
            fortuneAccount.setCurrency(accountExportDTO.getCurrency());
            fortuneAccount.setHkdAmount(accountExportDTO.getAmount());
            fortuneAccount.setFortuneAccountDate(this.getActualPayoutDate(actualPayoutDate));
            // 出账状态默认待出账
            fortuneAccount.setStatus(FortuneStatusEnum.CHECKED.getItemValue());

            // 转换为List<Map<String, Object>>
            Map<String, BigDecimal> fortuneAmounts = accountExportDTO.getFortuneAmounts();
            List<Map<String, Object>> fortuneList = new ArrayList<>();
            for (Map.Entry<String, BigDecimal> entry : fortuneAmounts.entrySet()) {
                Map<String, Object> fortune = new HashMap<>();
                fortune.put("fortuneName", entry.getKey());
                fortune.put("amount", entry.getValue());
                fortuneList.add(fortune);
            }
            fortuneAccount.setContent(GSON.toJson(fortuneList));
            fortuneAccount.setFortuneList(accountExportDTO.getFortuneList());
            fortuneAccount.setCreatorId(loginUserId.toString());
            fortuneAccount.setUpdaterId(loginUserId.toString());
            fortuneAccount.setCreateTime(currentDate);
            fortuneAccount.setUpdateTime(currentDate);

            fortuneAccountList.add(fortuneAccount);
        }
        // 保存出账记录
        this.saveBatch(fortuneAccountList);

        // 处理关联发佣记录
        List<Fortune> updateFortuneList = new ArrayList<>();
        // 处理关联预计发佣记录
        List<String> expectedFortuneBizIdList = new ArrayList<>();
        for (FortuneAccount fortuneAccount : fortuneAccountList) {
            String fortuneAccountBizId = fortuneAccount.getFortuneAccountBizId();
            List<Fortune> fortuneList = fortuneAccount.getFortuneList();
            if (CollectionUtils.isNotEmpty(fortuneList)) {
                for (Fortune item : fortuneList) {
                    Fortune updateFortune = new Fortune();
                    updateFortune.setId(item.getId());
                    updateFortune.setFortuneAccountBizId(fortuneAccountBizId);
                    updateFortune.setStatus(FortuneStatusEnum.CHECKED.getItemValue());
                    // 处理金额字段
                    if (BigDecimal.ZERO.compareTo(item.getHkdAmount()) != 0) {
                        updateFortune.setCurrentPaymentHkdAmount(item.getCurrentPaymentHkdAmount());
                        updateFortune.setCurrentPaymentRatio(item.getCurrentPaymentHkdAmount()
                                .divide(item.getHkdAmount(), 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));
                    }
                    updateFortune.setReconciliationOperator(currentLoginUser.getUsername());
                    updateFortuneList.add(updateFortune);
                    // 处理关联预计发佣记录
                    expectedFortuneBizIdList.add(item.getExpectedFortuneBizId());
                }
            }
        }

        // 更新发佣记录的出账记录id
        if (CollectionUtils.isNotEmpty(updateFortuneList)) {
            fortuneService.updateBatchById(updateFortuneList);
        }
        // 更新预计发佣记录的出账状态
        if (CollectionUtils.isNotEmpty(expectedFortuneBizIdList)) {

            // 查询预计发佣记录
            List<ExpectedFortune> expectedFortuneList = expectedFortuneService.list(new QueryWrapper<ExpectedFortune>()
                    .in("expected_fortune_biz_id", expectedFortuneBizIdList));

            List<ExpectedFortune> updateExpectedFortuneList = new ArrayList<>();
            for (ExpectedFortune expectedFortune : expectedFortuneList) {
                ExpectedFortune updateExpectedFortune = new ExpectedFortune();
                updateExpectedFortune.setId(expectedFortune.getId());
                updateExpectedFortune.setStatus(FortuneStatusEnum.CHECKED.getItemValue());
                updateExpectedFortune.setActualPayoutDate(actualPayoutDate);
                updateExpectedFortuneList.add(updateExpectedFortune);
            }
            expectedFortuneService.updateBatchById(updateExpectedFortuneList);
        }

    }

    private Date getActualPayoutDate(LocalDate actualPayoutDate) {
        if (actualPayoutDate == null) {
            return null;
        }
        return Date.from(actualPayoutDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    @Override
    public FortuneAccount getByFortuneAccountBizId(String fortuneAccountBizId) {
        return this.getOne(new QueryWrapper<FortuneAccount>().eq("fortune_account_biz_id", fortuneAccountBizId));
    }

    @Override
    public Map<String, Object> addFortuneAccount(FortuneAccountAddRequest fortuneAccountAddRequest) {
        // 当前登录用户
        AuthUserDto currentLoginUser = SecurityUtil.getCurrentLoginUser();
        Long loginUserId = currentLoginUser.getId();

        FortuneAccount fortuneAccount = new FortuneAccount();
        BeanUtils.copyProperties(fortuneAccountAddRequest, fortuneAccount);

        fortuneAccount.setFortuneAccountBizId(RandomStringGenerator.generateBizId16("fortune_account"));
        fortuneAccount.setRemark(fortuneAccountAddRequest.getRemark());
        fortuneAccount.setCreatorId(loginUserId.toString());
        fortuneAccount.setUpdaterId(loginUserId.toString());
        fortuneAccount.setCreateTime(new Date());
        fortuneAccount.setUpdateTime(new Date());

        this.save(fortuneAccount);
        return Collections.singletonMap("fortuneAccountBizId", fortuneAccount.getFortuneAccountBizId());
    }

    @Override
    public Boolean updateFortuneAccount(FortuneAccountUpdateRequest fortuneAccountUpdateRequest) {
        // 当前登录用户
        AuthUserDto currentLoginUser = SecurityUtil.getCurrentLoginUser();
        Long loginUserId = currentLoginUser.getId();

        String fortuneAccountBizId = fortuneAccountUpdateRequest.getFortuneAccountBizId();

        FortuneAccount fortuneAccount = this.getByFortuneAccountBizId(fortuneAccountBizId);
        if (fortuneAccount == null) {
            throw new BusinessException(ResultCode.NULL_ERROR.getCode(), ResultCode.NULL_ERROR.getMessage());
        }
        // 保存修改记录
        saveUpdateFortuneAccountRecord(fortuneAccount, fortuneAccountUpdateRequest);

        BeanUtils.copyProperties(fortuneAccountUpdateRequest, fortuneAccount, "fortuneAccountBizId");
        fortuneAccount.setUpdaterId(loginUserId.toString());
        fortuneAccount.setUpdateTime(new Date());

        this.updateById(fortuneAccount);

//        // 更新关联 expected_fortune、fortune的出账日期
//        if (fortuneAccountUpdateRequest.getFortuneAccountDate() != null) {
//            List<Fortune> fortuneList = fortuneService.lambdaQuery().eq(Fortune::getFortuneAccountBizId, fortuneAccountBizId).list();
//            if (CollectionUtils.isNotEmpty(fortuneList)) {
//                Set<String> expectedBizIdSet = fortuneList.stream().map(Fortune::getExpectedFortuneBizId).collect(Collectors.toSet());
//                // 查询 expected_fortune 列表
//                List<ExpectedFortune> expectedList = expectedFortuneService.lambdaQuery()
//                        .in(ExpectedFortune::getExpectedFortuneBizId, expectedBizIdSet)
//                        .list();
//
//            }
//        }
        return true;
    }

    private void saveUpdateFortuneAccountRecord(FortuneAccount fortuneAccount, FortuneAccountUpdateRequest fortuneAccountUpdateRequest) {
        FortuneAccountEditRecord fortuneAccountEditRecord = new FortuneAccountEditRecord();
        // 薪资记录业务id
        fortuneAccountEditRecord.setFortuneAccountBizId(fortuneAccount.getFortuneAccountBizId());
        fortuneAccountEditRecord.setIsDeleted(0);
        fortuneAccountEditRecord.setCreateTime(new Date());
        // 当前登录用户
        AuthUserDto currentLoginUser = SecurityUtil.getCurrentLoginUser();
        Long loginUserId = currentLoginUser.getId();

        fortuneAccountEditRecord.setUserBizId(loginUserId.toString());
        fortuneAccountEditRecord.setUserName(currentLoginUser.getUsername());

        // 构建修改内容
        StringBuilder editContentBuilder = new StringBuilder();
        editContentBuilder.append("修改了薪资记录:");
        editContentBuilder.append("转介人:").append(fortuneAccountUpdateRequest.getBroker()).append(";");
        editContentBuilder.append("团队:").append(fortuneAccountUpdateRequest.getTeam()).append(";");
        editContentBuilder.append("出账金额:").append(fortuneAccountUpdateRequest.getAmount()).append(";");
        editContentBuilder.append("出账币种:").append(fortuneAccountUpdateRequest.getCurrency()).append(";");
        if (StringUtils.isNotBlank(fortuneAccountUpdateRequest.getRemark())) {
            editContentBuilder.append("备注:").append(fortuneAccountUpdateRequest.getRemark()).append(";");
        }

        fortuneAccountEditRecord.setEditContent(editContentBuilder.toString());
        fortuneAccountEditRecord.setRemark(fortuneAccountUpdateRequest.getRemark());

        fortuneAccountEditRecordService.save(fortuneAccountEditRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean completeFortuneAccount(CompleteFortuneAccountRequest req) {
        /* 1. 参数校验 & 查询 --------------------------------------------------*/
        List<String> accountBizIds = req.getFortuneAccountBizIdList();
        if (CollectionUtils.isEmpty(accountBizIds)) {
            throw new BusinessException("出账清单不能为空");
        }
        List<FortuneAccount> accountList = this.lambdaQuery()
                .in(FortuneAccount::getFortuneAccountBizId, accountBizIds)
                .list();

        Long loginUserId = SecurityUtil.getCurrentLoginUser().getId();

        /* 2. 更新 FortuneAccount 状态为已出账 ----------------------------------*/
        accountList.forEach(a -> {
            a.setStatus(FortuneStatusEnum.SENT.getItemValue());
            a.setUpdaterId(loginUserId.toString());
            a.setUpdateTime(new Date());
        });
        this.updateBatchById(accountList);

        /* 3. 取出关联的所有 fortune 并一次性更新为已出账 ------------------------*/
        List<String> accountBizIdList = accountList.stream()
                .map(FortuneAccount::getFortuneAccountBizId).collect(Collectors.toList());

        List<Fortune> fortunes = fortuneService.lambdaQuery()
                .in(Fortune::getFortuneAccountBizId, accountBizIdList)
                .eq(Fortune::getStatus, FortuneStatusEnum.CHECKED.getItemValue())
                .list();
        if (CollectionUtils.isNotEmpty(fortunes)) {
            fortunes.forEach(f -> {
                if (f.getCurrentPaymentHkdAmount() == null) {
                    f.setCurrentPaymentHkdAmount(f.getHkdAmount());
                    f.setCurrentPaymentRatio(BigDecimal.valueOf(100));
                }
                f.setStatus(FortuneStatusEnum.SENT.getItemValue());
//                f.setActualPayoutDate(new Date());
                f.setUpdaterId(loginUserId.toString());
                f.setUpdateTime(new Date());
            });
            fortuneService.updateBatchById(fortunes);
        }

        /* 4. 按 expected_fortune 维度重新汇总已出账金额 --------------------------*/
        // 4.1 收集所有涉及的 expectedFortuneBizId
        Set<String> expectedIds = fortunes.stream()
                .map(Fortune::getExpectedFortuneBizId)
                .collect(Collectors.toSet());
        if (CollectionUtils.isEmpty(expectedIds)) {
            return true; // 没有对应预计出账，直接结束
        }

        // 4.2 先锁再算
        List<ExpectedFortune> expectedList = expectedFortuneService.lambdaQuery()
                .in(ExpectedFortune::getExpectedFortuneBizId, expectedIds)
                .last("FOR UPDATE")
                .list();

        // 4.3 全量汇总：把同一 expected_fortune 下所有 fortune 的 current_payment_hkd_amount 求和
        Map<String, BigDecimal> paidMap = fortuneService.lambdaQuery()
                .in(Fortune::getExpectedFortuneBizId, expectedIds)
                .eq(Fortune::getStatus, FortuneStatusEnum.SENT.getItemValue())
                .list()
                .stream()
                .collect(Collectors.groupingBy(Fortune::getExpectedFortuneBizId,
                        Collectors.mapping(Fortune::getCurrentPaymentHkdAmount,
                                Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));

        // 4.4 反写 expected_fortune
        expectedList.forEach(ef -> {
            BigDecimal totalPaid = paidMap.getOrDefault(ef.getExpectedFortuneBizId(), BigDecimal.ZERO);
            BigDecimal totalAmount = ef.getHkdAmount();
            BigDecimal unpaid = totalAmount.subtract(totalPaid);

            String newStatus;
            if (unpaid.compareTo(BigDecimal.ZERO) == 0) {
                newStatus = FortuneStatusEnum.SENT.getItemValue();        // 全部完成
            } else if (totalPaid.compareTo(BigDecimal.ZERO) > 0) {
                newStatus = FortuneStatusEnum.PARTIAL_SENT.getItemValue(); // 部分完成
            } else {
                newStatus = ef.getStatus();                               // 无变化
            }

            expectedFortuneService.lambdaUpdate()
                    .set(ExpectedFortune::getPaidAmount, totalPaid)
                    .set(ExpectedFortune::getUnpaidAmount, unpaid)
                    .set(ExpectedFortune::getPaidRatio,
                            totalPaid.divide(totalAmount, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)))
                    .set(ExpectedFortune::getUnpaidRatio,
                            unpaid.divide(totalAmount, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)))
                    .set(ExpectedFortune::getStatus, newStatus)
//                    .set(ExpectedFortune::getActualPayoutDate, new Date())
                    .set(ExpectedFortune::getUpdaterId, loginUserId.toString())
                    .set(ExpectedFortune::getUpdateTime, LocalDateTime.now())
                    .eq(ExpectedFortune::getId, ef.getId())
                    .update();
        });

        return true;
    }

    @Override
    public FortuneAccountStatisticsVO getFortuneAccountStatistics(List<Long> fortuneAccountIdList) {
        if (CollectionUtils.isEmpty(fortuneAccountIdList)) {
            FortuneAccountStatisticsVO vo = new FortuneAccountStatisticsVO();
            vo.setTotalAmount(BigDecimal.ZERO);
            vo.setBrokerCount(0L);
            return vo;
        }

        // 统计总金额和总人数
        List<FortuneAccount> fortuneAccountList = this.baseMapper.selectBatchIds(fortuneAccountIdList);

        BigDecimal totalAmount = fortuneAccountList.stream()
                .map(FortuneAccount::getHkdAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        int brokerCount = fortuneAccountList.stream()
                .map(FortuneAccount::getBroker)
                .collect(Collectors.toSet()).size();

        FortuneAccountStatisticsVO vo = new FortuneAccountStatisticsVO();
        vo.setTotalAmount(totalAmount);
        vo.setBrokerCount(brokerCount);
        return vo;
    }

    /**
     * 查询单个对象
     * @param fortuneAccountBizId
     * @return
     */
    @Override
    public FortuneAccount queryOne(String fortuneAccountBizId) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<FortuneAccount>()
                .eq(FortuneAccount::getFortuneAccountBizId,fortuneAccountBizId)
                .last(" limit 1 ")
        );
    }
}




