package com.qnfhq.modules.business.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qnfhq.modules.business.dao.TMingOrderMapper;
import com.qnfhq.modules.business.dao.TMingProductUserMapper;
import com.qnfhq.modules.business.entity.MingProduct;
import com.qnfhq.modules.business.dao.TMingProductMapper;
import com.qnfhq.modules.business.entity.MingProductUser;
import com.qnfhq.modules.business.entity.TMingOrder;
import com.qnfhq.modules.business.params.MingProductParams;
import com.qnfhq.modules.business.service.ITMingProductService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * mingProductService业务层处理
 * 
 * @author table
 * @date 2023-08-18
 */
@Service
public class TMingProductServiceImpl extends ServiceImpl<TMingProductMapper, MingProduct> implements ITMingProductService
{
    @Resource
    private TMingProductMapper tMingProductMapper;

    @Resource
    private TMingOrderMapper tMingOrderMapper;

    @Resource
    private TMingProductUserMapper tMingProductUserMapper;
    @Override
    public MingProduct selectTMingProductById(Long planId) {
        return tMingProductMapper.selectById(planId);
    }

    @Override
    public IPage<MingProduct> selectTMingProductList(MingProductParams params) {
        IPage<MingProduct> page = new Page<>(params.getCurrent(), params.getSize());
        LambdaQueryWrapper<MingProduct> lambda = new QueryWrapper<MingProduct>().lambda();
        lambda.eq(MingProduct::getStatus,0);
        lambda.eq(StringUtils.isNotBlank(params.getTitle()),MingProduct::getTitle,params.getTitle());
        IPage<MingProduct> mingProductIPage = tMingProductMapper.selectPage(page, lambda);
        if (mingProductIPage.getRecords() == null) {
            return mingProductIPage;
        }
        for(MingProduct mingProduct:mingProductIPage.getRecords()){
            List<TMingOrder> orderList = tMingOrderMapper.selectList(new QueryWrapper<TMingOrder>().lambda().eq(TMingOrder::getPlanId
            ,mingProduct.getId()));
            BigDecimal totalAmount = BigDecimal.ZERO;
            for(TMingOrder mingOrder:orderList){
                totalAmount = totalAmount.add(mingOrder.getAmount());
            }
            if(totalAmount.compareTo(BigDecimal.ZERO) == 0){
                mingProduct.setUserProcess("0%");
            }else{
                BigDecimal rate = totalAmount.divide(mingProduct.getLimitMax(),2,BigDecimal.ROUND_HALF_UP);
                rate = rate.multiply(BigDecimal.valueOf(100));
                mingProduct.setUserProcess(rate+"%");
            }
        }

        List<MingProduct> records = mingProductIPage.getRecords();
        if (!CollectionUtils.isEmpty(records)){
            records.stream().forEach(mProduct->{
                Long count = tMingOrderMapper.selectCount(new LambdaQueryWrapper<TMingOrder>()
                        .eq(TMingOrder::getPlanId, mProduct.getId())
                        .eq(TMingOrder::getUserId, StpUtil.getLoginIdAsLong()));
                if (Objects.nonNull(count)) mProduct.setBuyPurchase((long)count);
                MingProductUser one = tMingProductUserMapper.selectOne(new LambdaQueryWrapper<MingProductUser>()
                        .eq(MingProductUser::getProductId, mProduct.getId())
                        .eq(MingProductUser::getAppUserId, StpUtil.getLoginIdAsLong()));
                if (Objects.nonNull(one)) mProduct.setTimeLimit(one.getPledgeNum());
                if(mProduct.getCoin().equals("ccrwa")) {
                    mProduct.setRate(new BigDecimal("0.46"));
                }
            });
        }

        return mingProductIPage;
    }
}
