package com.xhpc.user.service.impl;

import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.user.domain.XhpcStationInternetBlacklist;
import com.xhpc.user.mapper.XhpcOperatorMapper;
import com.xhpc.user.mapper.XhpcStationInternetBlacklistMapper;
import com.xhpc.user.service.IXhpcStationInternetBlacklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 流量用户设置流量方黑名单 服务层
 *
 * @author ruoyi
 */
@Service
public class XhpcStationInternetBlacklistServiceImpl implements IXhpcStationInternetBlacklistService {

    @Autowired
    private XhpcStationInternetBlacklistMapper xhpcStationInternetBlacklistMapper;

    @Autowired
    private XhpcOperatorMapper xhpcOperatorMapper;

    /**
     * 新增流量用户设置流量方黑名单信息
     *
     * @param xhpcStationInternetBlacklist 流量用户设置流量方黑名单信息
     */
    @Override
    @Transactional
    public int insert(XhpcStationInternetBlacklist xhpcStationInternetBlacklist) {
        return xhpcStationInternetBlacklistMapper.insert(xhpcStationInternetBlacklist);
    }


    /**
     * 批量删除流量用户设置流量方黑名单信息
     *
     * @param internetUserId 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public void deleteByOperatorIdEvcs(String internetUserId) {

        xhpcStationInternetBlacklistMapper.deleteByOperatorIdEvcs(internetUserId);
    }

    @Override
    public void updateByOperatorIdEvcs(String internetUserId, String operatorIdEvcs) {
        xhpcStationInternetBlacklistMapper.updateByOperatorIdEvcs(internetUserId, operatorIdEvcs);
    }


    /**
     * 流量用户设置流量方黑名单信息列表
     *
     * @param internetUserId 流量用户ID
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> list(String internetUserId) {
        List<Map<String, Object>> list = xhpcOperatorMapper.getOperatorId();
        List<Map<String, Object>> xhpcOperatorList = new ArrayList<>();
        if (null != list && list.size() > 0) {
            List<Map<String, Object>> blacklist = xhpcStationInternetBlacklistMapper.list(Long.parseLong(internetUserId));
            boolean bool = false;
            if (null != blacklist && blacklist.size() > 0) {
                bool = true;
            }
            for (Map<String, Object> map : list) {
                String operatorId = StringUtils.valueOf(map.get("operatorId"));
                List<Map<String, Object>> listXhpcChargingStation = xhpcStationInternetBlacklistMapper.getXhpcChargingStation(Long.parseLong(operatorId));
                if (null != listXhpcChargingStation && listXhpcChargingStation.size() > 0) {
                    for (Map<String, Object> xhpcChargingStation : listXhpcChargingStation) {
                        xhpcChargingStation.put("isBlack", StatusConstants.IS_BLACK);
                        if (bool) {
                            for (Map<String, Object> map1 : blacklist) {
                                String chargingStationId = StringUtils.valueOf(xhpcChargingStation.get("chargingStationId"));
                                String chargingStationId1 = StringUtils.valueOf(map1.get("chargingStationId"));
                                if (chargingStationId.equals(chargingStationId1)) {
                                    xhpcChargingStation.put("isBlack", StatusConstants.BLACK);
                                }
                            }
                        }
                    }
                    map.put("listXhpcChargingStation", listXhpcChargingStation);
                    xhpcOperatorList.add(map);
                }
            }
        }
        return xhpcOperatorList;
    }
}
