package com.weather.modules.weather.station.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weather.common.constant.Constant;
import com.weather.common.page.PageData;
import com.weather.common.service.impl.CrudServiceImpl;
import com.weather.common.utils.ConvertUtils;
import com.weather.modules.sys.service.SysDeptService;
import com.weather.modules.weather.station.dao.WeatherStationDao;
import com.weather.modules.weather.station.dto.WeatherStationDTO;
import com.weather.modules.weather.station.dto.WeatherStationInfoSimpleDto;
import com.weather.modules.weather.station.entity.WeatherStationEntity;
import com.weather.modules.weather.station.service.WeatherStationService;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 气象站点表
 *
 * @author Mark 123
 * @since 1.0.0 2026-03-05
 */
@Service
@AllArgsConstructor
public class WeatherStationServiceImpl extends CrudServiceImpl<WeatherStationDao, WeatherStationEntity, WeatherStationDTO> implements WeatherStationService {
    private final SysDeptService sysDeptService;
    @Override
    public QueryWrapper<WeatherStationEntity> getWrapper(Map<String, Object> params) {
        String stationName = (String) params.get("stationName");
        String stationCode = (String) params.get("stationCode");

        QueryWrapper<WeatherStationEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(stationName), "station_name", stationName);
        wrapper.like(StrUtil.isNotBlank(stationCode), "station_code", stationCode);
        return sysDeptService.limitedQueryScope(wrapper);
    }

    @Override
    public PageData<WeatherStationDTO> page(Map<String, Object> params) {
        IPage<WeatherStationEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );

        return getPageData(page, WeatherStationDTO.class);

    }

    @Override
    public WeatherStationDTO getByStationCode(Long stationCode) {
        QueryWrapper<WeatherStationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("station_code", stationCode);
        return ConvertUtils.sourceToTarget(baseDao.selectOne(wrapper),WeatherStationDTO.class);
    }

    @Override
    public List<WeatherStationInfoSimpleDto> getStationByDeptId() {
        QueryWrapper<WeatherStationEntity> wrapper = new QueryWrapper<>();
        wrapper = sysDeptService.limitedQueryScope(wrapper);
        List<WeatherStationEntity> entities = baseDao.selectList(wrapper);
        return BeanUtil.copyToList(entities, WeatherStationInfoSimpleDto.class);
    }

    @Override
    public List<WeatherStationInfoSimpleDto> getStationByStationName() {
        return List.of();
    }

}