package com.zcloud.primeport.command.query;

import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.primeport.command.convertor.MkmjApprovalUserCoConvertor;
import com.zcloud.primeport.dto.MkmjApprovalUserPageQry;
import com.zcloud.primeport.dto.clientobject.MkmjApprovalUserCO;
import com.zcloud.primeport.persistence.dataobject.MkmjApprovalUserDO;
import com.zcloud.primeport.persistence.repository.MkmjApprovalUserRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:17
 */
@Component
@AllArgsConstructor
public class MkmjApprovalUserQueryExe {
    private final MkmjApprovalUserRepository mkmjApprovalUserRepository;
    private final MkmjApprovalUserCoConvertor mkmjApprovalUserCoConvertor;

    /**
     * 分页
     *
     * @param mkmjApprovalUserPageQry
     * @return
     */
    public PageResponse<MkmjApprovalUserCO> execute(MkmjApprovalUserPageQry mkmjApprovalUserPageQry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(mkmjApprovalUserPageQry);
        PageResponse<MkmjApprovalUserDO> pageResponse = mkmjApprovalUserRepository.listPage(parmas);
        List<MkmjApprovalUserCO> examCenterCOS = mkmjApprovalUserCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public List<MkmjApprovalUserCO> listAll(MkmjApprovalUserPageQry qry) {
        QueryWrapper<MkmjApprovalUserDO> mkmjApprovalUserCOQueryWrapper = new QueryWrapper<>();
        if (qry.getNeedSelfCorpApprovalUserFlag() != null) {
            mkmjApprovalUserCOQueryWrapper.eq("corp_id", AuthContext.getTenantId());
        }
        if (qry.getCorpId() != null) {
            mkmjApprovalUserCOQueryWrapper.eq("corp_id", qry.getCorpId());
        }
        if (qry.getDeptId() != null) {
            mkmjApprovalUserCOQueryWrapper.eq("dept_id", qry.getDeptId());
        }
        if (qry.getPersonnelPermissionFlag() != null) {
            mkmjApprovalUserCOQueryWrapper.eq("personnel_permission_flag", qry.getPersonnelPermissionFlag());
        }
        if (qry.getVehiclePermissionFlag() != null) {
            mkmjApprovalUserCOQueryWrapper.eq("vehicle_permission_flag", qry.getVehiclePermissionFlag());
        }
        if (qry.getTemporaryPermissionFlag() != null) {
            mkmjApprovalUserCOQueryWrapper.eq("temporary_permission_flag", qry.getTemporaryPermissionFlag());

        }
        List<MkmjApprovalUserDO> dataList = mkmjApprovalUserRepository.list(mkmjApprovalUserCOQueryWrapper);

        // 去重
        // 保留第一次出现的 userId
        List<MkmjApprovalUserDO> distinctList = new ArrayList <>(
                dataList.stream()
                        .collect(Collectors.toMap(
                                MkmjApprovalUserDO::getUserId,   // key：去重依据 userId
                                obj -> obj,  // value：当前对象
                                (existing, replacement) -> existing, // 重复时保留旧对象（第一条）
                                LinkedHashMap::new  // 保证原有顺序
                        ))
                        .values()
        );

        return mkmjApprovalUserCoConvertor.converDOsToCOs(distinctList);
    }

    public MkmjApprovalUserCO getInfoById(Long id) {
        MkmjApprovalUserDO byId = mkmjApprovalUserRepository.getById(id);
        return mkmjApprovalUserCoConvertor.converDOToCO(byId);
    }

    public List<MkmjApprovalUserDO> getByUserId(Long userId) {
        QueryWrapper<MkmjApprovalUserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("delete_enum", "FALSE");
        return mkmjApprovalUserRepository.list(wrapper);
    }

    public static void main(String[] args) {

        for (int i = 0; i < 30; i++) {
            DefaultIdentifierGenerator x = new DefaultIdentifierGenerator();
            Long l = x.nextId(new Object());
            System.out.println("0,2039146629018386432,"+l);
        }
    }
}
