package com.bocloud.ims.service.resource;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.Param;
import com.bocloud.ims.entity.resource.NetworkArea;
import com.bocloud.ims.interfaces.resource.NetworkAreaService;
import org.springframework.stereotype.Service;

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

/**
 * @Date: Created in 2018/10/15
 * @Author: guopeng
 * @Description:
 */
@Service("networkAreaService")
public class NetworkAreaServiceImpl implements NetworkAreaService {
    /**
     * 查询网络区域列表
     *
     * @param page
     * @param rows
     * @param params
     * @param sorter
     * @param simple
     * @return
     */
    @Override
    public BocloudResult list(int page, int rows, List<Param> params, Map<String, String> sorter, boolean simple) {
        return null;
    }

    /**
     * 查看网络区域详情
     *
     * @param id
     * @return
     */
    @Override
    public BocloudResult detail(Long id) {
        return null;
    }

    /**
     * 创建网络区域
     *
     * @param networkArea
     * @return
     */
    @Override
    public BocloudResult create(NetworkArea networkArea) {
        return null;
    }

    /**
     * 更新网络区域
     *
     * @param networkArea
     * @return
     */
    @Override
    public BocloudResult modify(NetworkArea networkArea) {
        return null;
    }

    /**
     * 删除网络区域
     *
     * @param id
     * @param userId
     * @return
     */
    @Override
    public BocloudResult remove(Long id, Long userId) {
        return null;
    }

    /**
     * 根据物理位置查询对应网络区域
     *
     * @param dcId
     * @return
     */
    @Override
    public BocloudResult listByDc(Long dcId) {
        return null;
    }

    /**
     * 查询未绑定云平台的网络区域
     *
     * @param dcId
     * @return
     */
    @Override
    public BocloudResult listUnbindNetworkArea(Long dcId) {
        return null;
    }

    /**
     * 绑定集群
     *
     * @param defClusterId
     * @param NetworkAreaId
     * @param clusterIds
     * @param userId
     * @return
     */
    @Override
    public BocloudResult bind(Long defClusterId, Long NetworkAreaId, List<Long> clusterIds, Long userId) {
        return null;
    }
   /* private static final Logger logger = LoggerFactory.getLogger(NetworkAreaServiceImpl.class);

    @Autowired
    private NetworkAreaRepository networkAreaRepository;
    @Autowired
    private ClusterRepository clusterRepository;
    @Autowired
    private IpPoolRepository ipPoolRepository;
    @Autowired
    private LockFactory lockFactory;

    @Override
    public BocloudResult list(int page, int rows, List<Param> params, Map<String, String> sorter, boolean simple) {
        List<NetworkArea> list = null;
        int total = 0;
        GridBean gridBean = null;
        try {
            if (null == params) {
                params = new ArrayList<>();
            }
            if (null == sorter) {
                sorter = Maps.newHashMap();
            }
            total = networkAreaRepository.count(params);
            if (simple) {
                List<SimpleBean> beans = this.networkAreaRepository.list(params, sorter);
                gridBean = new GridBean(1, 1, total, beans);
            } else {
                list = networkAreaRepository.list(page, rows, params, sorter);
                gridBean = GridHelper.getBean(page, rows, total, list);
            }
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("list networkArea failure:", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult detail(Long id) {
        try {
            NetworkArea networkArea = networkAreaRepository.query(id);
            return new BocloudResult<>(true, networkArea, "查询成功");
        } catch (Exception e) {
            logger.error("Get error message：", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult create(NetworkArea networkArea) {
        try {
            if (networkAreaRepository.exists(networkArea.getName(), networkArea.getDcId())) {
                return new BocloudResult<>(false, "名称已存在");
            }
            networkAreaRepository.save(networkArea);
            return new BocloudResult<>(true, networkArea, "添加成功");
        } catch (Exception e) {
            logger.error("Create networkArea error:", e);
            return new BocloudResult<>(false, "添加失败");
        }
    }

    @Override
    public BocloudResult modify(NetworkArea networkArea) {
        String path = networkArea.class.getSimpleName() + "_" + networkArea.getId();
        BocloudLock lock = null;
        try {
            lock = lockFactory.getBLock(path);
            if (!lock.acquire(10, TimeUnit.SECONDS)) {
                logger.warn("Get BocloudLock time out!");
                return new BocloudResult<>(false, "请求超时");
            }
            NetworkArea oldnetworkArea = this.networkAreaRepository.query(networkArea.getId());
            if (null == oldnetworkArea) {
                logger.warn("networkArea does not exist!");
                return new BocloudResult<>(false, "数据不存在");
            }
            if (!networkArea.getName().equals(oldnetworkArea.getName())) {
                if (networkAreaRepository.exists(networkArea.getName(), oldnetworkArea.getDcId())) {
                    return new BocloudResult<>(false, "名称已存在");
                }
            }
            oldnetworkArea.setName(networkArea.getName());
            oldnetworkArea.setRemark(networkArea.getRemark());
            oldnetworkArea.setMenderId(networkArea.getMenderId());
            this.networkAreaRepository.update(oldnetworkArea);
            return new BocloudResult<>(true, "修改成功");
        } catch (Exception e) {
            logger.error("Modify networkArea error:", e);
            return new BocloudResult<>(false, "修改失败");
        } finally {
            if (null != lock) {
                lock.release();
            }
        }
    }

    @Override
    public BocloudResult remove(Long id, Long userId) {
        String path = NetworkArea.class.getSimpleName() + "_" + id;
        BocloudLock lock = null;
        try {
            lock = lockFactory.getBLock(path);
            if (!lock.acquire(10, TimeUnit.SECONDS)) {
                logger.warn("Get BocloudLock time out!");
                return new BocloudResult<>(false, "请求超时");
            }
            NetworkArea oldnetworkArea = this.networkAreaRepository.query(id);
            if (null == oldnetworkArea) {
                logger.warn("networkArea does not exist!");
                return new BocloudResult<>(false, "数据不存在");
            }
            List<IpPool> pools = ipPoolRepository.listBynetworkArea(id, null);
            if (!ListTool.isEmpty(pools)) {
                logger.warn("Please delete the IP pool under the network area first");
                return new BocloudResult<>(false, "请先删除网络区域下的IP池");
            }
            List<Cluster> clusters = clusterRepository.listBynetworkArea(id);
            for (Cluster cluster : clusters) {
                cluster.setnetworkAreaId(null);
                cluster.setMenderId(userId);
                clusterRepository.update(cluster);
            }
            this.networkAreaRepository.delete(id, userId);
            return new BocloudResult<>(true, "删除成功");
        } catch (Exception e) {
            logger.error("Remove networkArea error:", e);
            return new BocloudResult<>(false, "删除失败");
        } finally {
            if (null != lock) {
                lock.release();
            }
        }
    }

    @Override
    public BocloudResult listByDc(Long dcId) {
        try {
            List<NetworkArea> networkAreas = networkAreaRepository.listByDc(dcId);
            List<NetworkArea> filterAreas = Lists.newArrayList();
            for (NetworkArea networkArea : networkAreas) {
                List<Cluster> clusters = clusterRepository.listBynetworkArea(networkArea.getId());
                if (!ListTool.isEmpty(clusters)) {
                    filterAreas.add(networkArea);
                }
            }
            return new BocloudResult<>(true, filterAreas, "查询成功");
        } catch (Exception e) {
            logger.error("Get error message：", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult listUnbindnetworkArea(Long dcId) {
        try {
            List<NetworkArea> networkAreas = networkAreaRepository.listUnbindnetworkArea(dcId);
            return new BocloudResult<>(true, networkAreas, "查询成功");
        } catch (Exception e) {
            logger.error("Get error message：", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult bind(Long defClusterId, Long networkAreaId, List<Long> clusterIds, Long userId) {
        try {
            NetworkArea networkArea = this.networkAreaRepository.query(networkAreaId);
            if (null == networkArea) {
                logger.warn("networkArea does not exist!");
                return new BocloudResult<>(false, "数据不存在");
            }
            // 清楚旧绑定数据
            List<Cluster> clusters = clusterRepository.listBynetworkArea(networkAreaId);
            for (Cluster cluster : clusters) {
                this.updateCluster(cluster.getId(), null, userId);
            }
            // 更新默认集群
            networkArea.setDefClusterId(defClusterId);
            networkArea.setMenderId(userId);
            networkAreaRepository.update(networkArea);
            if (!ListTool.isEmpty(clusterIds)) {
                // 绑定新集群
                for (Long clusterId : clusterIds) {
                    this.updateCluster(clusterId, networkAreaId, userId);
                }
            }
            return new BocloudResult<>(true, "绑定成功");
        } catch (Exception e) {
            logger.error("bind cluster error:", e);
            return new BocloudResult<>(false, "绑定失败");
        }
    }

    *//**
     * 更新集群
     *
     * @param clusterId
     * @param networkAreaId
     * @param userId
     * @return
     *//*
    private BocloudResult updateCluster(Long clusterId, Long networkAreaId, Long userId) {
        String path = Cluster.class.getSimpleName() + "_" + clusterId;
        BocloudLock lock = null;
        try {
            lock = lockFactory.getBLock(path);
            if (!lock.acquire(10, TimeUnit.SECONDS)) {
                logger.warn("Get BocloudLock time out!");
                return new BocloudResult<>(false, "请求超时");
            }
            Cluster oldCluster = this.clusterRepository.query(clusterId);
            if (null == oldCluster) {
                logger.warn("Cluster does not exist!");
                return new BocloudResult<>(false, "数据不存在");
            }
            oldCluster.setnetworkAreaId(networkAreaId);
            oldCluster.setMenderId(userId);
            clusterRepository.update(oldCluster);
            return new BocloudResult<>(true, "更新成功");
        } catch (Exception e) {
            logger.error("update clusters  error:", e);
            return new BocloudResult<>(false, "更新失败");
        } finally {
            if (null != lock) {
                lock.release();
            }
        }
    }*/
}
