package com.mcwl.myInvitation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.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.myInvitation.domain.Commission;
import com.mcwl.myInvitation.domain.Consume;
import com.mcwl.myInvitation.domain.vo.ConsumeVo;
import com.mcwl.myInvitation.mapper.ConsumeMapper;
import com.mcwl.myInvitation.service.CommissionService;
import com.mcwl.myInvitation.service.ConsumeService;
import com.mcwl.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ConsumeServiceImpl extends ServiceImpl<ConsumeMapper, Consume> implements ConsumeService {

    private final ConsumeMapper consumeMapper;

    private final CommissionService commissionService;

    private final ISysUserService sysUserService;


    @Override
    @Transactional
    public void saveConsume(Consume consume) {
        consumeMapper.insert(consume);

        Commission commission = new Commission();
        SysUser sysUser = sysUserService.selectUserById(consume.getUserId());
        // TODO 设置用户id
//        commission.setUserId(sysUser.getParentId());

        // 设置消费者id
        commission.setUserId(consume.getUserId());

        // TODO 设置佣金,目前固定2.0
        commission.setAmount(2.0);

        commissionService.save(commission);


    }

    @Override
    public TableDataInfo getWalletRecord(PageDomain pageDomain) {
        Page<Consume> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());

        Long userId = SecurityUtils.getUserId();

        LambdaQueryWrapper<Consume> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Consume::getUserId, userId).orderByDesc(Consume::getCreateTime);

        consumeMapper.selectPage(page, lqw);

        List<Consume> consumeList = page.getRecords();
        List<ConsumeVo> consumeVoList = new ArrayList<>();
        for (Consume consume : consumeList) {
            ConsumeVo consumeVo = new ConsumeVo();
            BeanUtil.copyProperties(consume, consumeVo);
            switch (consume.getType()) {
                case 0:
                    consumeVo.setProductName("模型");
                    break;
                case 1:
                    consumeVo.setProductName("工作流");
                    break;
                case 2:
                    consumeVo.setProductName("图片");
                    break;
                case 3:
                    consumeVo.setProductName("社区");
                    break;
                case 4:
                    consumeVo.setProductName("社区提问");
                    break;
                case 5:
                    consumeVo.setProductName("金币");
                    break;
                case 6:
                    consumeVo.setProductName("提现");
                    break;
            }
            consumeVoList.add(consumeVo);

        }


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(consumeVoList);
        rspData.setTotal(page.getTotal());
        return rspData;
    }
}
