package com.zhongshu.card.server.core.service.org;

import com.zhongshu.card.client.model.org.RegionModel;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.utils.ITree;
import com.zhongshu.card.client.utils.TreeUtil;
import com.zhongshu.card.server.core.dao.org.RegionDao;
import com.zhongshu.card.server.core.domain.org.Region;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class RegionService {

    @Autowired
    RegionDao regionDao;

    public ResultContent<List<RegionModel>> getRegion(RegionModel regionModel) {
        List<RegionModel> byCodeStartingWithAndTypeAndName = regionDao.findByCodeStartingWithAndTypeAndName(regionModel);
        return ResultContent.buildContent(byCodeStartingWithAndTypeAndName);
    }

    public ResultContent<List<ITree>> getAllTree() {
        List<Region> list = regionDao.findAll(Sort.by(Sort.Order.asc("index")));
        List<ITree> models = list.parallelStream().map(this::toModel).collect(Collectors.toList());
        List<ITree> iTrees = TreeUtil.buildTree(models, ITree.ROOT_ID);
        return ResultContent.buildSuccess(iTrees);
    }

    public ResultContent<List<RegionModel>> getListByParentCode(String parentCode) {
        List<RegionModel> models = new ArrayList<>();
        if (StringUtils.isEmpty(parentCode)) {
            parentCode = ITree.ROOT_ID;
        }
        List<Region> list = regionDao.findByParentIdOrderByIndexAsc(parentCode);
        if (ObjectUtils.isNotEmpty(list)) {
            models = list.parallelStream().map(this::toModel).collect(Collectors.toList());
        }
        return ResultContent.buildSuccess(models);
    }

    public void fixData() {
        Region region = regionDao.findByCode("11");
        if (region != null) {
            log.info("修复数据");
            if (StringUtils.isEmpty(region.getParentId())) {
                List<Region> list = regionDao.findAll();
                list.stream().forEach(it -> {
                    String code = it.getCode();
                    String parentId = ITree.ROOT_ID;
                    if (code != null && code.length() > 2) {
                        parentId = code.substring(0, code.length() - 2);
                    }
                    it.setIndex(Long.parseLong(code));
                    it.setParentId(parentId);
                });
                regionDao.saveAll(list);
            }

            if (region.getIndex() == null || region.getIndex() == 0) {
                List<Region> list = regionDao.findAll();
                list.stream().forEach(it -> {
                    String code = it.getCode();
                    it.setIndex(Long.parseLong(code));
                });
                regionDao.saveAll(list);
            }
        }
    }

    public RegionModel toModel(Region entity) {
        RegionModel model = new RegionModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);
            model.setId(entity.getCode());
        }
        return model;
    }
}
