package com.mcwl.communityCenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.common.utils.StringUtils;
import com.mcwl.communityCenter.constant.AdviceConstant;
import com.mcwl.communityCenter.domain.*;
import com.mcwl.communityCenter.domain.dto.*;
import com.mcwl.communityCenter.domain.vo.IncomeAmountVo;
import com.mcwl.communityCenter.domain.vo.IncomeInfoListVo;
import com.mcwl.communityCenter.domain.vo.IncomeInfoVo;
import com.mcwl.communityCenter.domain.vo.QuestionVo;
import com.mcwl.communityCenter.mapper.*;
import com.mcwl.communityCenter.service.IncomeInfoService;
import com.mcwl.communityCenter.service.QuestionService;
import com.mcwl.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class IncomeInfoServiceImpl extends ServiceImpl<IncomeInfoMapper, IncomeInfo> implements IncomeInfoService {


    /**
     * 社区收益
     */
    @Override
    public R<IncomeInfoVo> communityIncome() {

        // 社区收益
        IncomeAmountVo communityIncome = baseMapper.communityIncome(SecurityUtils.getUserId());

        // 问答收益
        IncomeAmountVo questionIncome = baseMapper.questionIncome(SecurityUtils.getUserId());

        // 累计收益
        Double totalIncome = baseMapper.totalIncome(SecurityUtils.getUserId());

        IncomeInfoVo incomeInfoVo = IncomeInfoVo.builder()
                .communityIncome(communityIncome)
                .questionIncome(questionIncome)
                .totalIncome(totalIncome)
                .build();

        return R.ok(incomeInfoVo);
    }

    @Override
    public TableDataInfo incomeList(IncomeInfoListPageRes incomeInfoListPageRes) {
        Page<IncomeInfo> page = this.initPage(incomeInfoListPageRes);
        List<IncomeInfoListVo> incomeInfoList = baseMapper.incomeList(page, incomeInfoListPageRes, SecurityUtils.getUserId());


        // 封装分页信息
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(incomeInfoList);
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    private Page<IncomeInfo> initPage(PageDomain pageDomain) {
        return new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
    }

}
