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.StringUtils;
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;
    }

    // 加盟商数量
    @Override
    public Integer selectJoinCount(AreaJoinQuery query) {
        AreaJoinQuery joinQuery = new AreaJoinQuery();
        BeanUtils.copyProperties(query, joinQuery);
        joinQuery.setType(AreaJoinType.JOIN.getCode());
        return selectCount(query);
    }

    // 合伙人数量
    @Override
    public Integer selectPartnerCount(AreaJoinQuery query) {
        AreaJoinQuery partnerQuery = new AreaJoinQuery();
        BeanUtils.copyProperties(query, partnerQuery);
        partnerQuery.setType(AreaJoinType.PARTNER.getCode());
        return selectCount(query);
    }

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

    @Override
    public BigDecimal selectRemainPoint(Long excludeId, Long areaId, String type, BigDecimal totalPoint) {
        if (areaId == null || StringUtils.isBlank(type)) {
            return BigDecimal.ZERO;
        }
        if (totalPoint == null) {
            totalPoint = BigDecimal.ZERO;
        }
        // 合伙人，查询当前运营区所有合伙人之和
        if (AreaJoinType.PARTNER.getCode().equals(type)) {
            AreaJoinQuery query = new AreaJoinQuery();
            query.setAreaId(areaId);
            query.setType(AreaJoinType.PARTNER.getCode());
            query.setExcludeId(excludeId);
            return MathUtils.subtractDecimal(totalPoint, areaJoinMapper.selectSumOfPoint(query));
        }
        // 加盟商，直接返回运营商的分成比例
        else if (AreaJoinType.JOIN.getCode().equals(type)) {
            return totalPoint;
        }
        return BigDecimal.ZERO;
    }

    @Override
    public BigDecimal selectMaxOfAreaPoint(Long areaId) {
        if (areaId == null) {
            return BigDecimal.ZERO;
        }

        // 查询合伙人
        AreaJoinQuery query = new AreaJoinQuery();
        query.setAreaId(areaId);
        query.setType(AreaJoinType.PARTNER.getCode());
        BigDecimal partnerPoint = areaJoinMapper.selectSumOfPoint(query);

        // 查询加盟商
        query.setType(AreaJoinType.JOIN.getCode());
        BigDecimal joinPoint = areaJoinMapper.selectMaxOfPoint(query);

        // 合伙人加算，加盟商取最大值
        return MathUtils.max(partnerPoint, joinPoint);
    }

    @Override
    public Integer selectCount(AreaJoinQuery query) {
        return MathUtils.dv(areaJoinMapper.selectCount(query));
    }

    @Override
    public Integer selectOperationCount(AreaJoinQuery query) {
        AreaJoinQuery operationQuery = new AreaJoinQuery();
        BeanUtils.copyProperties(query, operationQuery);
        operationQuery.setType(AreaJoinType.OPERATION.getCode());
        return selectCount(query);
    }

}
