package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.PayConfig;
import com.ruoyi.cai.domain.PayTotal;
import com.ruoyi.cai.dto.admin.vo.PayTotalAdminVo;
import com.ruoyi.cai.mapper.PayTotalMapper;
import com.ruoyi.cai.pay.PayConfigManager;
import com.ruoyi.cai.pay.PayTypeEnum;
import com.ruoyi.cai.service.PayTotalService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 支付统计Service业务层处理
 *
 * @author 77
 * @date 2024-01-21
 */
@Service
public class PayTotalServiceImpl extends ServiceImpl<PayTotalMapper,PayTotal> implements PayTotalService {

    @Override
    public void incsPrice(PayTypeEnum payTypeEnum, String appId, BigDecimal price) {
        PayTotal one = this.getOne(Wrappers.lambdaQuery(PayTotal.class)
                .eq(PayTotal::getPayId, appId)
                .eq(PayTotal::getDate, LocalDate.now())
                .last("limit 1"));
        if(one == null){
            ReentrantLock lock = new ReentrantLock();
            try {
                lock.lock();
                one = this.getOne(Wrappers.lambdaQuery(PayTotal.class)
                        .eq(PayTotal::getPayId, appId)
                        .eq(PayTotal::getDate, LocalDate.now())
                        .last("limit 1"));
                if(one == null){
                    one = new PayTotal();
                    one.setPayId(appId);
                    one.setDate(LocalDate.now());
                    one.setPayType(payTypeEnum.getCode());
                    one.setMoney(BigDecimal.ZERO);
                    this.save(one);
                }
            }finally {
                lock.unlock();
            }
        }
        baseMapper.incsPrice(one.getId(),price);
    }
    @Autowired
    private PayConfigManager payConfigManager;

    @Override
    public Page<PayTotalAdminVo> pageAdmin(PageQuery pageQuery, PayTotal bo) {
        Page<PayTotal> page = this.page(pageQuery.build(), Wrappers.lambdaQuery(bo)
                .orderByDesc(PayTotal::getDate));
        List<PayTotal> records = page.getRecords();
        List<PayTotalAdminVo> vos = BeanConvertUtil.convertListTo(records, PayTotalAdminVo::new);
        for (PayTotalAdminVo vo : vos) {
            Collection<PayConfig> config = payConfigManager.getAllPayConfig();
            for (PayConfig payConfig : config) {
                if(PayTypeEnum.ALI.getCode().equals(vo.getPayType())){ // 阿里
                    if(vo.getPayId().equals(payConfig.getAppid())){
                        vo.setName(payConfig.getName());
                    }
                }else if(PayTypeEnum.WX.getCode().equals(vo.getPayType())){ // wx
                    if(vo.getPayId().equals(payConfig.getWxMcid())){
                        vo.setName(payConfig.getName());
                    }
                }
            }
        }
        Page<PayTotalAdminVo> resp = new Page<>();
        resp.setRecords(vos);
        resp.setTotal(page.getTotal());
        resp.setSize(page.getSize());
        resp.setCurrent(page.getCurrent());
        return resp;
    }

}

