package com.xtd.project.hkmanager.service.impl;

import com.github.pagehelper.PageHelper;
import com.xtd.common.exception.CustomException;
import com.xtd.common.utils.IdUtils;
import com.xtd.project.hkmanager.entity.Region;
import com.xtd.project.hkmanager.entity.RegionForm;
import com.xtd.project.hkmanager.mapper.RegionMapper;
import com.xtd.project.hkmanager.service.RegionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author lym
 * @date 2020/4/7 17:28
 */
@Service
public class RegionServiceImpl implements RegionService {

    @Resource
    RegionMapper regionMapper;

    @Override
    public int delByPrimaryKey(String s) {
        return regionMapper.delete(s);
    }

    @Override
    public int save(Region region) {
        return 0;
    }

    @Override
    public int saveSelective(Region region) {
        int isExist = regionMapper.isExistRegion(region);
        if (isExist > 0){
            throw new CustomException("区域名称已存在",1);
        }

        region.setGuid(IdUtils.simpleUUID());
        return regionMapper.insertSelective(region);
    }

    @Override
    public Region queryByPrimaryKey(String s) {
        return regionMapper.selectByPrimaryKey(s);
    }

    @Override
    public int modifyByPrimaryKeySelective(Region region) {
        return regionMapper.updateByPrimaryKeySelective(region);
    }

    @Override
    public int modifyByPrimaryKeyWithBLOBs(Region region) {
        return 0;
    }

    @Override
    public int modifyByPrimaryKey(Region region) {
        return 0;
    }

    @Override
    public int batchSave(Map<String,Region> map) {
        List<String> existIds = regionMapper.findIds();
        for (String id:existIds){
            map.remove(id);
        }
        int i = 0;
        if (map.size() > 0){
            Collection<Region> values = map.values();
            List<Region> list = new ArrayList<>(values);
            i = regionMapper.batchSave(list);
        }

        return i;
    }

    @Override
    public List<Region> selectList(RegionForm region) {
        PageHelper.startPage(region.getPageNum(),region.getPageSize());
        List<Region> list = regionMapper.selectList(region);
        return list;
    }

    @Override
    public List<RegionForm> selectTree(RegionForm form) {
        List<RegionForm> list = regionMapper.selectTree(form);
        List<RegionForm> resultList = new ArrayList<>();
        if (list != null && list.size() > 0){
            for (RegionForm regionForm : list){
                if (form.getParentId() == null){
                    if ("-1".equals(regionForm.getParentId()) || regionForm.getParentId() == null){
                         regionForm.setChildList(getChildList(regionForm,list));
                         resultList.add(regionForm);
                    }
                }else {
                    regionForm.setChildList(selectChilds(regionForm));
                    resultList.add(regionForm);
                }
            }
        }
        return resultList;
    }

    /**
     * 子级
     * @param regionForm
     * @param list
     * @return
     */
    private List<RegionForm> getChildList(RegionForm regionForm,List<RegionForm> list){
        List<RegionForm> childList = new ArrayList<>();
        String parentId = regionForm.getGuid();
        for (RegionForm region : list){
            if (parentId.equals(region.getParentId())){
                region.setChildList(getChildList(region,list));
                childList.add(region);
            }
        }
        return childList;
    }

    private List<RegionForm> selectChilds(RegionForm form){
        RegionForm params = new RegionForm();
        params.setFkOrgGuid(form.getFkOrgGuid());
        params.setParentId(form.getGuid());
        List<RegionForm> list = regionMapper.selectTree(params);
        List<RegionForm> resultList = new ArrayList<>();
        for(RegionForm bean : list){
            bean.setChildList(selectChilds(bean));
            resultList.add(bean);
        }
        return resultList;
    }
}
