package com.zcloud.basic.info.persistence.repository.impl;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.zcloud.basic.info.persistence.dataobject.UserQualificationInfoDO;
import com.zcloud.basic.info.persistence.mapper.UserQualificationInfoMapper;
import com.zcloud.basic.info.persistence.repository.UserQualificationInfoRepository;
import com.alibaba.cola.dto.PageResponse;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.gbscommon.utils.Tools;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
* web-infrastructure
* @Author SondonYong
* @Date 2025-10-31 09:50:42
*/
@Service
@RequiredArgsConstructor
public class UserQualificationInfoRepositoryImpl extends BaseRepositoryImpl<UserQualificationInfoMapper, UserQualificationInfoDO> implements UserQualificationInfoRepository {
    private final UserQualificationInfoMapper userQualificationInfoMapper;

    @Override
    public  PageResponse<UserQualificationInfoDO> listPage(Map<String,Object> params) {
        if(params.get("eqCorpinfoId") == null){
            params.put("eqCorpinfoId", AuthContext.getTenantId());
        }
        IPage<UserQualificationInfoDO> iPage = new Query<UserQualificationInfoDO>().getPage(params);
        QueryWrapper<UserQualificationInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params, "a.");
        queryWrapper.eq("a.delete_enum", "FALSE");
        queryWrapper.orderByDesc("a.create_time");
        IPage<UserQualificationInfoDO> result = userQualificationInfoMapper.selectQualificationInfoPage(iPage, queryWrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public MultiResponse<UserQualificationInfoDO> list(Map<String, Object> params) {
        if(params.get("eqCorpinfoId") == null){
            params.put("eqCorpinfoId", AuthContext.getTenantId());
        }
        QueryWrapper<UserQualificationInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params, "a.");
        queryWrapper.eq("a.delete_enum", "FALSE");
        queryWrapper.orderByDesc("a.create_time");
        List<UserQualificationInfoDO> result = userQualificationInfoMapper.selectQualificationInfoList(queryWrapper);
        return MultiResponse.of(result);
    }

    @Override
    public SingleResponse<UserQualificationInfoDO> getInfoById(Long id) {
        return SingleResponse.of(userQualificationInfoMapper.selectById(id));
    }

    @Override
    public List<UserQualificationInfoDO> getTypeCountByCorpInfoId(Long id) {
        QueryWrapper<UserQualificationInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corpinfo_id", id);
        List<UserQualificationInfoDO> result = userQualificationInfoMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<UserQualificationInfoDO> listByCertificateNo(String certificateNo) {
        QueryWrapper<UserQualificationInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("certificate_no", certificateNo);
        queryWrapper.eq("delete_enum", "FALSE");
        List<UserQualificationInfoDO> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<UserQualificationInfoDO> listByIdAndCertificateNo(Long id, String certificateNo) {
        QueryWrapper<UserQualificationInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", id);
        queryWrapper.eq("certificate_no", certificateNo);
        queryWrapper.eq("delete_enum", "FALSE");
        List<UserQualificationInfoDO> list = this.list(queryWrapper);
        return list;
    }
}

