package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yonge.cooleshow.biz.dal.entity.SysArea;
import com.yonge.cooleshow.biz.dal.mapper.SysAreaMapper;
import com.yonge.cooleshow.biz.dal.service.SysAreaService;
import com.yonge.cooleshow.biz.dal.wrapper.SysAreaWrapper;
import com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper;
import com.yonge.cooleshow.common.enums.EStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 区域表
 * 2022-11-23 18:28:58
 */
@Slf4j
@Service
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea> implements SysAreaService {

	/**
     * 查询详情
     * @param id 详情ID
     * @return SysAreaWrapper.SysArea
     */
	@Override
    public SysAreaWrapper.SysArea detail(Long id) {

        SysArea entity =  baseMapper.selectById(id);
        SysAreaWrapper.SysArea result = new SysAreaWrapper.SysArea();

        BeanUtils.copyProperties(entity, result);
        return result;
    }

    /**
     * 分页查询
     * @param page IPage<SysAreaWrapper.SysArea>
     * @param query SysAreaWrapper.SysAreaQuery
     * @return IPage<SysAreaWrapper.SysArea>
     */
    @Override
    public IPage<SysAreaWrapper.SysArea> selectPage(IPage<SysAreaWrapper.SysArea> page, SysAreaWrapper.SysAreaQuery query){

        List<SysAreaWrapper.SysArea> sysAreas = baseMapper.selectPage(page, query);
        if (CollectionUtils.isNotEmpty(sysAreas)) {
            // 统计子级数
            List<Integer> collect = sysAreas.stream()
                .filter(x -> x.getCode().toString().endsWith("00"))
                .map(SysAreaWrapper.SysArea::getId).distinct().collect(Collectors.toList());

            Map<Long, Integer> statMap = Maps.newHashMap();
            if (CollectionUtils.isNotEmpty(collect)) {

                statMap = getBaseMapper().selectProvinceAreaNumberStatInfo(collect, EStatus.DISABLE.getValue()).stream()
                    .collect(Collectors.toMap(StatGroupWrapper::getId, StatGroupWrapper::getTotal, (o, n) -> n));
            }

            for (SysAreaWrapper.SysArea item : sysAreas) {

                item.setNumber(statMap.getOrDefault(Long.valueOf(item.getId()), 0));
            }
        }

        return page.setRecords(sysAreas);
    }

    /**
     * 添加
     * @param sysArea SysAreaWrapper.SysArea
     * @return Boolean
     */
    @Override
    public Boolean add(SysAreaWrapper.SysArea sysArea) {

        SysArea result = new SysArea();
        BeanUtils.copyProperties(sysArea, result);

        return this.save(result);
    }

    /**
     * 更新
     * @param sysArea SysAreaWrapper.SysArea
     * @return Boolean
     */
    @Override
    public Boolean update(SysAreaWrapper.SysArea sysArea){

        SysArea result = new SysArea();
        BeanUtils.copyProperties(sysArea, result);

        return this.updateById(result);
    }

	@Override
	public List<SysArea> queryByCodes(List<Integer> codeList) {
        if (CollectionUtils.isEmpty(codeList)) {
            return Lists.newArrayList();
        }
		return baseMapper.queryByCodes(codeList);
	}

    @Override
    public <K extends Collection> Map<Integer, String> getNameMap(K codeList) {
        if(org.springframework.util.CollectionUtils.isEmpty(codeList)){
            return Collections.emptyMap();
        }
        codeList.removeAll(Collections.singleton(null));
        if(org.springframework.util.CollectionUtils.isEmpty(codeList)){
            return Collections.emptyMap();
        }
        List<Map> mapList = baseMapper.getNameMap(codeList);
        return mapList.stream().collect(Collectors.toMap(s -> Integer.parseInt(s.get("key").toString()), s -> s.get("value").toString()));
    }
}
