package com.zcloud.basic.info.service;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.zcloud.basic.info.api.DepartmentServiceI;
import com.zcloud.basic.info.command.DepartmentAddExe;
import com.zcloud.basic.info.command.DepartmentRemoveExe;
import com.zcloud.basic.info.command.DepartmentUpdateExe;
import com.zcloud.basic.info.command.query.DepartmentQueryExe;
import com.zcloud.basic.info.dto.*;
import com.zcloud.basic.info.dto.clientobject.CorpDepartmentCO;
import com.zcloud.basic.info.dto.clientobject.DepartmentCO;
import com.zcloud.basic.info.dto.clientobject.DepartmentInfoCO;
import com.zcloud.basic.info.dto.clientobject.DepartmentTreeInfoCO;
import com.zcloud.gbscommon.utils.Tools;
import lombok.AllArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:03:21
 */
@Service
@AllArgsConstructor
public class DepartmentServiceImpl implements DepartmentServiceI {
    private final DepartmentAddExe departmentAddExe;
    private final DepartmentUpdateExe departmentUpdateExe;
    private final DepartmentRemoveExe departmentRemoveExe;
    private final DepartmentQueryExe departmentQueryExe;

    @Override
    public PageResponse<DepartmentCO> listPage(DepartmentPageQry qry) {

        return departmentQueryExe.execute(qry);
    }

    @Override
    public SingleResponse add(DepartmentAddCmd cmd) {

        departmentAddExe.execute(cmd);
        return SingleResponse.buildSuccess();
    }

    @Override
    public void edit(DepartmentUpdateCmd departmentUpdateCmd) {
        departmentUpdateExe.execute(departmentUpdateCmd);
    }

    @Override
    public void remove(Long id) {
        departmentRemoveExe.execute(id);
    }

    @Override
    public void removeBatch(Long[] ids) {
        departmentRemoveExe.execute(ids);
    }

    @Override
    public List<DepartmentTreeInfoCO> listTree(DepartmentTreeQry qry) {
        return departmentQueryExe.listTree(qry);
    }

    @Override
    public DepartmentInfoCO info(Long id) {
        return departmentQueryExe.info(id);
    }

    @Override
    public List<DepartmentCO> listByDepartIds(Collection<Long> collection) {
         return departmentQueryExe.listByDepartIds(collection);
    }

    @Override
    public List<DepartmentCO> getDeptList() {
        return departmentQueryExe.getDeptList();
    }

    @Override
    public List<DepartmentTreeInfoCO> listAllTree() {
        return departmentQueryExe.listAllTree();
    }

    @Override
    public Collection<DepartmentTreeInfoCO> listAllTreeByCorpType(DepartmentTreeQry qry) {
        return departmentQueryExe.listAllTreeByCorpType(qry);

    }

    @Override
    @Cacheable(value = "deptAllNameCache", key="'deptId:'+#deptId")
    public String getFullNameForList(Long deptId, List<DepartmentCO> list) {
        String name = Tools.getTreeFullName(list,"id", "parentId", "name", deptId);
        return name;
    }

    @Override
    public Collection<DepartmentTreeInfoCO> listTreeNopermission(DepartmentTreeQry qry) {
         return departmentQueryExe.listTreeNopermission(qry);
    }
}

