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

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.basic.info.command.convertor.UserCorpRecordCoConvertor;
import com.zcloud.basic.info.command.convertor.UserJobHandoverCoConvertor;
import com.zcloud.basic.info.domain.enums.UserChangeRecordStatusEnum;
import com.zcloud.basic.info.dto.UserChangeRecordInfoQry;
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.dataobject.UserJobHandoverDO;
import com.zcloud.basic.info.persistence.repository.UserCorpRecordRepository;
import com.zcloud.basic.info.persistence.repository.UserJobHandoverRepository;
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;
import java.util.stream.Collectors;


/**
 * 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;
    private final UserJobHandoverRepository userJobHandoverRepository;
    private final UserJobHandoverCoConvertor userJobHandoverCoConvertor;
    /**
     * 分页
     *
     * @param userCorpRecordPageQry
     * @return
     */
    public PageResponse<UserCorpRecordCO> execute(UserCorpRecordPageQry userCorpRecordPageQry) {

        Long userId = userCorpRecordPageQry.getEqUserId() == null ? AuthContext.getUserId() : userCorpRecordPageQry.getEqUserId();
        UserDO userDO = userRepository.getById(userId);
        if(userDO == null){
            return PageResponse.of(null, 0, userCorpRecordPageQry.getPageSize(), userCorpRecordPageQry.getPageIndex());
        }

        List<UserDO> userDOList = userRepository.getListByPhone(userDO.getPhone(),null);
        //获取id list
        List<Long> userIdList = userDOList.stream().map(UserDO::getId).collect(Collectors.toList());
        userCorpRecordPageQry.setEqUserId(null);
        Map<String, Object> params = PageQueryHelper.toHashMap(userCorpRecordPageQry);
        params.put("inUserId", userIdList);
        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;
        }

        //补充用户信息
        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());
        userCO.setFlowFlag(userCorpRecordDO.getFlowFlag());
        return userCO;
    }

    public UserCorpRecordCO getChangerRecordInfoById(UserChangeRecordInfoQry userChangeRecordInfoQry) {
        if(userChangeRecordInfoQry.getCorpinfoId() == null){
            userChangeRecordInfoQry.setCorpinfoId(AuthContext.getTenantId());
        }

        UserCorpRecordDO userCorpRecordDO = userCorpRecordRepository.getUserCorpRecordInfoByUserStatus(userChangeRecordInfoQry.getUserId(), userChangeRecordInfoQry.getCorpinfoId(), UserChangeRecordStatusEnum.PENDING.getCode());


        if(userCorpRecordDO == null){
            return null;
        }
        UserCorpRecordCO co = new UserCorpRecordCO();
        BeanUtils.copyProperties(userCorpRecordDO, co);

        //组装未完成的工作
        List<UserJobHandoverDO> userJobHandoverDOList = userJobHandoverRepository.getListByUserId(userChangeRecordInfoQry.getUserId(), userChangeRecordInfoQry.getCorpinfoId());
        if(CollUtil.isNotEmpty(userJobHandoverDOList))
            co.setUserJobHandoverCOList(userJobHandoverCoConvertor.converDOsToCOs(userJobHandoverDOList));
        return co;


    }
}

