package com.yinet.yyqh.service.impl;

import com.yinet.common.core.domain.Ztree;
import com.yinet.yyqh.domain.YyqhGuidance;
import com.yinet.yyqh.mapper.YyqhGuidanceMapper;
import com.yinet.yyqh.service.YyqhGuidanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 办事指南 业务层处理
 *
 * @author ruoyi
 */
@Service
public class YyqhGuidanceServiceImpl implements YyqhGuidanceService {

    @Autowired
    private YyqhGuidanceMapper guidanceMapper;

    @Override
    public List<YyqhGuidance> selectGuidanceList() {
        List<YyqhGuidance> guidanceList = guidanceMapper.selectGuidanceList();
        return guidanceList;
    }

    @Override
    public YyqhGuidance selectGuidanceById(Long id) {
        YyqhGuidance touchScreenGuidance = guidanceMapper.selectGuidanceById(id);
        return touchScreenGuidance;
    }

    @Override
    public int updateGuidance(YyqhGuidance guidance) {
        int i = guidanceMapper.updateGuidance(guidance);
        return i;
    }

    @Override
    public int insertGuidance(YyqhGuidance guidance) {
        guidance.setSubmitTime(new Date());
        int i = guidanceMapper.insertGuidance(guidance);
        return i;
    }

    @Override
    public int selectCountGuidanceByParentId(Long parentId) {
        int i = guidanceMapper.selectCountGuidanceByParentId(parentId);
        return i;
    }

    @Override
    public List<YyqhGuidance> selectGuidanceByParentId(Long parentId) {
        List<YyqhGuidance> yyqhGuidances = guidanceMapper.selectGuidanceByParentId(parentId);
        return yyqhGuidances;
    }

    @Override
    public int deleteGuidanceById(Long guidanceId) {
        int i = guidanceMapper.deleteGuidanceById(guidanceId);
        return i;
    }

    @Override
    public List<Ztree> guidanceTreeData() {
        List<YyqhGuidance> guidanceList = guidanceMapper.selectGuidanceList();
        List<Ztree> ztrees = initZtree(guidanceList);
        return ztrees;
    }

    @Override
    public List<YyqhGuidance> selectGuidanceListById(Long id) {
        List<YyqhGuidance> childList = guidanceMapper.selectGuidanceByParentId(id);
        List<YyqhGuidance> AllList = guidanceMapper.selectGuidanceNotParentId(id);
        for (int i = 0; i < childList.size(); i++) {
            List<YyqhGuidance> temp = new ArrayList<>();
            for (int j = 0; j < AllList.size(); j++) {
                if (AllList.get(j).getParentId().equals(childList.get(i).getYyqhGuidanceId())) {
                    temp.add(AllList.get(j));
                }
            }
            childList.get(i).setChildren(temp);
        }
        return childList;
    }

    /**
     * 对象转菜单树
     *
     * @param guidanceList 菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<YyqhGuidance> guidanceList) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (YyqhGuidance guidance : guidanceList) {
            if (guidance.getGuidanceType().equals("1")) {
                Ztree ztree = new Ztree();
                ztree.setId(guidance.getYyqhGuidanceId());
                ztree.setpId(guidance.getParentId());
                ztree.setName(guidance.getGuidanceName());
                ztree.setTitle(guidance.getGuidanceName());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    @Override
    public List<YyqhGuidance> selectParentGuidanceList() {
        return guidanceMapper.selectParentGuidanceList();
    }
}
