package cn.reghao.tnb.content.app.geo.db.repository;

import cn.reghao.tnb.content.app.geo.db.mapper.CameraPhotoMapper;
import cn.reghao.tnb.content.app.geo.db.mapper.MallReplyMapper;
import cn.reghao.tnb.content.app.geo.model.po.CameraPhoto;
import cn.reghao.tnb.content.app.geo.model.po.MallReply;
import cn.reghao.tnb.content.app.geo.model.vo.MapMarker;
import cn.reghao.tnb.content.app.geo.model.vo.MapPoint;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @author reghao
 * @date 2025-08-15 21:10:03
 */
@Repository
public class MallReplyRepository {
    private final MallReplyMapper mallReplyMapper;
    private final CameraPhotoMapper cameraPhotoMapper;

    public MallReplyRepository(MallReplyMapper mallReplyMapper, CameraPhotoMapper cameraPhotoMapper) {
        this.mallReplyMapper = mallReplyMapper;
        this.cameraPhotoMapper = cameraPhotoMapper;
    }

    @CacheEvict(cacheNames = "tnb:geo:mark", key = "#mallReply.itemType")
    @Transactional(rollbackFor = Exception.class)
    public void saveTmallReply(MallReply mallReply, List<CameraPhoto> list) {
        mallReplyMapper.save(mallReply);
        cameraPhotoMapper.saveAll(list);
    }

    @Cacheable(cacheNames = "tnb:geo:mark", key = "#itemType", unless = "#result == null")
    public List<MapMarker> findMapMarkers(int itemType, int pageSize) {
        List<MapMarker> list;
        if (itemType == 1) {
            pageSize = 1000;
            list = cameraPhotoMapper.findAlbumIdGroup(pageSize);
        } else if (itemType == 2) {
            list = cameraPhotoMapper.findByImagePost(pageSize);
        } else if (itemType == 3) {
            pageSize = 1000;
            list = cameraPhotoMapper.findByMallReply(pageSize);
        } else if (itemType == 4) {
            pageSize = 1000;
            list = cameraPhotoMapper.findByMallReplyCup(pageSize);
        } else {
            list = cameraPhotoMapper.findByMallReplyItem(pageSize, itemType);
        }

        return list;
    }

    @Cacheable(cacheNames = "tnb:geo:mark:area", key = "#areaCode", unless = "#result == null")
    public List<MapMarker> findMapMarkers(int areaCode) {
        int pageSize = 1000;
        String areaCodeStr = areaCode+"";
        List<MapMarker> mapMarkerList = cameraPhotoMapper.findByMallReplyCupArea(pageSize, areaCodeStr);
        return mapMarkerList;
    }

    public List<MapMarker> findMapMarkers(MapPoint mapPoint, int distance) {
        List<MapMarker> mapMarkerList = cameraPhotoMapper.findByDistance(mapPoint, distance);
        return mapMarkerList;
    }
}
