package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomInfoDao;
import com.crm.manager.dao.mapper.CustomLoginDao;
import com.crm.manager.dao.mapper.UserInfoDao;
import com.crm.manager.service.DataRoleService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by max on 2020/6/17.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
public class DataRoleServiceImpl implements DataRoleService {


    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private CustomLoginDao customLoginDao;
    @Autowired
    private CustomInfoDao customInfoDao;

    @Override
    public void validateByCustomIdAndStamp(Long customId, String stamp) throws Exception {
        /*
            获取代理ID
         */
        Long agentId = customInfoDao.getAgentIdById(customId);
        if (agentId == null || agentId <= 0) {
            throw new ServiceException(Constants.NOT_PERMIT);
        }
        /*
            获取代理的stamp
         */
        String subStamp = userInfoDao.getStampById(agentId);
        if (!subStamp.startsWith(stamp)) {
            throw new ServiceException(Constants.NOT_PERMIT);
        }
    }

    @Override
    public void validateByLoginAndStamp(Long login, String stamp) throws Exception {
        /*
            获取客户ID
         */
        Long customId = customLoginDao.getAgentIdByLogin(login);
        if (customId == null || customId <= 0) {
            throw new ServiceException(Constants.NOT_PERMIT);
        }
        //客户ID验证
        validateByCustomIdAndStamp(customId, stamp);
    }

    @Override
    public void validateByCustomIdListAndStamp(List<Long> customIds, String stamp) throws Exception {
        /*
            获取代理ID集合
         */
        List<Long> agentIds = customInfoDao.getAgentIdListByIds(customIds);
        if (CollectionUtils.isEmpty(agentIds)) {
            throw new ServiceException(Constants.NOT_PERMIT);
        }
        /*
            获取代理stamp集合
         */
        List<String> stamps = userInfoDao.getStampListByIds(agentIds);
        if (CollectionUtils.isEmpty(stamps)) {
            throw new ServiceException(Constants.NOT_PERMIT);
        }
        for (String s : stamps) {
            if (s == null || !s.startsWith(stamp)) {
                throw new ServiceException(Constants.NOT_PERMIT);
            }
        }
    }

    @Override
    public void validateByLoginListAndStamp(List<Long> logins, String stamp) throws Exception {
        /*
            获取客户ID集合
         */
        List<Long> customIds = customLoginDao.getCustomIdsByLogins(logins);
        if (CollectionUtils.isEmpty(customIds)) {
            throw new ServiceException(Constants.NOT_PERMIT);
        }

        validateByCustomIdListAndStamp(logins, stamp);
    }
}
