package com.wansenai.service.support.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wansenai.dto.support.BasAreaDto;
import com.wansenai.entities.support.BasArea;
import com.wansenai.mappers.support.BasAreaMapper;
import com.wansenai.service.support.BasAreaService;
import com.wansenai.vo.support.BasAreaVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class BasAreaServiceImpl extends ServiceImpl<BasAreaMapper, BasArea> implements BasAreaService {

    @Override
    public List<BasAreaVo> getList(BasAreaDto basAreaDto) {
        var result = new ArrayList<BasAreaVo>();
        var areas = this.lambdaQuery()
                .eq(basAreaDto.getId() != null, BasArea::getId, basAreaDto.getId())
                .eq(basAreaDto.getParentId() != null, BasArea::getParentId, basAreaDto.getParentId())
                .eq(StringUtils.isNotEmpty(basAreaDto.getRegionType()), BasArea::getRegionType, basAreaDto.getRegionType())
                .like(StringUtils.isNotEmpty(basAreaDto.getName()), BasArea::getName, basAreaDto.getName())
                .list();
        areas.forEach(model->{
            BasAreaVo vo = new BasAreaVo();
            BeanUtils.copyProperties(model, vo);
            result.add(vo);
        });
        return result;
    }

    @Override
    public BasAreaVo getDetail(Long id) {
        var area = this.getById(id);
        if(Optional.ofNullable(area).isEmpty()){
            return null;
        }else {
            var result = new BasAreaVo();
            BeanUtils.copyProperties(area, result);
            return result;
        }
    }
}
