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.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.basic.info.command.convertor.UserChangeRecordCoConvertor;
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.UserChangeRecordPageQry;
import com.zcloud.basic.info.dto.UserChangeRecordListQry;
import com.zcloud.basic.info.dto.clientobject.UserChangeRecordCO;
import com.zcloud.basic.info.persistence.dataobject.UserChangeRecordDO;
import com.zcloud.basic.info.persistence.dataobject.UserJobHandoverDO;
import com.zcloud.basic.info.persistence.repository.UserChangeRecordRepository;
import com.zcloud.basic.info.persistence.repository.UserJobHandoverRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import lombok.AllArgsConstructor;
import org.apache.catalina.User;
import org.springframework.stereotype.Component;
import org.springframework.beans.BeanUtils;

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


/**
 * web-app
 *
 * @Author SondonYong
 * @Date 2025-12-02 14:08:19
 */
@Component
@AllArgsConstructor
public class UserChangeRecordQueryExe {
    private final UserChangeRecordRepository userChangeRecordRepository;
    private final UserChangeRecordCoConvertor userChangeRecordCoConvertor;
    private final UserJobHandoverRepository userJobHandoverRepository;
    private final UserJobHandoverCoConvertor userJobHandoverCoConvertor;

    /**
     * 分页
     *
     * @return
     */
    public PageResponse<UserChangeRecordCO> execute(UserChangeRecordPageQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<UserChangeRecordDO> pageResponse = userChangeRecordRepository.listPage(params);
        List<UserChangeRecordCO> examCenterCOS = userChangeRecordCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public MultiResponse<UserChangeRecordCO> execute(UserChangeRecordListQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        List<UserChangeRecordDO> list = userChangeRecordRepository.list(params);
        List<UserChangeRecordCO> examCenterCOS = userChangeRecordCoConvertor.converDOsToCOs(list);
        return MultiResponse.of(examCenterCOS);
    }

    public SingleResponse<UserChangeRecordCO> execute(Long id) {
        SingleResponse<UserChangeRecordDO> userChangeRecordDO = userChangeRecordRepository.getInfoById(id);
        UserChangeRecordCO co = new UserChangeRecordCO();
        BeanUtils.copyProperties(userChangeRecordDO.getData(), co);
        return SingleResponse.of(co);
    }

    public UserChangeRecordCO getChangerRecordInfoById(UserChangeRecordInfoQry userChangeRecordInfoQry) {
        QueryWrapper<UserChangeRecordDO> queryWrapper = new QueryWrapper<>();
        if(userChangeRecordInfoQry.getCorpinfoId() == null){
            userChangeRecordInfoQry.setCorpinfoId(AuthContext.getTenantId());
        }

        UserChangeRecordDO userChangeRecordDO =userChangeRecordRepository.getChangerRecordInfoById(userChangeRecordInfoQry.getUserId(), userChangeRecordInfoQry.getCorpinfoId(),UserChangeRecordStatusEnum.PENDING.getCode());


        if(userChangeRecordDO == null){
            return null;
        }
        UserChangeRecordCO co = new UserChangeRecordCO();
        BeanUtils.copyProperties(userChangeRecordDO, co);

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


    }
}

