package com.gz.service.system.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.dto.system.ArchivesTreeDTO;
import com.gz.dto.system.DeptDTO;
import com.gz.mapper.system.DeptMapper;
import com.gz.service.system.DeptService;
import com.gz.utils.JwtUtils;
import com.gz.vo.PageVO;
import org.apache.catalina.webresources.JarWarResource;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

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

/**
 * @author LiuchangLan
 * @date 2021/3/9 16:16
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    private DeptMapper mapper;

    @Override
    public Integer insert(DeptDTO dto) {
        dto.setCreated(JwtUtils.getCurrentUserJwtPayload().getId());
        return mapper.insertSelective(dto);
    }

    @Override
    public Integer delete(String id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(DeptDTO dto) {
        dto.setUpdated(JwtUtils.getCurrentUserJwtPayload().getId());
        return mapper.updateByPrimaryKeySelective(dto);
    }

    @Override
    public List<DeptDTO> selectAll() {
        Example example = new Example(DeptDTO.class);
        example.setOrderByClause("dept_order ASC");
        List<DeptDTO> deptDTOS = mapper.selectByExample(example);

        deptDTOS.forEach(e -> {
            if (e.getParentId() == null) {
                e.setParentId("null");
            }
        });
        return deptDTOS;
    }

    @Override
    public DeptDTO selectByPrimaryKey(String id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DeptDTO> selectByOrder() {
        Example example = new Example(DeptDTO.class);
        example.setOrderByClause("dept_order ASC");
        return mapper.selectByExample(example);
    }

    @Override
    public List<Tree<String>> selectTree() {
        List<DeptDTO> deptDTOS = this.selectAll();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setIdKey("id");
        // 最大递归深度
        treeNodeConfig.setDeep(3);

        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(deptDTOS, "null", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setName(treeNode.getDeptName());
                    tree.putExtra("title", treeNode.getDeptName());
                    tree.putExtra("spread", true);
                });
        return treeNodes;
    }

    @Override
    public PageInfo<DeptDTO> selectByPage(PageVO vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        return new PageInfo<>(this.selectAll());
    }
}
