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

import com.alibaba.cola.dto.PageResponse;
import com.zcloud.basic.info.command.convertor.UserCorpRecordCoConvertor;
import com.zcloud.basic.info.dto.UserCorpRecordPageQry;
import com.zcloud.basic.info.dto.clientobject.UserCO;
import com.zcloud.basic.info.dto.clientobject.UserCorpRecordCO;
import com.zcloud.basic.info.persistence.dataobject.UserCorpRecordDO;
import com.zcloud.basic.info.persistence.dataobject.UserDO;
import com.zcloud.basic.info.persistence.repository.UserCorpRecordRepository;
import com.zcloud.basic.info.persistence.repository.UserRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-12-03 10:21:59
 */
@Component
@AllArgsConstructor
public class UserCorpRecordQueryExe {
    private final UserCorpRecordRepository userCorpRecordRepository;
    private final UserCorpRecordCoConvertor userCorpRecordCoConvertor;
    private final UserRepository userRepository;

    /**
     * 分页
     *
     * @param userCorpRecordPageQry
     * @return
     */
    public PageResponse<UserCorpRecordCO> execute(UserCorpRecordPageQry userCorpRecordPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(userCorpRecordPageQry);
        PageResponse<UserCorpRecordDO> pageResponse = userCorpRecordRepository.listPage(params);
        List<UserCorpRecordCO> examCenterCOS = userCorpRecordCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public UserCO getInfoById(Long id) {
        UserCorpRecordDO userCorpRecordDO = userCorpRecordRepository.getById(id);
        if (userCorpRecordDO == null) {
            return null;
        }

        UserCorpRecordCO userCorpRecordCO = userCorpRecordCoConvertor.convertDOToCO(userCorpRecordDO);
        //补充用户信息
        Long userId = userCorpRecordDO.getUserId();
        UserDO userDO = userRepository.getInfoById(userId);
        UserCO userCO = new UserCO();
        BeanUtils.copyProperties(userDO, userCO);
        userCO.setCorpinfoId(userCorpRecordDO.getCorpinfoId());
        userCO.setCorpinfoName(userCorpRecordDO.getCorpinfoName());
        userCO.setDepartmentId(userCorpRecordDO.getDepartmentId());
        userCO.setDepartmentName(userCorpRecordDO.getDepartmentName());
        userCO.setPostId(userCorpRecordDO.getPostId());
        userCO.setPostName(userCorpRecordDO.getPostName());
        return userCO;
    }
}

