package com.aircraft.modules.system.service.impl;

import com.aircraft.modules.system.domain.EmArea;
import com.aircraft.modules.system.domain.EmScenic;
import com.aircraft.modules.system.mapper.EmAreaMapper;
import com.aircraft.modules.system.service.EmAreaService;
import com.aircraft.modules.system.service.EmScenicService;
import com.aircraft.utils.PageResult;
import com.aircraft.utils.PageUtil;
import com.aircraft.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.LinkedHashMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cli
 * @since 2025-07-09
 */
@Service
public class EmAreaServiceImpl extends ServiceImpl<EmAreaMapper, EmArea> implements EmAreaService {

    @Lazy
    @Autowired
    private EmScenicService emScenicService;

    @Override
    public List<EmArea> list(EmArea example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public PageResult<EmArea> page(EmArea example, IPage page) {
        IPage<EmArea> emAreaIPage = this.page(page,buildWrapper(example));
        if (emAreaIPage.getRecords() != null&& !emAreaIPage.getRecords().isEmpty()) {
            for (EmArea record : emAreaIPage.getRecords()) {
                List<EmScenic> scenics = emScenicService.findByAreaId(record.getId());
                record.setScenicNum(scenics.size());
            }
        }
        return PageUtil.toPage(emAreaIPage.getRecords());
    }

    @Override
    public EmArea findByName(String name) {
        return this.baseMapper.findByName(name);
    }

    @Override
    public void setAreaScenicNumToZero() {
        UpdateWrapper<EmArea> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(EmArea::getScenicNum, 0);
        wrapper.lambda().eq(EmArea::getDelFlag, 0);
        super.update(wrapper);
    }

    @Override
    public List<EmArea> findByIdIn(List<Integer> collect) {
        QueryWrapper<EmArea> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(EmArea::getId, collect)
                .eq(EmArea::getDelFlag, 0);
        return super.list(wrapper);
    }


    @Override
    public Map<Long, String> getNameMapByIds(List<Long> regionIds) {
        if (regionIds == null || regionIds.isEmpty()) {
            return Collections.emptyMap();
        }

        // 使用MyBatis-Plus的条件构造器查询区域信息
        LambdaQueryWrapper<EmArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EmArea::getId, regionIds);

        // 执行查询
        List<EmArea> areas = list(queryWrapper);

        // 将结果转换为Map<Long, String>
        return areas.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(
                        area -> area.getId().longValue(), // 区域ID
                        EmArea::getName,                  // 区域名称
                        (existing, replacement) -> existing, // 处理重复键的策略
                        LinkedHashMap::new                // 使用有序Map保持插入顺序
                ));
    }

    @Override
    public EmArea findById(Long areaId) {
        QueryWrapper<EmArea> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(EmArea::getId, areaId);
        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<EmArea> buildWrapper(EmArea example) {
        QueryWrapper<EmArea> wrapper = new QueryWrapper<>();

        String name = example.getName();
        String createBy = example.getCreateBy();

        wrapper.lambda()
                .like(StringUtils.isNotEmpty(name), EmArea::getName, name)
                .eq(StringUtils.isNotEmpty(createBy), EmArea::getCreateBy, createBy)
                .eq(EmArea::getDelFlag, 0)
                .orderByDesc(EmArea::getId);
        return wrapper;
    }
}
