package ink.wgink.module.map.service.grid.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.interfaces.map.IGridPointDeleteAfterHandler;
import ink.wgink.interfaces.map.IGridPointSaveAfterHandler;
import ink.wgink.module.map.dao.grid.IGridPointDao;
import ink.wgink.module.map.pojo.dtos.grid.GridPointDTO;
import ink.wgink.module.map.pojo.vos.grid.GridPointVO;
import ink.wgink.module.map.service.grid.IGridPointService;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName: GridPointServiceImpl
 * @Description: 网格点
 * @Author: wanggeng
 * @Date: 2021/8/30 10:07 下午
 * @Version: 1.0
 */
@Service
public class GridPointServiceImpl extends DefaultBaseService implements IGridPointService {

    @Autowired
    private IGridPointDao gridPointDao;
    @Autowired(required = false)
    private IGridPointSaveAfterHandler gridPointSaveAfterHandler;
    @Autowired(required = false)
    private IGridPointDeleteAfterHandler gridPointDeleteAfterHandler;

    @Override
    public void save(String gridId, List<GridPointVO> pointArray) {
        if (pointArray == null || pointArray.isEmpty()) {
            return;
        }
        Map<String, Object> params = getHashMap(4);
        params.put("gridId", gridId);
        for (GridPointVO gridPointVO : pointArray) {
            params.put("lng", gridPointVO.getLng());
            params.put("lat", gridPointVO.getLat());
            gridPointDao.save(params);
        }
        if (gridPointSaveAfterHandler != null) {
            gridPointSaveAfterHandler.handle(gridId);
        }
    }

    @Override
    public void deleteByGridId(String gridId) {
        List<String> gridIds = new ArrayList<>();
        gridIds.add(gridId);
        deleteByGridIds(gridIds);
        if (gridPointDeleteAfterHandler != null) {
            gridPointDeleteAfterHandler.handle(gridIds);
        }
    }

    @Override
    public void deleteByGridIds(List<String> gridIds) throws RemoveException {
        Map<String, Object> params = getHashMap(2);
        params.put("gridIds", gridIds);
        gridPointDao.delete(params);
        if (gridPointDeleteAfterHandler != null) {
            gridPointDeleteAfterHandler.handle(gridIds);
        }
    }

    @Override
    public void updatePointArrayByGridId(String gridId, List<GridPointVO> pointArray) throws Exception {
        // 删除原有网格数组
        Map<String, Object> params = getHashMap(2);
        params.put("gridId", gridId);
        gridPointDao.delete(params);
        if (gridPointDeleteAfterHandler != null) {
            gridPointDeleteAfterHandler.handle(Arrays.asList(gridId));
        }
        // 新增网格数组
        for (GridPointVO gridPointVO : pointArray) {
            params = HashMapUtil.beanToMap(gridPointVO);
            params.put("gridId", gridId);
            gridPointDao.save(params);
        }
        if (gridPointSaveAfterHandler != null) {
            gridPointSaveAfterHandler.handle(gridId);
        }
    }

    @Override
    public List<GridPointDTO> list(Map<String, Object> params) {
        return gridPointDao.list(params);
    }

    @Override
    public List<GridPointDTO> list(String gridId) {
        Map<String, Object> params = getHashMap(2);
        params.put("gridId", gridId);
        return list(params);
    }

    @Override
    public List<GridPointDTO> list(List<String> gridIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("gridIds", gridIds);
        return list(params);
    }
}
