package com.sqx.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.UserMoneyDetailsDao;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserMoneyDetailsServiceImpl extends ServiceImpl<UserMoneyDetailsDao, UserMoneyDetails> implements UserMoneyDetailsService {

    @Override
    public Result queryUserMoneyDetails(Integer page, Integer limit, Long sysUserId, Long userId, Integer classify,
                                        Integer type, Integer moneyType, Integer viewType) {
        PageHelper.startPage(page, limit);
        QueryWrapper<UserMoneyDetails> queryWrapper = new QueryWrapper();
        if (sysUserId != null) {
            queryWrapper.eq("sys_user_id", sysUserId);
        }
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        if (classify != null) {
            queryWrapper.eq("classify", classify);
        }
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        if (moneyType != null) {
            queryWrapper.eq("money_type", moneyType);
        }
        if (viewType == 1) {
            ArrayList<Integer> arrayList = new ArrayList<>();
            arrayList.add(1);
            arrayList.add(6);
            queryWrapper.in("classify", arrayList);
        }
        queryWrapper.orderByDesc("create_time");
        return Result.success().put("data", PageUtils.page(new PageInfo<>(baseMapper.selectList(queryWrapper)),true));
    }

    @Override
    public Double monthIncome(String date, Long userId) {
        return baseMapper.monthIncome(date, userId);
    }

    @Override
    public Map<String, Double> queryUserTotalEarning(Long userId) {
        Double inviteMoney = baseMapper.queryUserInviteMoney(userId);
        Double inviteGoldMoney = baseMapper.queryUserInviteGoldMoney(userId);

        Map<String, Double> map = new HashMap<>(2);
        map.put("inviteMoney", inviteMoney == null ? 0 : inviteMoney);
        map.put("inviteGoldMoney", inviteGoldMoney == null ? 0 : inviteGoldMoney);

        return map;
    }
}
