package com.ruoyi.bst.areaJoin.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.areaJoin.domain.AreaJoinQuery;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinType;
import com.ruoyi.bst.areaJoin.domain.vo.AreaJoinStatVO;
import com.ruoyi.bst.areaJoin.mapper.AreaJoinMapper;
import com.ruoyi.bst.areaJoin.service.AreaJoinDashboard;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.dashboard.constants.StatKeys;

@Service
public class AreaJoinDashboardImpl implements AreaJoinDashboard {
    
    @Autowired
    private AreaJoinMapper areaJoinMapper;

    @Override
    public BigDecimal selectMaxOfPoint(AreaJoinQuery query) {
        return areaJoinMapper.selectMaxOfPoint(query);
    }

    @Override
    public AreaJoinStatVO selectStat(AreaJoinQuery query, List<String> keys) {
        AreaJoinStatVO vo = new AreaJoinStatVO();
        if (keys.contains(StatKeys.AREA_JOIN_COUNT)) {
            vo.setJoinCount(selectJoinCount(query));
        } else if (keys.contains(StatKeys.AREA_JOIN_PARTNER_COUNT)) {
            vo.setPartnerCount(selectPartnerCount(query));
        }
        return vo;
    }

    // 加盟商数量
    private int selectJoinCount(AreaJoinQuery query) {
        AreaJoinQuery joinQuery = new AreaJoinQuery();
        BeanUtils.copyProperties(query, joinQuery);
        joinQuery.setType(AreaJoinType.JOIN.getCode());
        return MathUtils.dv(areaJoinMapper.selectCount(joinQuery));
    }

    // 合伙人数量
    private int selectPartnerCount(AreaJoinQuery query) {
        AreaJoinQuery partnerQuery = new AreaJoinQuery();
        BeanUtils.copyProperties(query, partnerQuery);
        partnerQuery.setType(AreaJoinType.PARTNER.getCode());
        return MathUtils.dv(areaJoinMapper.selectCount(partnerQuery));
    }

    @Override
    public BigDecimal selectSumOfPoint(AreaJoinQuery query) {
        return areaJoinMapper.selectSumOfPoint(query);
    }
}
