package com.zcloud.primeport.command.query;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.primeport.command.convertor.PersonApplyCoConvertor;
import com.zcloud.primeport.command.convertor.XgfApplyPersonCoConvertor;
import com.zcloud.primeport.dto.PersonApplyPageQry;
import com.zcloud.primeport.dto.PersonCountPageQry;
import com.zcloud.primeport.dto.XgfApplyPersonPageQry;
import com.zcloud.primeport.dto.clientobject.PersonApplyCO;
import com.zcloud.primeport.dto.clientobject.PersonCountCO;
import com.zcloud.primeport.dto.clientobject.XgfPersonApplyCO;
import com.zcloud.primeport.dto.clientobject.XgfPersonCountCO;
import com.zcloud.primeport.persistence.dataobject.PersonApplyDO;
import com.zcloud.primeport.persistence.dataobject.PersonCountDto;
import com.zcloud.primeport.persistence.dataobject.VehicleApplyDO;
import com.zcloud.primeport.persistence.dataobject.XgfApplyPersonDO;
import com.zcloud.primeport.persistence.repository.PersonApplyRepository;
import com.zcloud.primeport.persistence.repository.XgfApplyPersonRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

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


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:22
 */
@Component
@AllArgsConstructor
public class PersonApplyQueryExe {
    private final PersonApplyRepository personApplyRepository;
    private final XgfApplyPersonRepository xgfPersonApplyRepository;
    private final PersonApplyCoConvertor personApplyCoConvertor;
    private final XgfApplyPersonCoConvertor xgfApplyPersonCoConvertor;

    /**
     * 分页
     *
     * @param personApplyPageQry
     * @return
     */
    public PageResponse<XgfPersonApplyCO> execute(PersonApplyPageQry personApplyPageQry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(personApplyPageQry);
        PageResponse<XgfApplyPersonDO> pageResponse = xgfPersonApplyRepository.listPage(parmas);
        List<XgfPersonApplyCO> examCenterCOS = xgfApplyPersonCoConvertor.converDOsToCOs(pageResponse.getData());
        Long userId = AuthContext.getUserId();
        if (!examCenterCOS.isEmpty()) {
            examCenterCOS.forEach(item -> {
                item.setCurrentUserCanAudit(2);
                if (item.getAuditFlag() == 1) {
                    if (item.getAuditUserId().equals(userId)) {
                        item.setCurrentUserCanAudit(1);
                    }
                }
                if (item.getApplyUserName() == null) {
                    item.setApplyUserName(item.getEmployeePersonUserName());
                }
            });
        }
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public PageResponse<XgfPersonApplyCO> xgfPersonAuditList(XgfApplyPersonPageQry qry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(qry);
        PageResponse<XgfApplyPersonDO> pageResponse = xgfPersonApplyRepository.xgfPersonAuditList(parmas);
        List<XgfPersonApplyCO> examCenterCOS = xgfApplyPersonCoConvertor.converDOsToCOs(pageResponse.getData());
        Long userId = AuthContext.getUserId();
        if (!examCenterCOS.isEmpty()) {
            examCenterCOS.forEach(item -> {
                item.setCurrentUserCanAudit(2);
                if (userId.equals(item.getAuditUserId())) {
                    item.setCurrentUserCanAudit(1);
                }
            });
        }
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public PageResponse<XgfPersonApplyCO> xgfPersonApplyList(XgfApplyPersonPageQry qry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(qry);
//        parmas.put("applyCorpId", AuthContext.getTenantId());
        PageResponse<XgfApplyPersonDO> pageResponse = xgfPersonApplyRepository.xgfPersonAuditList(parmas);
        List<XgfPersonApplyCO> examCenterCOS = xgfApplyPersonCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public PageResponse<PersonApplyCO> tmpPersonApplyH5List(XgfApplyPersonPageQry qry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(qry);
        parmas.put("person_belong_type", 4);
        PageResponse<PersonApplyDO> pageResponse = personApplyRepository.listPage(parmas);
        List<PersonApplyCO> examCenterCOS = personApplyCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());

    }

    public PageResponse<PersonCountCO> personnelVehicleManagementList(PersonCountPageQry qry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(qry);
        parmas.put("applyCorpId", qry.getCorpId());
        PageResponse<PersonCountDto> pageResponse = xgfPersonApplyRepository.personnelVehicleManagementList(parmas);
        List<PersonCountCO> examCenterCOS = personApplyCoConvertor.converDtosToCOs(pageResponse.getData());

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

    public PageResponse<PersonApplyCO> xgfPersonPermissionsList(XgfApplyPersonPageQry qry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(qry);
        PageResponse<PersonApplyDO> pageResponse = personApplyRepository.xgfPersonPermissionsList(parmas);
        List<PersonApplyCO> examCenterCOS = personApplyCoConvertor.converDOsToCOs(pageResponse.getData());

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

    public PageResponse<XgfPersonCountCO> xgfPersonnelVehicleManagementList(PersonCountPageQry qry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(qry);
        PageResponse<PersonApplyDO> pageResponse = personApplyRepository.xgfPersonnelVehicleManagementList(parmas);
        List<XgfPersonCountCO> examCenterCOS = personApplyCoConvertor.converDOsToXgfCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());

    }

    public SingleResponse<PersonApplyCO> xgfPersonPermissionsInfo(Long id) {
        return SingleResponse.of(personApplyCoConvertor.converDOToCO(personApplyRepository.xgfPersonPermissionsInfo(id)));
    }

    public PersonApplyCO getInfoByUserId(Long userId) {
        QueryWrapper<PersonApplyDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("employee_person_user_id", userId);

        return personApplyCoConvertor.converDOToCO(personApplyRepository.getInfoByUserId(userId));
    }
}

