package com.zcloud.primeport.command.query;

import com.alibaba.cola.dto.MultiResponse;
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.primeport.command.convertor.ClosedAreaPersonApplyCoConvertor;
import com.zcloud.primeport.command.convertor.ClosedAreaPersonApplyDeptCoConvertor;
import com.zcloud.primeport.dto.ClosedAreaPersonApplyAuthCmd;
import com.zcloud.primeport.dto.ClosedAreaPersonApplyPageQry;
import com.zcloud.primeport.dto.clientobject.ClosedAreaPersonApplyCO;
import com.zcloud.primeport.dto.clientobject.ClosedAreaPersonApplyDeptCO;
import com.zcloud.primeport.dto.clientobject.UserCo;
import com.zcloud.primeport.persistence.dataobject.*;
import com.zcloud.primeport.persistence.repository.ClosedAreaFgsPersonAreaRepository;
import com.zcloud.primeport.persistence.repository.ClosedAreaFgsPersonAuthRepository;
import com.zcloud.primeport.persistence.repository.ClosedAreaPersonApplyRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.alibaba.cola.dto.PageResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

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


/**
 * web-app
 *
 * @Author dearLin
 * @Date 2026-03-19 13:59:16
 */
@Component
@AllArgsConstructor
public class ClosedAreaPersonApplyQueryExe {
    private final ClosedAreaPersonApplyRepository closedAreaPersonApplyRepository;
    private final ClosedAreaFgsPersonAuthRepository closedAreaFgsPersonAuthRepository;
    private final ClosedAreaFgsPersonAreaRepository closedAreaFgsPersonAreaRepository;
    private final ClosedAreaPersonApplyCoConvertor closedAreaPersonApplyCoConvertor;
    private final ClosedAreaPersonApplyDeptCoConvertor closedAreaPersonApplyDeptCoConvertor;

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public ClosedAreaPersonApplyCO queryById(Long id) {
        return closedAreaPersonApplyCoConvertor.converDOToCO(closedAreaPersonApplyRepository.getById(id));
    }

    /**
     * 获取全部部门
     *
     * @return
     */
    public MultiResponse <ClosedAreaPersonApplyDeptCO> obtainDeptList() {

        List <ClosedAreaPersonApplyDeptDO> closedAreaPersonApplyDeptDOs = closedAreaPersonApplyRepository.deptList();

        return MultiResponse.of(closedAreaPersonApplyDeptCoConvertor.converDOsToCOs(closedAreaPersonApplyDeptDOs));
    }

    /**
     * 分页
     *
     * @param closedAreaPersonApplyPageQry
     * @return
     */
    public PageResponse<ClosedAreaPersonApplyCO> execute(ClosedAreaPersonApplyPageQry closedAreaPersonApplyPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(closedAreaPersonApplyPageQry);
        PageResponse<ClosedAreaPersonApplyDO> pageResponse = closedAreaPersonApplyRepository.listPage(params);
        List<ClosedAreaPersonApplyCO> examCenterCOS = closedAreaPersonApplyCoConvertor.converDOsToCOs(pageResponse.getData());
        
        // 获取当前登录用户ID
        Long currentUserId = AuthContext.getUserId();
        
        // 设置当前用户是否能审核
        for (ClosedAreaPersonApplyCO co : examCenterCOS) {
            // 审核中(1)且当前用户是审批人 → 能审核(1)，否则不能审核(2)
            if (co.getAuditFlag() != null && co.getAuditFlag() == 1 
                    && co.getAuditPersonUserId() != null 
                    && co.getAuditPersonUserId().equals(currentUserId)) {
                co.setCurrentUserCanAudit(1);
            } else {
                co.setCurrentUserCanAudit(2);
            }
            if (co.getApplyUserName() == null) {
                co.setApplyUserName(co.getApplyPersonUserName());
            }
        }
        
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public PageResponse<UserCo> getCorpUserList(ClosedAreaPersonApplyPageQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        params.put("corpinfoId", AuthContext.getTenantId());
        params.put("selfUserId", AuthContext.getUserId());
        PageResponse<UserDto> pageResponse = closedAreaPersonApplyRepository.getCorpUserList(params);
        List<UserCo> examCenterCOS = closedAreaPersonApplyCoConvertor.converUserDtosToUserCos(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());

    }

    public SingleResponse<ClosedAreaPersonApplyCO> getAuthorizationPersonInfo(Long id) {
        ClosedAreaFgsPersonAuthDO closedAreaFgsPersonAuthDO = closedAreaFgsPersonAuthRepository.getAuthorizationPersonInfo(id);
        QueryWrapper<ClosedAreaFgsPersonAreaDO> closedAreaFgsPersonAreaDOQueryWrapper = new QueryWrapper<>();
        closedAreaFgsPersonAreaDOQueryWrapper.eq("fgs_person_auth_id",id);
        List<ClosedAreaFgsPersonAreaDO> list = closedAreaFgsPersonAreaRepository.list(closedAreaFgsPersonAreaDOQueryWrapper);
        ClosedAreaPersonApplyCO closedAreaPersonApplyCO = new ClosedAreaPersonApplyCO();
        closedAreaPersonApplyCO.setApplyPersonUserId(id);
        if (closedAreaFgsPersonAuthDO != null) {
            ArrayList<ClosedAreaPersonApplyAuthCmd.FgsPersonArea> fgsPersonAreas = new ArrayList<>();
            closedAreaPersonApplyCO.setMkmjPermission(closedAreaFgsPersonAuthDO.getMkmjPermission());
            for (ClosedAreaFgsPersonAreaDO closedAreaFgsPersonAreaDO : list) {
                ClosedAreaPersonApplyAuthCmd.FgsPersonArea fgsPersonArea = new ClosedAreaPersonApplyAuthCmd.FgsPersonArea();
                fgsPersonArea.setFgsPersonAuthId(closedAreaFgsPersonAreaDO.getFgsPersonAuthId());
                fgsPersonArea.setClosedAreaId(closedAreaFgsPersonAreaDO.getClosedAreaId());
                fgsPersonArea.setClosedAreaName(closedAreaFgsPersonAreaDO.getClosedAreaName());
                fgsPersonAreas.add(fgsPersonArea);
            }
            closedAreaPersonApplyCO.setAreaList(fgsPersonAreas);
        }
        return SingleResponse.of(closedAreaPersonApplyCO);
    }
}

