package com.multictrl.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.multictrl.common.constant.BusinessConstant;
import com.multictrl.common.page.PageData;
import com.multictrl.common.service.impl.CrudServiceImpl;
import com.multictrl.modules.business.dao.GeoMarkDao;
import com.multictrl.modules.business.dto.GeoMarkDTO;
import com.multictrl.modules.business.entity.GeoMarkEntity;
import cn.hutool.core.util.StrUtil;
import com.multictrl.modules.business.service.GeoMarkService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 地图标注
 *
 * @author Sdy
 * @since 1.0.0 2026-05-25
 */
@Service
@RequiredArgsConstructor
public class GeoMarkServiceImpl extends CrudServiceImpl<GeoMarkDao, GeoMarkEntity, GeoMarkDTO> implements GeoMarkService {

    @Override
    public QueryWrapper<GeoMarkEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        String dockSn = (String) params.get("dockSn");
        String markType = (String) params.get("markType");
        String markName = (String) params.get("markName");
        String showAll = (String) params.get("showAll");

        QueryWrapper<GeoMarkEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.and(StrUtil.isNotBlank(dockSn), v ->
                v.eq("dock_sn", dockSn).or(StrUtil.isNotBlank(showAll) && "true".equals(showAll),
                        vv -> vv.eq("dock_sn", "ALL")));
        wrapper.eq(StrUtil.isNotBlank(markType), "mark_type", markType);
        wrapper.like(StrUtil.isNotBlank(markName), "mark_name", markName);
        wrapper.orderByDesc("create_date");

        return wrapper;
    }

    @Override
    public PageData<GeoMarkDTO> pageList(Map<String, Object> params) {
        PageData<GeoMarkDTO> page = page(params);
        for (GeoMarkDTO geoMarkDTO : page.getList()) {
            String iconUrl = geoMarkDTO.getIconUrl();
            if (StrUtil.isNotBlank(iconUrl)) {
                geoMarkDTO.setIconUrl(BusinessConstant.IMAGE_PATH + iconUrl);
            }
        }
        return page;
    }

    @Override
    public GeoMarkDTO getDetail(Long id) {
        GeoMarkDTO geoMarkDTO = get(id);
        if (geoMarkDTO != null) {
            String iconUrl = geoMarkDTO.getIconUrl();
            geoMarkDTO.setIconUrl(BusinessConstant.IMAGE_PATH + iconUrl);
        }
        return geoMarkDTO;
    }
}