package com.izouma.awesomeadmin.service.impl;

import java.util.*;

import com.izouma.awesomeadmin.dto.TreeNode;
import com.izouma.awesomeadmin.model.SysRole;
import com.izouma.awesomeadmin.model.UserInfo;
import com.izouma.awesomeadmin.service.SysRoleService;
import com.izouma.awesomeadmin.service.UserInfoService;
import com.izouma.awesomeadmin.util.TreeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dao.DepartInfoMapper;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.DepartInfo;
import com.izouma.awesomeadmin.service.DepartInfoService;

/**
 * depart_info service接口实现类
 * Thu Apr 26 10:56:37 CST 2018  Suo Chen Cheng
 */
@Service
public class DepartInfoServiceImpl implements DepartInfoService {

    private static Logger logger = Logger.getLogger(DepartInfoServiceImpl.class);

    @Autowired
    private DepartInfoMapper departInfoMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public List<DepartInfo> getDepartInfoList(DepartInfo record) {

        logger.info("getDepartInfoList");
        try {

            return departInfoMapper.queryAllDepartInfo(record);
        } catch (Exception e) {
            logger.error("getDepartInfoList", e);
        }

        return null;
    }

    @Override
    public List<DepartInfo> getDepartInfoByPage(Page page, DepartInfo record) {

        logger.info("getDepartInfoByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            return departInfoMapper.queryDepartInfosByPage(parameter);
        } catch (Exception e) {
            logger.error("getDepartInfoByPage", e);
        }

        return null;
    }

    @Override
    public DepartInfo getDepartInfoById(String id) {

        logger.info("getDepartInfoById");
        try {

            return departInfoMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getDepartInfoById", e);
        }

        return null;
    }

    @Override
    public DepartInfo getDepartInfo(DepartInfo record) {

        logger.info("getDepartInfo");
        try {

            return departInfoMapper.queryDepartInfo(record);
        } catch (Exception e) {
            logger.error("getDepartInfo", e);
        }

        return null;
    }

    @Override
    public boolean createDepartInfo(DepartInfo record) {

        logger.info("createDepartInfo");
        try {

            int updates = departInfoMapper.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createDepartInfo", e);
        }

        return false;
    }

    @Override
    public boolean deleteDepartInfo(String id) {

        logger.info("deleteDepartInfo");
        try {

            int updates = departInfoMapper.delete(id);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deleteDepartInfo", e);
        }

        return false;
    }

    @Override
    public boolean updateDepartInfo(DepartInfo record) {

        logger.info("updateDepartInfo");
        try {

            int updates = departInfoMapper.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("updateDepartInfo", e);
        }

        return false;
    }

    @Override
    public List<DepartInfo> getParentList(Integer id) {
        logger.info("getParentList");
        try {

            List<DepartInfo> list = departInfoMapper.getParentList(id);

            return list;
        } catch (Exception e) {
            logger.error("getParentList", e);
        }
        return null;
    }

    private List<DepartInfo> getParents(DepartInfo departInfo) {
        List<DepartInfo> menuList = new ArrayList<>();
        menuList.add(departInfo);
        if (departInfo.getParentId() != null) {
            DepartInfo parent = getDepartInfoById(String.valueOf(departInfo.getParentId()));
            if (parent != null) {
                menuList.addAll(getParents(parent));
            }
        }
        return menuList;
    }


    @Override
    public List<TreeNode> getDepartTree(boolean all, Integer userId) {

        logger.info("getDepartTree");
        try {
            List<DepartInfo> pp = getDepartInfoList(new DepartInfo());
            List<TreeNode> trees = new ArrayList<>();
            List<TreeNode> allNodes = new ArrayList<>();
            for (DepartInfo info : pp) {
                TreeNode treeNode = new TreeNode(String.valueOf(info.getId()), info.getDepartName(), String.valueOf(info.getParentId()), info);
                allNodes.add(treeNode);
            }
            TreeUtils.buildTree(trees, allNodes);
            return trees;
        } catch (Exception e) {
            logger.error("getDepartTree", e);
        }

        return null;
    }

    @Override
    public List<UserInfo> getDepartUsers(Integer departId) {

        logger.info("getDepartUsers");
        try {
            return departInfoMapper.getDepartUsers(departId);
        } catch (Exception e) {
            logger.error("getDepartUsers", e);
        }

        return null;
    }

    private List removeDuplicate(List list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }
}

