package com.zcloud.basic.info.persistence.repository.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.jjb.saas.system.client.dept.facade.DeptFacade;
import com.jjb.saas.system.client.dept.request.DeptUpdateCmd;
import com.zcloud.basic.info.domain.enums.MenuEnum;
import com.zcloud.basic.info.domain.model.DepartmentE;
import com.zcloud.basic.info.persistence.dataobject.CorpInfoDO;
import com.zcloud.basic.info.persistence.dataobject.DepartmentDO;
import com.zcloud.basic.info.persistence.mapper.DepartmentMapper;
import com.zcloud.basic.info.persistence.repository.DepartmentRepository;
import com.zcloud.basic.info.utils.Query;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Tools;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:03:20
 */
@Service
@RequiredArgsConstructor
public class DepartmentRepositoryImpl extends BaseRepositoryImpl<DepartmentMapper, DepartmentDO> implements DepartmentRepository {
    private final DepartmentMapper departmentMapper;
    @DubboReference
    private DeptFacade deptFacade;

    @Override
    public PageResponse<DepartmentDO> listPage(Map<String, Object> parmas) {
        IPage<DepartmentDO> iPage = new Query<DepartmentDO>().getPage(parmas);
        QueryWrapper<DepartmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, parmas);
        queryWrapper.orderByAsc("dep_order").orderByDesc("create_time");
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(parmas.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(parmas.get("menuPath").toString());
        }
        queryWrapper.eq("delete_enum", "FALSE");
        IPage<DepartmentDO> result = departmentMapper.selectDeptPage(iPage, queryWrapper, menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public List<DepartmentDO> listTree(Map<String, Object> parmas) {
        QueryWrapper<DepartmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, parmas);
        queryWrapper.orderByAsc("dep_order").orderByDesc("create_time");
        List<DepartmentDO> departmentDOList = departmentMapper.selectList(queryWrapper);
        if (CollUtil.isEmpty(departmentDOList)) {
            return Collections.emptyList();
        }
      return departmentDOList;
    }

    @Override
    public List<DepartmentDO> listByParams(Map<String, Object> parmas) {
        IPage<DepartmentDO> iPage = new Query<DepartmentDO>().getPage(parmas);
        QueryWrapper<DepartmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, parmas);
        queryWrapper.orderByAsc("dep_order").orderByDesc("create_time");
        List<DepartmentDO> departmentDOList = departmentMapper.selectList(queryWrapper);
        return departmentDOList;
    }

    @Override
    public Long countByParams(Map<String, Object> parmas) {
        IPage<DepartmentDO> iPage = new Query<DepartmentDO>().getPage(parmas);
        QueryWrapper<DepartmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, parmas);
        queryWrapper.orderByAsc("dep_order").orderByDesc("create_time");
        return departmentMapper.selectCount(queryWrapper);
    }

    @Override
    public Long getCountByCorpInfoId(Long id) {
        QueryWrapper<DepartmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corpinfo_id", id);
        return departmentMapper.selectCount(queryWrapper);
    }

    @Override
    public Long addCorpDepartment(DepartmentE departmentE) {
        DepartmentDO departmentDO = new DepartmentDO();
        BeanUtils.copyProperties(departmentE, departmentDO);
        if(departmentDO.getDepartmentId()==null){
            departmentDO.setDepartmentId(Tools.get32UUID());
        }
        departmentMapper.insert(departmentDO);
        return departmentDO.getId();
    }

    @Override
    public List<DepartmentDO> listByCorpInfoId(Long corpInfoId) {
        QueryWrapper<DepartmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corpinfo_id", corpInfoId);
        queryWrapper.eq("delete_enum", "FALSE");
        return list(queryWrapper);
    }

    @Override
    public void updateInfoById(DepartmentDO departmentDO) {
        updateById(departmentDO);
        if (!ObjectUtils.isEmpty(departmentDO.getName())){
            DeptUpdateCmd deptUpdateCmd = new DeptUpdateCmd();
            deptUpdateCmd.setId(departmentDO.getId());
            deptUpdateCmd.setDeptName(departmentDO.getName());
            deptUpdateCmd.setVersion(1);
            deptFacade.update(deptUpdateCmd);
        }
    }

    @Override
    public List<DepartmentDO> listAllTree() {
       return departmentMapper.listAllTree();
    }

    @Override
    public List<DepartmentDO> listAllTreeByCorpType(Map<String, Object> parmas) {
        return departmentMapper.listAllTreeByCorpType(parmas);
    }

    @Override
    public boolean existsByName(Long parentId,String name, Long id) {
        QueryWrapper<DepartmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("parent_id", parentId);
        queryWrapper.eq("delete_enum", "FALSE");
        if (!ObjectUtils.isEmpty(id)){
            queryWrapper.ne("id", id);
        }
        return departmentMapper.selectCount(queryWrapper) > 0;

    }
    @Override
    public void deleteByCorpInfoId(Long corpInfoId) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("corpinfo_id", corpInfoId);
        updateWrapper.set("delete_enum", "TRUE");
        update(updateWrapper);
    }
}

