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

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

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

import com.ruoyi.bst.area.domain.AreaQuery;
import com.ruoyi.bst.area.domain.AreaVO;
import com.ruoyi.bst.area.mapper.AreaMapper;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.area.service.AreaValidator;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.areaJoin.service.AreaJoinDashboard;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class AreaValidatorImpl implements AreaValidator {

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private AreaService areaService;

    @Autowired
    private AreaJoinDashboard areaJoinDashboard;


    @Override
    public boolean canEdit(Long areaId) {
        return canOperate(Arrays.asList(areaId));
    }

    @Override
    public boolean canDeleteAll(List<Long> areaIds) {
        return canOperate(areaIds);
    }

    @Override
    public boolean canAddSubArea(Long areaId) {
        return canOperate(Arrays.asList(areaId));
    }

    // 是否可以操作运营区
    private boolean canOperate(List<Long> areaIds) {
        return hasPermission(areaIds, AreaJoinPermission.AREA_EDIT.getCode());
    }

    // 是否有运营区权限
    private boolean hasPermission(List<Long> areaIds, String permission) {
        if (SecurityUtils.isSysAdmin()) {
            return true;
        }
        if (CollectionUtils.isEmptyElement(areaIds)) {
            return true;
        }
        // 查询运营区
        AreaQuery query = new AreaQuery();
        query.setIds(areaIds);
        query.setScope(true);
        query.addAreaPermission(permission);
        List<Long> areaIdList = areaMapper.selectIdByQuery(query);
        return new HashSet<>(areaIdList).containsAll(areaIds);
    }

    @Override
    public boolean canView(List<Long> areaIds) {
        return hasPermission(areaIds, AreaJoinPermission.AREA_VIEW.getCode());
    }

    @Override
    public boolean canCheckForJoin(Long areaId) {
        return canOperate(Arrays.asList(areaId));
    }

    @Override
    public boolean canAddCustomerService(Long areaId) {
        return canOperate(Collections.singletonList(areaId));
    }

    private boolean isAgent(AreaVO area, Long userId) {
        return area != null && area.getAgentId() != null && area.getAgentId().equals(userId);
    }

    private boolean isMch(AreaVO area, Long userId) {
        return area != null && area.getUserId() != null && area.getUserId().equals(userId);
    }


    @Override
    public void checkPoint(Long id) {
        AreaVO area = areaService.selectAreaById(id, true);
        ServiceUtil.assertion(area == null, "ID为%s的运营区不存在", id);

        BigDecimal maxPoint = areaJoinDashboard.selectMaxOfAreaPoint(id);
        ServiceUtil.assertion(maxPoint.compareTo(area.getUserPoint()) > 0,
                "运营区【%s】无法设置运营商【%s】,当前运营区已分配最大分成为【%s%%】，超出运营商分成比例【%s%%】",
                area.getName(), area.getUserName(), maxPoint, area.getUserPoint());
    }

}
