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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.EnumUtil;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.constant.enums.KindTypeEnum;
import com.kxs.common.mybatis.handler.MonthTableNameHandler;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.user.api.dto.kxsapp.userAccount.UserAmountVariationListDTO;
import com.kxs.user.api.vo.kxsapp.userAccount.AmountInExpendTotalVO;
import com.kxs.user.api.vo.kxsapp.userAccount.UserAmountVariationListVO;
import com.kxs.common.core.constant.enums.VariationTypeEnum;
import com.kxs.user.api.vo.kxsapp.userAccount.VariationTypeListVO;
import com.kxs.user.biz.mapper.KxsUserAmountLogMapper;
import com.kxs.user.api.model.KxsUserAmountLog;
import com.kxs.user.biz.service.KxsUserAmountLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户余额记录日志表(KxsUserAmountLog)表服务实现类
 *
 * @author 系统
 * @since 2024-01-03 09:39:56
 */
@Service("kxsUserAmountLogService")
public class KxsUserAmountLogServiceImpl extends ServiceImpl<KxsUserAmountLogMapper, KxsUserAmountLog> implements KxsUserAmountLogService {

    @Override
    public IPage<UserAmountVariationListVO> getByPage(Page<UserAmountVariationListVO> page, UserAmountVariationListDTO param) {

        param.setUserId(SecurityUtils.getUser().getId());
        //拼接表名
        String tableName = param.getTradeMonth();
        String[] split = tableName.split("-");
        tableName = "kxs_user_amount_log_" + split[0] + split[1];
        return baseMapper.getByPage(page, param,tableName);
    }

    @Override
    public AmountInExpendTotalVO amountInExpendTotal(String tradeMonth) {
        AmountInExpendTotalVO amountInExpendTotalVO = new AmountInExpendTotalVO();


        QueryWrapper<KxsUserAmountLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", SecurityUtils.getUser().getId());
        queryWrapper.eq("DATE_FORMAT(create_time, '%Y-%m')", tradeMonth);
        queryWrapper.eq("kind", KindTypeEnum.ADD.getType());
        queryWrapper.select("IFNULL(SUM(amount),0) AS amount");

        //收益
        KxsUserAmountLog add = baseMapper.selectOne(queryWrapper);
        amountInExpendTotalVO.setIncome(add.getAmount());

        //支出
        queryWrapper.clear();
        queryWrapper.eq("user_id", SecurityUtils.getUser().getId());
        queryWrapper.eq("DATE_FORMAT(create_time, '%Y-%m')", tradeMonth);
        queryWrapper.eq("kind", KindTypeEnum.SUB.getType());
        queryWrapper.select("IFNULL(SUM(amount),0) AS amount");
        KxsUserAmountLog sub = baseMapper.selectOne(queryWrapper);
        amountInExpendTotalVO.setExpend(sub.getAmount());

        return amountInExpendTotalVO;
    }

    @Override
    public Object variationTypeList(String kind) {
        return baseMapper.variationTypeList(kind);
    }

    @Override
    public Object variationList(String changeTypeId) {
        return baseMapper.variationList(changeTypeId);
    }
}

