package com.kxs.stat.biz.service.impl;

import cn.hutool.core.date.DatePattern;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.constant.CommonConstants;
import com.kxs.common.core.util.AssertUtil;
import com.kxs.common.core.util.R;
import com.kxs.stat.api.model.KxsMerchantTrade;
import com.kxs.stat.api.vo.statUser.MchTradeDetailVO;
import com.kxs.stat.biz.mapper.KxsMerchantTradeMapper;
import com.kxs.stat.biz.service.KxsMerchantTradeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 商户交易统计总表(KxsMerchantTrade)表服务实现类
 *
 * @author 系统
 * @since 2024-05-07 17:25:06
 */
@Service("kxsMerchantTradeService")
@RequiredArgsConstructor
@Slf4j
public class KxsMerchantTradeServiceImpl extends ServiceImpl<KxsMerchantTradeMapper, KxsMerchantTrade> implements KxsMerchantTradeService {


    private final KxsMerchantTradeMapper kxsMerchantTradeMapper;


    @Override
    public R merchantTradeDetail(Long mchId, String month) {
        AssertUtil.isyyyyMM(month);
        String tableName = CommonConstants.MCH_TABLE_PREFIX + month;

        LocalDate now = LocalDate.now();
        List<Integer> times = new ArrayList<>();
        //判断month是否是本月  否则获取月份中全部日日期
        int year = Integer.parseInt(month.substring(0, 4));
        int monthOfYear = Integer.parseInt(month.substring(4, 6));
        // 判断给定的月份字符串是否表示当前月份
        boolean isCurrentMonth = year == now.getYear() && monthOfYear == now.getMonthValue();
        // 如果是当前月份，则生成本月到今天的日期字符串
        if (isCurrentMonth) {
            LocalDate firstDayOfMonth = now.withDayOfMonth(1);
            for (LocalDate date = firstDayOfMonth; !date.isAfter(now); date = date.plusDays(1)) {
                times.add(Integer.valueOf(date.format(DatePattern.PURE_DATE_FORMATTER)));
            }
        } else {
            // 如果不是当前月份，则生成整个月的日期字符串
            LocalDate firstDayOfMonth = LocalDate.of(year, monthOfYear, 1);
            int daysInMonth = firstDayOfMonth.lengthOfMonth();
            for (int day = 1; day <= daysInMonth; day++) {
                LocalDate date = firstDayOfMonth.withDayOfMonth(day);
                times.add(Integer.valueOf(date.format(DatePattern.PURE_DATE_FORMATTER)));
            }
        }
        Collections.reverse(times);

        BigDecimal totalAmt = BigDecimal.ZERO;
        List<KxsMerchantTrade> mchTrades = kxsMerchantTradeMapper.mchTradeInListOfDay(mchId, times, tableName);
        //使用交易日作为map的key
        Map<Integer, KxsMerchantTrade> tradeMap = mchTrades.stream().collect(Collectors.toMap(KxsMerchantTrade::getTradeDate, Function.identity()));

        List<Map<String, Object>> tradeDays = new ArrayList<>();
        for (Integer time : times) {
            BigDecimal tradeAmt = BigDecimal.ZERO;
            KxsMerchantTrade mchTrade = tradeMap.get(time);
            if(mchTrade != null){
                tradeAmt = mchTrade.getTradeAmt();
            }
            HashMap<String, Object> amtMap = new HashMap<>();
            amtMap.put("time",time);
            amtMap.put("amt",tradeAmt);
            tradeDays.add(amtMap);
            totalAmt = totalAmt.add(tradeAmt);
        }
        MchTradeDetailVO mchTradeVo = MchTradeDetailVO.builder().tradeAmt(tradeDays)
                .tradeAmtMonth(totalAmt)
                .tradeMonth(month)
                .merchantId(mchId).build();

        return R.ok(mchTradeVo);
    }

    @Override
    public R loadByMchTotalTrade(Long mchId) {

        return null;
    }


}

