package com.zcloud.basic.info.facade;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.zcloud.basic.info.api.PostServiceI;
import com.zcloud.basic.info.command.convertor.PostCoConvertor;
import com.zcloud.basic.info.dto.PostListQry;
import com.zcloud.basic.info.dto.clientobject.PostCO;
import com.zcloud.gbscommon.utils.DeepCopyUtil;
import com.zcloud.gbscommon.zcloudpost.facade.ZcloudPostFacade;
import com.zcloud.gbscommon.zcloudpost.request.ZcloudPostQry;
import com.zcloud.gbscommon.zcloudpost.response.ZcloudPostCO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author SondonYong
 * @description 岗位管理对其他领域dubbo接口实现
 * @date 2025/11/1/周六 15:28
 */
@DubboService
public class ZcloudPostFacadeImpl implements ZcloudPostFacade {

    @Resource
    private PostServiceI postService;
    @Resource
    private PostCoConvertor postCoConvertor;


    @Override
    public SingleResponse<ZcloudPostCO> listByPostId(ZcloudPostQry qry){
        if(Objects.isNull(qry) || Objects.isNull(qry.getPostId())){
            return null;
        }
        SingleResponse<PostCO> postCO = postService.getInfoById(qry.getPostId());
        if(Objects.isNull(postCO) || Objects.isNull(postCO.getData())){
            return null;
        }

        // 定义映射关系
        Map<Class<?>, Class<?>> mapping = new HashMap<>();
//        mapping.put(PostDepartmentCO.class, ZcloudPostDepartmentCO.class);

        // 单个对象
        ZcloudPostCO zcloudPostCO = DeepCopyUtil.deepCopy(postCO.getData(), ZcloudPostCO.class, mapping);

        return SingleResponse.of(zcloudPostCO);
    }

    @Override
    public MultiResponse<ZcloudPostCO> listByPostIds(ZcloudPostQry qry){

        if(qry == null || qry.getPostIds() == null || qry.getPostIds().length == 0){
            return null;
        }

        PostListQry postListQry = new PostListQry();
        BeanUtils.copyProperties(qry, postListQry);
        MultiResponse<PostCO> list = postService.list(postListQry);

        if(Objects.isNull(list) || Objects.isNull(list.getData())){
            return null;
        }

        // 定义映射关系
        Map<Class<?>, Class<?>> mapping = new HashMap<>();
//        mapping.put(PostDepartmentCO.class, ZcloudPostDepartmentCO.class);

        // 集合
        List<ZcloudPostCO> zcloudPostCOList = DeepCopyUtil.copyList(list.getData(), ZcloudPostCO.class, mapping);

        return MultiResponse.of(zcloudPostCOList);
    }

    @Override
    public MultiResponse<ZcloudPostCO> listByPostIds(Collection<Long> collection) {
        List<PostCO> postCOS = postService.listByPostIds(collection);
        List<ZcloudPostCO> zcloudPostCo = postCoConvertor.converCOsToDubboCOs(postCOS);
        return MultiResponse.of(zcloudPostCo);
    }

    @Override
    public MultiResponse<ZcloudPostCO> getPostList() {
        List<PostCO> list = postService.getPostList();
        List<ZcloudPostCO> zcloudUserCo = postCoConvertor.converCOsToDubboCOs(list);
        return MultiResponse.of(zcloudUserCo);
    }

    @Override
    public SingleResponse<ZcloudPostCO> getPostById(Long aLong) {
        PostCO postCO =  postService.getPostById(aLong);
        ZcloudPostCO zcloudPostCO = postCoConvertor.converCOToDubboCO(postCO);
        return SingleResponse.of(zcloudPostCO);
    }
}
