package com.elephant.framework.galaxy.aroad.module.zhyh.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elephant.framework.galaxy.aroad.module.zhjg.entity.ZhjgLzglCxysxk;
import com.elephant.framework.galaxy.aroad.module.zhjg.mapper.ZhjgLzglCxysxkMapper;
import com.elephant.framework.galaxy.aroad.module.zhyh.entity.ZhyhDzxgcglGcxx;
import com.elephant.framework.galaxy.aroad.module.zhyh.entity.ZhyhDzxgcglGcxxTreeOne;
import com.elephant.framework.galaxy.aroad.module.zhyh.entity.ZhyhDzxgcglGcxxTreeTwo;
import com.elephant.framework.galaxy.aroad.module.zhyh.mapper.ZhyhDzxgcglGcxxMapper;
import com.elephant.framework.galaxy.aroad.module.zhyh.service.ZhyhDzxgcglGcxxService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * 智慧养护_大中修工程管理_工程信息
 *
 * @author wangjian
 * @date 2019-04-23 17:00:31
 */
@Service
@AllArgsConstructor
public class ZhyhDzxgcglGcxxServiceImpl extends ServiceImpl<ZhyhDzxgcglGcxxMapper, ZhyhDzxgcglGcxx> implements ZhyhDzxgcglGcxxService {
    private final ZhyhDzxgcglGcxxMapper zhyhDzxgcglGcxxMapper;


    /**
     * 获取tree树结构
     * @return
     */
    public List<ZhyhDzxgcglGcxxTreeOne> getTree() {
        List<ZhyhDzxgcglGcxxTreeOne> zhyhDzxgcglGcxxTreeOnes = getTreeGydwNameGroupBy();

        List<ZhyhDzxgcglGcxx> zhyhDzxgcglGcxxes = zhyhDzxgcglGcxxMapper.selectList(Wrappers.<ZhyhDzxgcglGcxx>query());

        for(ZhyhDzxgcglGcxx zhyhDzxgcglGcxx : zhyhDzxgcglGcxxes) {

            for(ZhyhDzxgcglGcxxTreeOne zhyhDzxgcglGcxxTreeOne: zhyhDzxgcglGcxxTreeOnes) {

                if(zhyhDzxgcglGcxx.getGydw().equals(zhyhDzxgcglGcxxTreeOne.getLabel())) {
                    ZhyhDzxgcglGcxxTreeTwo zhyhDzxgcglGcxxTreeTwo = new ZhyhDzxgcglGcxxTreeTwo();
                    zhyhDzxgcglGcxxTreeTwo.setId(zhyhDzxgcglGcxx.getId());
                    zhyhDzxgcglGcxxTreeTwo.setLabel(zhyhDzxgcglGcxx.getGcmc());
                    zhyhDzxgcglGcxxTreeOne.getChildren().add(zhyhDzxgcglGcxxTreeTwo);
                    break;
                }
            }
        }
        return zhyhDzxgcglGcxxTreeOnes;
    }

    //获取tree树结构
    public List<ZhyhDzxgcglGcxxTreeOne> getTreeGydwNameGroupBy() {
        List<ZhyhDzxgcglGcxx> zhyhDzxgcglGcxxs = zhyhDzxgcglGcxxMapper.selectList(Wrappers.<ZhyhDzxgcglGcxx>query().groupBy("gydw"));
        List<ZhyhDzxgcglGcxxTreeOne> zhyhDzxgcglGcxxTreeOnes = new ArrayList<ZhyhDzxgcglGcxxTreeOne>();
        for(ZhyhDzxgcglGcxx zhyhDzxgcglGcxx : zhyhDzxgcglGcxxs) {
            ZhyhDzxgcglGcxxTreeOne zhyhDzxgcglGcxxTreeOne = new ZhyhDzxgcglGcxxTreeOne();
            List<ZhyhDzxgcglGcxxTreeTwo> children = new ArrayList<ZhyhDzxgcglGcxxTreeTwo>();
            zhyhDzxgcglGcxxTreeOne.setLabel(zhyhDzxgcglGcxx.getGydw());
            zhyhDzxgcglGcxxTreeOne.setChildren(children);
            zhyhDzxgcglGcxxTreeOnes.add(zhyhDzxgcglGcxxTreeOne);
        }
        return zhyhDzxgcglGcxxTreeOnes;
    }
}
