package com.zcloud.basic.info.command.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcloud.basic.info.command.convertor.DepartmentCoConvertor;
import com.zcloud.basic.info.command.convertor.PostCoConvertor;
import com.zcloud.basic.info.dto.PostListQry;
import com.zcloud.basic.info.dto.PostPageQry;
import com.zcloud.basic.info.dto.clientobject.DepartmentCO;
import com.zcloud.basic.info.dto.clientobject.PostCO;
import com.zcloud.basic.info.persistence.dataobject.DepartmentDO;
import com.zcloud.basic.info.persistence.dataobject.PostDO;
import com.zcloud.basic.info.persistence.repository.DepartmentRepository;
import com.zcloud.basic.info.persistence.repository.PostRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;


/**
 * web-app
 *
 * @Author SondonYong
 * @Date 2025-10-31 17:24:04
 */
@Component
@AllArgsConstructor
public class PostQueryExe {
    private final PostRepository postRepository;
    private final DepartmentRepository departmentRepository;
    private final DepartmentCoConvertor departmentCoConvertor;
    private final PostCoConvertor postCoConvertor;

    /**
     * 分页
     *
     * @param postPageQry
     * @return
     */
    public PageResponse<PostCO> execute(PostPageQry postPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(postPageQry);
        PageResponse<PostDO> pageResponse = postRepository.listPage(params);

        if (pageResponse == null || CollUtil.isEmpty(pageResponse.getData())) {
            return PageResponse.of(new ArrayList<>(), 0, 0, 0);
        }

        Long[] departmentIds = pageResponse.getData().stream().map(PostDO::getDepartmentId).toArray(Long[]::new);
        List<DepartmentDO> departmentDOList = departmentRepository.listByIds(Arrays.asList(departmentIds));

        // Map<部门id, 部门名称>
        Map<Long, String> departmentMap = new HashMap<>();
        if (CollUtil.isNotEmpty(departmentDOList)) {
            List<DepartmentCO> departmentCOList = departmentCoConvertor.converDOsToCOs(departmentDOList);
            departmentMap = departmentCOList.stream().collect(Collectors.toMap(DepartmentCO::getId, DepartmentCO::getName));
        }

        List<PostCO> postCOList = postCoConvertor.converDOsToCOs(pageResponse.getData());
        if (CollUtil.isNotEmpty(postCOList)) {
            for (PostCO postCO : postCOList) {
                if (CollUtil.isNotEmpty(departmentMap)) {
                    postCO.setDepartmentName(departmentMap.get(postCO.getDepartmentId()));
                }
            }
        }

        return PageResponse.of(postCOList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    /**
     * 查询集合
     *
     * @param qry
     * @return
     */
    public MultiResponse<PostCO> execute(PostListQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);

        // 岗位集合
        MultiResponse<PostDO> postMRDO = postRepository.list(params);
        if (postMRDO == null || CollUtil.isEmpty(postMRDO.getData())) {
            return new MultiResponse<>();
        }
        Long[] departmentIds = postMRDO.getData().stream().map(PostDO::getDepartmentId).toArray(Long[]::new);
        List<DepartmentDO> departmentDOList = departmentRepository.listByIds(Arrays.asList(departmentIds));

        // Map<部门id, 部门名称>
        Map<Long, String> departmentMap = new HashMap<>();
        if (CollUtil.isNotEmpty(departmentDOList)) {
            List<DepartmentCO> departmentCOList = departmentCoConvertor.converDOsToCOs(departmentDOList);
            departmentMap = departmentCOList.stream().collect(Collectors.toMap(DepartmentCO::getId, DepartmentCO::getName));
        }

        // 赋值
        List<PostDO> postDOList = postMRDO.getData();
        List<PostCO> postCOList = postCoConvertor.converDOsToCOs(postDOList);
        if (CollUtil.isNotEmpty(postCOList)) {
            for (PostCO postCO : postCOList) {
                if (CollUtil.isNotEmpty(departmentMap)) {
                    postCO.setDepartmentName(departmentMap.get(postCO.getDepartmentId()));
                }
            }
        }

        return MultiResponse.of(postCOList);
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    public SingleResponse<PostCO> execute(Long id) {
        SingleResponse<PostDO> postDO = postRepository.getInfoById(id);
        PostCO co = new PostCO();
        BeanUtils.copyProperties(postDO.getData(), co);

        // 查询部门名称
        DepartmentDO departmentDO = departmentRepository.getById(postDO.getData().getDepartmentId());
        if(departmentDO != null) {
            co.setDepartmentName(departmentDO.getName());
        }

        return SingleResponse.of(co);
    }

    public List<PostCO> listByPostIds(Collection<Long> collection) {
        QueryWrapper<PostDO> dOQueryWrapper = new QueryWrapper<>();
        dOQueryWrapper.in("id", collection);
        List<PostDO> list = postRepository.list(dOQueryWrapper);
        return postCoConvertor.converDOsToCOs(list);

    }

    public List<PostCO> getPostList() {
        List<PostDO> list = postRepository.list();
        return postCoConvertor.converDOsToCOs(list);
    }

    public PostCO getPostById(Long aLong) {
        PostDO data = postRepository.getById(aLong);
        return postCoConvertor.converDOToCO(data);
    }
}

