package com.zcloud.basic.info.gatewayimpl;

import cn.hutool.json.JSONUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.system.client.dept.facade.DeptFacade;
import com.jjb.saas.system.client.dept.request.DeptAddCmd;
import com.jjb.saas.system.client.dept.response.DeptDetailCO;
import com.zcloud.basic.info.domain.gateway.DepartmentGateway;
import com.zcloud.basic.info.domain.model.DepartmentE;
import com.zcloud.basic.info.persistence.dataobject.DepartmentDO;
import com.zcloud.basic.info.persistence.repository.DepartmentRepository;
import com.zcloud.gbscommon.utils.Tools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:03:20
 */
@Service
@AllArgsConstructor
@Slf4j
public class DepartmentGatewayImpl implements DepartmentGateway {
    private final DepartmentRepository departmentRepository;
    @DubboReference
    private DeptFacade deptFacade;
    @Override
    public Long add(DepartmentE departmentE) {
        //TODO 父id的值待定
        if(departmentE.getParentId()==null){
            departmentE.setParentId(AuthContext.getTenantId());
        }
        if(departmentE.getCorpinfoId() == null){
            departmentE.setCorpinfoId(AuthContext.getTenantId());
        }
        DepartmentDO d = new DepartmentDO();
        BeanUtils.copyProperties(departmentE, d);
        if(StringUtils.isEmpty(d.getDepartmentId())){
            d.setDepartmentId(Tools.get32UUID());
        }

        SSOUser ssoUser = AuthContext.getCurrentUser();
        System.out.println(ssoUser.getTenantId());
        SingleResponse<DeptDetailCO> deptDetailCO = deptFacade.getDetail(departmentE.getParentId());
        // 需要调用GBS，新增
        DeptAddCmd deptCmd = new DeptAddCmd();
        deptCmd.setDeptName(departmentE.getName());
        deptCmd.setParentIds(deptDetailCO.getData().getParentIds());
        deptCmd.setAddTypeEnum("MANUAL");
        if(departmentE.getId()!=null){
            deptCmd.setId(departmentE.getId());
        }
        //TODO gbs部门表 sys_dept tenant_id，parent_ids，add_type_enum，存储错误，导致gbs页面看不到，参数还没有，待定
        // TODO gbs的这个创建部门接口没有区分企业, 新建的企业创建其他企业有的部门名称时报错:名称已存在
        log.info("DepartmentAddExe,新增部门调用GBS请求："+ JSONUtil.toJsonStr(deptCmd));
        SingleResponse<Long>  response = deptFacade.add(deptCmd);
        d.setId(response.getData());
        departmentRepository.save(d);
        log.info("DepartmentAddExe,新增部门调用GBS返回："+response.toString());
        return d.getId();
    }

    @Override
    public Boolean update(DepartmentE departmentE) {
        DepartmentDO d = new DepartmentDO();
        BeanUtils.copyProperties(departmentE, d);
        departmentRepository.updateInfoById(d);
        return true;
    }

    @Override
    public Boolean deletedDepartmentById(Long id) {
        return departmentRepository.removeById(id);
    }

    @Override
    public Boolean deletedDepartmentByIds(Long[] ids) {
        return departmentRepository.removeByIds(Collections.singletonList(ids));
    }
}

