package com.judong.chuanyiserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.judong.chuanyiserver.dao.DepartmentDao;
import com.judong.chuanyiserver.entity.Department;
import com.judong.chuanyiserver.entity.TreeDepartment;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.exception.CustomException;
import com.judong.chuanyiserver.service.DepartmentService;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public synchronized Result addDepartment(Department department) {
        if (Blank.isEmpty(department.getParentId(), department.getDepartmentName())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "父id，部门名称不能为空");
        }
        if (Blank.isEmpty(departmentDao.getDepartmentByName(department.getDepartmentName()))) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "该部门名称已被使用，请更换其他部门名称");
        }
        if (departmentDao.addDepartment(department) <= 0) {
            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "新增失败");
        }
        return Result.ok("添加部门成功");
    }

    @Override
    public synchronized Result updateDepartment(Department department) {
        if (Blank.isEmpty(department.getId(), department.getParentId(), department.getDepartmentName())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "id,父id，部门名称不能为空");
        }
        if (Blank.isNotEmpty(departmentDao.getDepartmentByNoIdName(department.getId(), department.getDepartmentName()))) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "已存在该部门名称，请更换部门名称");
        }
        if (departmentDao.updateDepartment(department) <= 0) {
            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "修改部门失败");
        }
        return Result.ok("修改部门成功");
    }

    @Override
    public Result getDepartmentListByPage(Integer page, Integer limit) {
        JSONObject jsonObject = new JSONObject();
        Long count = departmentDao.getDepartmentCount();
        Long startNum = Long.valueOf((page - 1) * limit);
        List<Department> departmentList = departmentDao.getDepartmentListByPage(startNum, Long.valueOf(limit));
        jsonObject.put("count", count);
        jsonObject.put("departmentList", departmentList);
        return Result.ok(jsonObject);
    }

    @Override
    public Result getDepartmentById(Integer id) {
        return Result.ok(departmentDao.getDepartmentById(id));
    }

    @Override
    public Result queryDepartmentByParentId(Integer parentId) {
        List<Department> departments = departmentDao.queryDepartmentListByparentId(parentId);
        return Result.ok(departments);
    }

    @Override
    public List<Department> queryDepartmentAllChildren(Integer parentId) {
        //存放节点数据
        List<Department> departments = new ArrayList<>();
        //根据父节点得到一级子节点
        List<Department> departmentSon = departmentDao.queryDepartmentListByparentId(parentId);
        //循环判断每个子节点是否为其他节点父节点
        for (Department d:departmentSon){
            d.setChildren(queryDepartmentAllChildren(d.getId()));
            departments.add(d);
        }
        return departments;
    }

    @Override
    public Result isDeleteById(List<Integer> id) {
        Integer deleteById = departmentDao.isDeleteByid(id);
        return Result.ok(deleteById);
    }


}
