package cn.cslg.pas.service;

import cn.cslg.pas.domain.Department;
import cn.cslg.pas.mapper.DepartmentMapper;
import cn.cslg.pas.common.utils.Response;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台部门表 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-03-25
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class DepartmentService extends ServiceImpl<DepartmentMapper, Department> {

    public List<Department> getDepartmentByIds(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Department::getId, ids);
        return this.list(queryWrapper);
    }

    public List<Tree<Integer>> getTreeList() {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getStatus, 1);
        List<Department> departmentList = this.list(queryWrapper);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setWeightKey("sort");
        List<TreeNode<Integer>> nodeList = CollUtil.newArrayList();
        for (Department department : departmentList) {
            nodeList.add(new TreeNode<>(department.getId(), department.getParentId(), department.getName(), department.getSort()));
        }
        return TreeUtil.build(nodeList, 0, treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setWeight(treeNode.getWeight());
            tree.setName(treeNode.getName());
        });
    }

    public String add(Department department) {
        Department temp = this.getDepartmentByName(department.getName());
        if (temp != null) {
            return Response.error("部门名称已存在");
        }
        department.setStatus(1);
        department.insert();
        return Response.success(department.getId());
    }

    public String edit(Department department) {
        Department temp = this.getDepartmentByName(department.getName());
        if (temp != null && !temp.getId().equals(department.getId())) {
            return Response.error("部门名称已存在");
        }
        department.updateById();
        return Response.success();
    }

    public String delete(Integer id) {
        this.removeById(id);
        return Response.success();
    }

    public Department getDepartmentByName(String name) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getName, name);
        return this.getOne(queryWrapper);
    }
}
