package com.mortals.xhx.module.base.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.mortals.framework.common.Rest;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.area.model.AreaEntity;
import com.mortals.xhx.module.area.service.AreaService;
import com.mortals.xhx.module.base.dao.BaseAreaDao;
import com.mortals.xhx.module.base.model.BaseAreaEntity;
import com.mortals.xhx.module.base.model.BaseAreaQuery;
import com.mortals.xhx.module.base.service.BaseAreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * BaseAreaService
 * 区域 service实现
 *
 * @author zxfei
 * @date 2022-11-22
 */
@Service("baseAreaService")
public class BaseAreaServiceImpl extends AbstractCRUDServiceImpl<BaseAreaDao, BaseAreaEntity, Long> implements BaseAreaService {

    @Autowired
    private AreaService areaService;
    @Override
    public Rest<String> genSubAreaByRootName(BaseAreaEntity baseAreaEntity, Context context) {

        String areaCode = baseAreaEntity.getAreaCode();
        String matchCode = areaCode.replaceAll("(0)+$", "");
        BaseAreaQuery baseAreaQuery = new BaseAreaQuery();
        baseAreaQuery.setAreaCode(matchCode + "%");
        List<BaseAreaEntity> baseAreaEntityList =this.find(baseAreaQuery);

        //清除子区域数据
        Rest<String> rest = areaService.removeAll();
        List<AreaEntity> collect = baseAreaEntityList.stream().map(item -> {
            AreaEntity areaEntity = new AreaEntity();
            areaEntity.initAttrValue();
            BeanUtils.copyProperties(item, areaEntity,BeanUtil.getNullPropertyNames(item));
            if(item.getAreaCode().equalsIgnoreCase(areaCode)){
                areaEntity.setPid("0");
            }
            return areaEntity;
        }).collect(Collectors.toList());
        log.info("更新数据条数:"+collect.size());
        List<List<AreaEntity>> partition = ListUtil.partition(collect, 100);
        for (List<AreaEntity> areaEntities : partition) {
            areaService.save(areaEntities, context);
        }
        areaService.loadCache();
        return Rest.ok("初始化区域数据成功！");
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<BaseAreaEntity> list, BaseAreaEntity t) {
        // 得到子节点列表
        List<BaseAreaEntity> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BaseAreaEntity tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BaseAreaEntity> getChildList(List<BaseAreaEntity> list, BaseAreaEntity t) {
        return list.stream().map(item -> {
            if (!ObjectUtils.isEmpty(item.getPid()) && item.getPid() == t.getIid()) {
                return item;
            }
            return null;
        }).filter(f -> f != null).collect(Collectors.toList());
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BaseAreaEntity> list, BaseAreaEntity t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}