package cn.pluss.platform.platformData.impl;

import cn.pluss.platform.entity.PlatformData;
import cn.pluss.platform.mapper.PlatformDataMapper;
import cn.pluss.platform.platformData.PlatformDataService;
import cn.pluss.platform.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页数据统计表 服务实现类
 * </p>
 *
 * @author bzg
 * @since 2021-10-20
 */
@Service
public class PlatformDataServiceImpl extends ServiceImpl<PlatformDataMapper, PlatformData> implements PlatformDataService {


    /**
     * @description:统计首页数据
     * @date: 2021/10/20 15:36
     * @return PlatformData
     */
    @Override
    public PlatformData getYestDayPlatformData() {
        return baseMapper.getYestDayPlatformData();
    }

    /**
     * @description:获得昨日退款数据
     * @date: 2021/10/20 15:49
     * @return cn.pluss.platform.entity.PlatformData
     */
    @Override
    public PlatformData getYestDayPlatformRefundData() {
        return baseMapper.getYestDayPlatformRefundData();
    }

    /**
     * @description:获取昨日新增数据
     * @date: 2021/10/20 15:54
     * @return cn.pluss.platform.entity.PlatformData
     */
    @Override
    public PlatformData getYestDayPlatformAddData() {
        return baseMapper.getYestDayPlatformAddData();
    }

    @Override
    public PlatformData censusPlatformData() {
        Date frontDate = DateUtils.add(Calendar.DAY_OF_MONTH, DateUtils.currentDate(), -1);
        String frontStr = DateUtils.toString(frontDate);
        QueryWrapper<PlatformData> queryWrapper = new QueryWrapper<PlatformData>().eq("operatDate",frontStr);
        PlatformData data = baseMapper.selectOne(queryWrapper);
        if(data != null){
            return data;
        }
        PlatformData oData = baseMapper.getYestDayPlatformData();
        PlatformData rData = baseMapper.getYestDayPlatformRefundData();
        PlatformData aData = baseMapper.getYestDayPlatformAddData();
        BigDecimal actualAmt = oData.getProfitAmt().subtract(oData.getShareAmt()).subtract(oData.getFansAmt()).setScale(2, RoundingMode.HALF_UP);
        oData.setActualAmt(actualAmt);
        oData.setRefundAmt(rData.getRefundAmt());
        oData.setAliRefundAmt(rData.getAliRefundAmt());
        oData.setWxRefundAmt(rData.getWxRefundAmt());
        oData.setYsfRefundAmt(rData.getYsfRefundAmt());
        oData.setPromoterNum(aData.getPromoterNum());
        oData.setMemberNum(aData.getMemberNum());
        oData.setStoreNum(aData.getStoreNum());
        oData.setOperatDate(frontStr);
        baseMapper.insert(oData);
        return oData;
    }

    @Override
    public List<Map<String, Object>> getChartTransCountData() {
        return baseMapper.getChartTransCountData();
    }

    @Override
    public List<Map<String, Object>> getChartChannelCountData() {
        return baseMapper.getChartChannelCountData();
    }
}
